Java Arrays String StringBuilder SringBuffer Vector
Java Arrays String StringBuilder SringBuffer Vector
One-dimensional array
Multidimensional array
One-dimensional Array
1. Declare an array.
2. Assign values to the array.
Declaring an Array
An array needs to be declared before it can be used in
a program. You can declare a one-dimensional array by
using the following syntax:
Declaring an Array
You can declare a two-dimensional array by using the following
syntax:
words[0][0] = “alpep”;
words[0][1] = “apple”;
One-dimensional Array
To access a one-dimensional array, the following syntax
is used:
arrayname[index];
arrayname[row][column];
System.out.println(jumbledWords[0][0]);
However, if you want to display all the elements, you can
use the for loop, as shown in the following code snippet:
String[][] jumbledWords = new String[][]{
{“elapp”,”apple”},{“argneo”,”orange”},{“agrspe”,”grapes”}};
class Array {
// Method 1
// Inside helper class
// to compute length of an array
public Array(int length)
{
arr = new int[length];
}
// Method 2
// Inside helper class
// To print array
public void printArray()
{
if (arr.length == count) {
arr[count++] = element;
}
}
// Class 2
// Main class
public class DynamicArray {
System.out.println(s1==s2);
System.out.println(s1.equals(s2));
Example :
String s=new String(“CIMAGE”);
String s=“CIMAGE”;
Just a Minute
How memory is allocated in above case. In Heap and
String Constant Pool ?
Example :
Just a Minute
How memory is allocated in above case. In Heap and
String Constant Pool ?
The following table lists the some of the most commonly
used methods of the String class
Method Description Example
int length() Returns the length of a String str = “newstring”;
string object.
int len = str.length();
System.out.println(len);
Output:
9
char Returns the character at String str = “Fruit”;
charAt(int index) the specified index, char result = str.charAt(3);
which ranges from 0 to
the length of string object
System.out.println(result);
-1.
Output:
i
void Copies characters from a String Str1 = new String(“Welcome to
java”);
getChars(int source string object into
char[] Str2 = new char[6];
srcBegin,int the destination character Str1.getChars(8, 10, Str2, 0);
srcEnd, array. The first character
to be copied is at index
char[] dst, srcBegin, the last System.out.print(“Value Copied = “);
int dstBegin) character to be copied is
at index srcEnd-1. The System.out.println(Str2);
Output:
character is copied into
Value Copied = to
dst, starting at index,
dstbegin.
boolean Compares the current String str1 = “Fruit”;
string object with the
equals(object other string and returns String str2 = “Fruit”;
obj) a boolean value. boolean result = str1.equals(str2);
System.out.println(result);
Output:
True
Method Description Example
int compareTo( Compares the current String str1 = “fruits”;
string object with String str2 = “fruits are good”;
String str) another string int result = str1.compareTo( str2
);
int indexOf( Returns the index of the String Str = new String(“Welcome to
first occurrence of the Java”);
int ch) specified character System.out.print(“Index Found at:”);
within a string. If the System.out.println(Str.indexOf('o'));
character is not found,
the method returns –1.
Output:
Method Description Example
String Str = new String(“Welcome to
int Returns the index of the
Java”);
specified character
lastIndexOf occurring last in the System.out.print(“Last Index Found
at :” );
(int ch) string. If the character
System.out.println(Str.lastIndexOf(
is not found, the 'o' ));
method, indexOf(),
Output:
returns –1.
Last Index Found at :9
s1.concat(“Fall”);
String s2=s1.concat(“Winter”);
s2.concat(“Summer”);
System.out.println(s1);
System.out.println(s2);
String s1=new String(“We like core Java”);
System.out.println(s1==s2); //false
System.out.println(s1==s3); //false
System.out.println(s3==s4); //true
System.out.println(s4==s5); //true
System.out.println(s4==s9); //true
Method Description Example
String String Str = new String(“Welcome to
Converts the string Java”);
toUpperCase() to uppercase and System.out.print(“Value Returned:”);
System.out.println(Str.toUpperCase()
returns it. );
Output:
Value Returned: WELCOME TO JAVA
String String Str = new String(“WELCOME TO
Converts the string JAVA”);
toLowerCase() into lowercase and System.out.print(“Value Returned:”);
returns it. System.out.println(Str.toLowerCase())
;
Output:
Value Returned: welcome to java
Output:
Return Value : 10
Note :-
StringBuffer class has the same methods as that of the
StringBuilder class.
However, the methods in StringBuffer class are
synchronized
The following table lists the some of the most commonly
used methods of the StringBuilder class
Method Description Example
StringBuilder Appends the argument StringBuilder sb = new
append(String to the string builder. StringBuilder(“Fruits “);
obj) sb.append(“are good for health”);
System.out.println(sb);
Output:
Fruits are good for health
StringBuilder Deletes the sequence from StringBuilder str = new
delete(int start to end in the char StringBuilder(“fruits are very
start, int sequence. good”);
end)
str.delete(10, 15);
System.out.println(“After deletion =
“+ str);
Output:
After deletion = fruits are good
StringBuilder Inserts the second StringBuilder str = new
insert(int argument into the string StringBuilder(“fruitsgood”);
offset, String builder. The first argument str.insert(6, “ are “);
obj) indicates the index before
System.out.print(“After insertion =
which the data is to be
“);
inserted.
System.out.println(str.toString());
Output:
After insertion = fruits are good
StringBuilder The sequence of StringBuilder str = new
reverse() characters in the string StringBuilder(“fruits”);
builder is reversed. System.out.println(“reverse = “ +
str.reverse());
Output:
reverse = stiurf
Just A Minute
StringBuffer sb1=new StringBuffer(“CIMAGE”);
StringBuffer sb2=new StringBuffer(“CIMAGE”);
System.out.println(sb1==sb2)
System.out.println(sb1.equals(sb2));
Method Description
boolean add(E e) Is used to add the specified object at the end of
Vector.
void clear() Is used to remove all the objects from Vector.
E get(int index) Is used to retrieve the object at the specified index from Vector.
E remove(int index) Is used to remove the object at the specified index from Vector.
boolean Is used to remove the first occurrence of the specified object from Vector.
remove(Object o)
int size() Is used to get the number of objects in Vector.
The following table lists the commonly used methods of the Vector class.
Method Description
import java.util.ListIterator;
import java.util.Vector;
public class VectorDemo
{
public static void main(String[] args)
{
Vector<Double> obj = new Vector<Double>();
Double dobj1 = new Double(77.5);
Double dobj2 = new Double(68.1);
Double dobj3 = new Double(52.8);
Double dobj4 = new Double(40.2);
System.out.println("Size of Vector is: "
+obj.size());
obj.add(dobj1);
obj.add(dobj2);
obj.add(dobj3);
obj.add(dobj4);
obj.add(dobj1);
Vector after adding the objects: [77.5, 68.1, 52.8, 40.2, 77.5] Size of
Vector after adding objects: 5
Vector after removing the objects: [68.1, 40.2, 77.5] Size of Vector after
removing objects: 3