Chapter 2.3
Chapter 2.3
Derived Syntactical
Constructs in Java
Mr. R. M. Patil
SY CO, JPR
2023-2024
2.3. Array in Java
Normally, an array is a collection of similar type of
elements which has contiguous memory location.
Disadvantages:
Types of Array
Single (One) Multidimensional
Dimensional Array Array
2.3. Array in Java – 1. Single Dimensional Array
Syntax to Declare an
Array in Java: Instantiation of an Array in Java:
dataType[] arr; (or) arrayRefVar=new datatype[size];
dataType []arr; (or)
dataType arr[];
2.3. Array in Java – 1. Single Dimensional Array
class Testarray
{
public static void main(String args[])
{
int a[]=new int[5]; //declaration and instantiation Output:
a[0]=10; //initialization 10
a[1]=20; 20
a[2]=70;
a[3]=40;
70
a[4]=50; 40
50
for(int i=0;i<a.length;i++) //length is the property of array
{
System.out.println(a[i]);
}
}
}
2.3. Array in Java – 1. Single Dimensional Array
int a[]={33,3,4,5};
class Testarray1
{
public static void main(String args[])
{
int a[]={33,3,4,5}; //declaration, instantiation and initialization
class Testarray1
{
public static void main(String args[]) Output:
{
33
int arr[]={33,3,4,5};
3
4
for(int i : arr)
5
{
System.out.println(i);
}
}
}
2.3. Array in Java – 2. Multidimensional Array
Syntax to Declare an
Array in Java:
dataType []arrayRefVar[];
2.3. Array in Java – 2. Multidimensional Array
arr[0][0]=1;
arr[0][1]=2;
arr[0][2]=3;
arr[1][0]=4;
arr[1][1]=5;
arr[1][2]=6;
arr[2][0]=7;
arr[2][1]=8;
arr[2][2]=9;
2.3. Array in Java – 2. Multidimensional Array
class Testarray3
{
public static void main(String args[])
{
int arr[][]={{1,2,3},{2,4,5},{4,4,5}}; Output:
for(int i=0;i<3;i++)
{ 123
for(int j=0;j<3;j++) 245
{
System.out.print(arr[i][j]+" "); 445
}
System.out.println();
}
}
}
2.3. Array in Java - Cloning an Array in Java
Since, Java array implements the Cloneable interface,
we can create the clone of the Java array.
int a []={33,3,4,5};
int b[]=a.clone();
2.3. Array in Java - Cloning an Array in Java
class test
{
public static void main(String args[]) Output:
{ a[0]=10
int []a = {10,20,30,40,50}; a[1]=20
int []b = a.clone(); a[2]=30
a[3]=40
for(int i=0; i<a.length;i++) a[4]=50
System.out.println("a["+i+"]="+a[i]); b[0]=10
b[1]=20
for(int i=0; i<b.length;i++) b[2]=30
System.out.println("b["+i+"]="+b[i]);
b[3]=40
}
}
b[4]=50
2.3. Array in Java - Jagged Array in Java
is same as:
String s=“sitpoly";
2.3. Java String Class
The java.lang.String class provides a lot of methods to
work on string.
System.out.println(s.toUpperCase()); //SACHIN
System.out.println(s.toLowerCase()); //sachin
System.out.println(s); // Sachin
System.out.println(s.trim()); //Sachin
2.3. Java String Class Methods - Example
String s="Sachin";
System.out.println(s.startsWith("Sa")); //true
System.out.println(s.endsWith("n")); //true
String s="Sachin";
System.out.println(s.charAt(0)); //S
System.out.println(s.charAt(3)); //h
String s="Sachin";
System.out.println(s.length()); //6
2.3. Java String Class Methods - Example
String replaceString=s1.replace("Java","Kava");
System.out.println(replaceString);
2.3. Java String Class Methods - Example
import java.io.*;
import java.util.*;
class test
{
public static void main (String[] args)
{
String s= "sitpolytechnic";
String s1 = "sit";
String s2 = "yadrav";
System.out.println("Concatenated string = " +s1.concat(s2));
2.3. Java String Class Methods - Example
String s4 = "Learn Share Learn";
out = "SiT".equalsIgnoreCase("sit");
System.out.println("Checking Equality(IgnoreCase) " + out);
import java.io.*;
class stbuff
{
public static void main(String[] args)
{ Output:
sitpoly
StringBuffer s = new StringBuffer("sit"); sitpoly1
s.append("poly");
System.out.println(s);
s.append(1);
System.out.println(s);
}
}
2.3. Java StringBuffer Class - Methods
3. insert( ): It is used to insert text at the specified index position.
import java.io.*;
class stbuff Output:
sitpoforlytechnic
{
11sitpoforlytechnic
public static void main(String[] args) 11rahulsitpoforlytechnic
{
StringBuffer s = new StringBuffer("sitpolytechnic");
s.insert(5, "for");
System.out.println(s);
s.insert(0, 11);
System.out.println(s);
char a_arr[] = { 'r', 'a', 'h', 'u', 'l' };
s.insert(2, a_arr); //insert character array at offset 9
System.out.println(s);
}
}
2.3. Java StringBuffer Class - Methods
4. reverse( ): It can reverse the characters within a StringBuffer
object using reverse( ).This method returns the reversed object on
which it was called. .
Here, start Index specifies the index of the first character to remove,
and end Index specifies an index one past the last character to
remove. Thus, the substring deleted runs from start Index to
endIndex–1.
import java.io.*;
class stbuff
{
public static void main(String[] args)
{
StringBuffer s = new StringBuffer("sitpolytechnic");
s.delete(0, 5);
System.out.println(s);
s.deleteCharAt(7); Output:
System.out.println(s);
lytechnic
} lytechnc
}
2.3. Java StringBuffer Class - Methods
6. replace() : It can replace one set of characters with another set
inside a StringBuffer object by calling replace( ). The substring being
replaced is specified by the indexes start Index and endIndex.
System.out.println(s.charAt(2));
s.setLength(30);
2.3. String Vs. StringBuffer Class
Constructor Description
It is used to replace each element of the list with the result of applying the operator
32) replaceAll() to that element.
It is used to retain only that element in the vector which is contained in the specified
33) retainAll() collection.
It is used to replace the element at the specified position in the vector with the
34) set() specified element.
It is used to set the component at the specified index of the vector to the specified
35) setElementAt()
object.
36) setSize() It is used to set the size of the given vector.
37) size() It is used to get the number of components in the given vector.
38) sort() It is used to sort the list according to the order induced by the specified Comparator.
It is used to create a late-binding and fail-fast Spliterator over the elements in the
39) spliterator()
list.
It is used to get a view of the portion of the list between fromIndex, inclusive, and
40) subList() toIndex, exclusive.
41) toArray() It is used to get an array containing all of the elements in this vector in correct order.
v.addElement("Rat");
v.addElement("Cat");
v.addElement("Deer");
v.add("Tiger");
v.add("Lion");
v.add("Dog");
v.add("Elephant");
}
}
2.3. Vector class in Java – Ex. 03
import java.util.*;
public class VectorExample2
{
public static void main(String args[])
{
Vector v = new Vector();
v.add(100);
v.add(200);
v.add(300);
v.add(200);
v.add(400);
v.add(500);
v.add(600);
v.add(700);
System.out.println("Values in vector: " +v);
2.3. Vector class in Java – Ex. 03
v.removeElementAt(5);
For example,
// Using addAll()
Vector<String> v2 = new Vector<>();
v2.add("Crocodile");
v2.addAll(v1);
System.out.println("New Vector: " + v2);
}
}
HOMEWORK
o Change the value in Method: Java supports only call by value. So,
if we pass a primitive value, it will not change the original value.
But, if we convert the primitive value in an object, it will change the
original value.
Example:
int a=20;
Integer j = a;
Example:
Integer a = 3;
int j = a;