Java
Arrays
Functions
Arrays
 Step1 : Create a reference. Cannot mention
the size.
◦ Datatype [ ] arrname;
◦ Datatype arrname[ ]; Valid – not recommended
 Step 2 : Reserve memory
◦ arrname = new datatype[SIZE];
 Step3 : Access elements
 Array index begins from 0.
 int [] a = {5 , 8 , 10}; //Valid (decl + def).
mohammed.sikander@cranessoftware.
com 2
Arrays
int [] a , b;
int c [ ] , d;
a and b; both are arrays
c is array and d is not a array
Arrays
To know the number of elements
Arr.length
int [ ] arr1 = {3 , 9 , 1, 4 , 7 };
System.out.println(arr1.length);
int [ ] arr2;
System.out.println(arr2.length);
int [ ] arr3;
arr3 = new int[10];
System.out.println(arr3.length);
Array - Accessing elements
 Method 1 :
 Find Length : arr.length
 for(int index = 0; index < arr.length ; index++)
 System.out.println(arr[index]);
 Method 2:
 for(int x : arr)
 System.out.println(x);
mohammed.sikander@cranessoftware.
com 5
int [ ] arr = {3 , 9 , 1, 4 , 7 };
for(int val : arr)
System.out.println(val);
float [ ] arr = {3 , 9 , 1, 4 , 7 };
for(float val : arr)
System.out.println(val);
int [ ] arr = {3 , 9 , 1, 4 , 7 };
int val;
for(val : arr)
System.out.println(val);
Can we use == to compare
arrays?
int [] arr1 = {7 , 5 , 2 , 8};
int [] arr2 = {7 , 5 , 2 , 8};
if(arr1 == arr2)
System.out.println("Equal");
else
System.out.println("Not Equal");
1. int [] arr1 = {7 , 5 , 2 , 8};
2. int [] arr2 ;
3. arr2 = arr1;
4. if(arr1 == arr2)
5. System.out.println("Equal");
6. else
7. System.out.println("Not
Equal");
Can we use == to compare
arrays?
Can we assign character variable to
integer?
1. char c = 'A';
2. int x = c;
Can we assign from character array to
integer array?
3. char [] carr = {'S','I','K'};
4. int [] iarr = carr;
 int [] arr;
 arr = new int[5];
 arr[0] = 12;
 arr[1] = 23;
 arr[2] = 34;

 for(int i = 0 ; i < arr.length ; i++)
 System.out.println(arr[i]);
Multi-dimensional Array
 int [] [] marks;
 int row = 3;
 int col = 4;
 marks = new int[row][col];
 for(int i = 0 ; i < row ; i++)
 for(int j = 0 ; j < col ; j++)
 marks[i][j] = i + j;
Multi-dimensional Arrays
 Method1 : Declare and initialise
 int [][] a = { {1 , 2 , 3 , 4} , {5,6,7} , {8}};
 1st row has 4 col, 3rd row has 1 col.
 System.out.println("No. of Rows = " + a.length);
 for(int x = 0 ; x < a.length ; x++)
 System.out.println("No. of Columns in " + x + " = " +
a[x].length);
 for(int r = 0 ; r < a.length ; r++)
 {
 for(int c = 0 ; c < a[r].length ; c++)
 System.out.print(a[r][c] + " ");
 } mohammed.sikander@cranessoftware.
com 12
 int [] [] marks;
 int row = 3;
 int col = 4;
 marks = new int[row][];

 for(int i = 0 ; i < row ; i++)
 for(int j = 0 ; j < col ; j++)
 marks[i][j] = i + j;
for(int i = 0 ; i < row ; i++)
marks[i] = new int[col];
Multi-dimensional Array
Multi-Dimensional Array
 int [][] mat = {{5 , 8 , 9 } , {2 , 4 } , {6}};
 mat[0][0]
 mat[0][1]
 mat[0][2]
 mat[1][0]
 mat[1][1]
 mat[1][2]
 mat[2][0]
 mat[2][1]
 mat[2][2]
 mat[0][0]
 mat[0][1]
 mat[0][2]
 mat[1][0]
 mat[1][1]
 mat[1][2]
 mat[2][0]
 mat[2][1]
 mat[2][2]
Copying Arrays
int [] arr1 = {5 ,8 , 10, 15, 20};
int [] arr2;
arr2 = arr1;
for(int x: arr2)
System.out.println(x);
arr1[0] = 2; arr1[1] = 4;
for(int x: arr2)
System.out.println(x);
 System.arraycopy(src Array, src pos,
destination Array, dest pos, length);
 int [] arr1 = {5 ,8 , 10, 15, 20};
 int [] arr2 = new int[5];

 System.arraycopy(arr1, 0, arr2, 0, 5);
int [] arr1 = {5 ,8 , 10, 15, 20};
int [] arr2 = Arrays.copyOf(arr1, 5);
for(int x: arr2)
System.out.println(x);
Sorting of Array
 Arrays.sort(Type [] arr);
 Arrays.sort(Type [], int fromIndex, int toIndex);
 int arr[] = {5,2,9,1,2,8};
 Arrays.sort(arr); OR
 Arrays.sort(arr,0,6);
 Output : 1 2 2 5 8 9
 int arr[] = {5,2,9,1,2,8};
 Arrays.sort(arr,1,5);
 Output : 5 1 2 2 9 8
Anonymous Arrays
public static void printArray(int [] x)
{
for(int i : x)
System.out.println(x);
}
public static void main(String [] args)
{
int [] marks = {3,6,4,7};
1. printArray(marks);
2. printArray(1 ,3 ,5 ,7);
3. printArray([]{1 ,3 ,4 ,5});
4. printArray(new int []{1 ,3 ,4 ,5});
}
Identify the valid calls to
printArray?
FUNCTIONS
Argument Passing
public static void function(int x)
{
x = 20;
System.out.println("Function " + x);
}
public static void main(String [] args)
{
int x = 5;
function(x);
System.out.println("Main " + x);
}
Passing Array to Function
public class ArgumentPassingArray {
public static void function(int [] arr)
{
arr[0] = 20;
}
public static void main(String [] args)
{
int [] arr = {5,8,10};
function(arr);
System.out.println("Main " + arr[0]);
}
}
String str;
str = new String("SIKANDER");
System.out.println(str.hashCode());
str = str.toLowerCase();
System.out.println(str.hashCode());
Argument Passing to Function
class Rectangle {
int length ;
int breadth;
Rectangle(int l , int b) {
length = l ;
breadth = b;
}
}
public class FunctionDemo {
public static void function(Rectangle x)
{
x.length = 20;
System.out.println("Function " + x.length + " " + x.breadth);
}
public static void main(String [] args)
{
Rectangle obj = new Rectangle(4 ,5 );
function(obj);
System.out.println("Main " + obj.length + " " + obj.breadth);
}
}
class Rectangle {
int length ;
int breadth;
Rectangle(int l , int b) {
length = l ;
breadth = b;
}
}
public class FunctionDemo {
public static void function(Rectangle x)
{
x = new Rectangle( 7 , 9);
System.out.println("Function " + x.length + " " + x.breadth);
}
public static void main(String [] args)
{
Rectangle obj = new Rectangle(4 ,5 );
function(obj);
System.out.println("Main " + obj.length + " " + obj.breadth);
}
}

Java arrays

  • 1.
  • 2.
    Arrays  Step1 :Create a reference. Cannot mention the size. ◦ Datatype [ ] arrname; ◦ Datatype arrname[ ]; Valid – not recommended  Step 2 : Reserve memory ◦ arrname = new datatype[SIZE];  Step3 : Access elements  Array index begins from 0.  int [] a = {5 , 8 , 10}; //Valid (decl + def). mohammed.sikander@cranessoftware. com 2
  • 3.
    Arrays int [] a, b; int c [ ] , d; a and b; both are arrays c is array and d is not a array
  • 4.
    Arrays To know thenumber of elements Arr.length int [ ] arr1 = {3 , 9 , 1, 4 , 7 }; System.out.println(arr1.length); int [ ] arr2; System.out.println(arr2.length); int [ ] arr3; arr3 = new int[10]; System.out.println(arr3.length);
  • 5.
    Array - Accessingelements  Method 1 :  Find Length : arr.length  for(int index = 0; index < arr.length ; index++)  System.out.println(arr[index]);  Method 2:  for(int x : arr)  System.out.println(x); mohammed.sikander@cranessoftware. com 5
  • 6.
    int [ ]arr = {3 , 9 , 1, 4 , 7 }; for(int val : arr) System.out.println(val); float [ ] arr = {3 , 9 , 1, 4 , 7 }; for(float val : arr) System.out.println(val); int [ ] arr = {3 , 9 , 1, 4 , 7 }; int val; for(val : arr) System.out.println(val);
  • 7.
    Can we use== to compare arrays? int [] arr1 = {7 , 5 , 2 , 8}; int [] arr2 = {7 , 5 , 2 , 8}; if(arr1 == arr2) System.out.println("Equal"); else System.out.println("Not Equal");
  • 8.
    1. int []arr1 = {7 , 5 , 2 , 8}; 2. int [] arr2 ; 3. arr2 = arr1; 4. if(arr1 == arr2) 5. System.out.println("Equal"); 6. else 7. System.out.println("Not Equal"); Can we use == to compare arrays?
  • 9.
    Can we assigncharacter variable to integer? 1. char c = 'A'; 2. int x = c; Can we assign from character array to integer array? 3. char [] carr = {'S','I','K'}; 4. int [] iarr = carr;
  • 10.
     int []arr;  arr = new int[5];  arr[0] = 12;  arr[1] = 23;  arr[2] = 34;   for(int i = 0 ; i < arr.length ; i++)  System.out.println(arr[i]);
  • 11.
    Multi-dimensional Array  int[] [] marks;  int row = 3;  int col = 4;  marks = new int[row][col];  for(int i = 0 ; i < row ; i++)  for(int j = 0 ; j < col ; j++)  marks[i][j] = i + j;
  • 12.
    Multi-dimensional Arrays  Method1: Declare and initialise  int [][] a = { {1 , 2 , 3 , 4} , {5,6,7} , {8}};  1st row has 4 col, 3rd row has 1 col.  System.out.println("No. of Rows = " + a.length);  for(int x = 0 ; x < a.length ; x++)  System.out.println("No. of Columns in " + x + " = " + a[x].length);  for(int r = 0 ; r < a.length ; r++)  {  for(int c = 0 ; c < a[r].length ; c++)  System.out.print(a[r][c] + " ");  } mohammed.sikander@cranessoftware. com 12
  • 13.
     int [][] marks;  int row = 3;  int col = 4;  marks = new int[row][];   for(int i = 0 ; i < row ; i++)  for(int j = 0 ; j < col ; j++)  marks[i][j] = i + j; for(int i = 0 ; i < row ; i++) marks[i] = new int[col]; Multi-dimensional Array
  • 14.
    Multi-Dimensional Array  int[][] mat = {{5 , 8 , 9 } , {2 , 4 } , {6}};  mat[0][0]  mat[0][1]  mat[0][2]  mat[1][0]  mat[1][1]  mat[1][2]  mat[2][0]  mat[2][1]  mat[2][2]  mat[0][0]  mat[0][1]  mat[0][2]  mat[1][0]  mat[1][1]  mat[1][2]  mat[2][0]  mat[2][1]  mat[2][2]
  • 15.
    Copying Arrays int []arr1 = {5 ,8 , 10, 15, 20}; int [] arr2; arr2 = arr1; for(int x: arr2) System.out.println(x); arr1[0] = 2; arr1[1] = 4; for(int x: arr2) System.out.println(x);
  • 16.
     System.arraycopy(src Array,src pos, destination Array, dest pos, length);  int [] arr1 = {5 ,8 , 10, 15, 20};  int [] arr2 = new int[5];   System.arraycopy(arr1, 0, arr2, 0, 5);
  • 17.
    int [] arr1= {5 ,8 , 10, 15, 20}; int [] arr2 = Arrays.copyOf(arr1, 5); for(int x: arr2) System.out.println(x);
  • 18.
    Sorting of Array Arrays.sort(Type [] arr);  Arrays.sort(Type [], int fromIndex, int toIndex);  int arr[] = {5,2,9,1,2,8};  Arrays.sort(arr); OR  Arrays.sort(arr,0,6);  Output : 1 2 2 5 8 9  int arr[] = {5,2,9,1,2,8};  Arrays.sort(arr,1,5);  Output : 5 1 2 2 9 8
  • 19.
    Anonymous Arrays public staticvoid printArray(int [] x) { for(int i : x) System.out.println(x); } public static void main(String [] args) { int [] marks = {3,6,4,7}; 1. printArray(marks); 2. printArray(1 ,3 ,5 ,7); 3. printArray([]{1 ,3 ,4 ,5}); 4. printArray(new int []{1 ,3 ,4 ,5}); } Identify the valid calls to printArray?
  • 20.
  • 21.
    Argument Passing public staticvoid function(int x) { x = 20; System.out.println("Function " + x); } public static void main(String [] args) { int x = 5; function(x); System.out.println("Main " + x); }
  • 22.
    Passing Array toFunction public class ArgumentPassingArray { public static void function(int [] arr) { arr[0] = 20; } public static void main(String [] args) { int [] arr = {5,8,10}; function(arr); System.out.println("Main " + arr[0]); } }
  • 23.
    String str; str =new String("SIKANDER"); System.out.println(str.hashCode()); str = str.toLowerCase(); System.out.println(str.hashCode());
  • 24.
    Argument Passing toFunction class Rectangle { int length ; int breadth; Rectangle(int l , int b) { length = l ; breadth = b; } } public class FunctionDemo { public static void function(Rectangle x) { x.length = 20; System.out.println("Function " + x.length + " " + x.breadth); } public static void main(String [] args) { Rectangle obj = new Rectangle(4 ,5 ); function(obj); System.out.println("Main " + obj.length + " " + obj.breadth); } }
  • 25.
    class Rectangle { intlength ; int breadth; Rectangle(int l , int b) { length = l ; breadth = b; } } public class FunctionDemo { public static void function(Rectangle x) { x = new Rectangle( 7 , 9); System.out.println("Function " + x.length + " " + x.breadth); } public static void main(String [] args) { Rectangle obj = new Rectangle(4 ,5 ); function(obj); System.out.println("Main " + obj.length + " " + obj.breadth); } }

Editor's Notes

  • #4 Error: b is an array
  • #5 Length can be access only if array has been defined (using new / initialised eles).
  • #6 Length can be access only if array has been defined (using new / initialised eles).
  • #8 No Compilation Error Output : Not Equal , as they are pointing to different arrays.
  • #9 Line 3 : Valid Output : Equal.
  • #10 Line 2 is valid Line 4 is not Valid
  • #11 12 23 34 0 0
  • #12 Draw the diagram: Assume marks as pointer to pointer New int[row][col]; will first create an array of pointers [row] and then Allocates memory for each row (col no. of elements). We can place the elements
  • #13 No. of Rows = 4 No. of Columns in 0 = 4 No. of Columns in 1 = 3 No. of Columns in 2 = 1 1 2 3 4 5 6 7 8
  • #14 Error :
  • #16 public class ArrayCopy { public static void main(String [] args) { int [] arr1 = {5 ,8 , 10, 15, 20}; int [] arr2; arr2 = arr1; //Copies only reference. for(int x: arr2) System.out.println(x); arr1[0] = 2; arr1[1] = 4; for(int x: arr2) System.out.println(x); } }
  • #17 https://docs.oracle.com/javase/tutorial/java/nutsandbolts/arrays.html public class ArrayCopyMethod { public static void main(String [] args) { int [] arr1 = {5 ,8 , 10, 15, 20}; int [] arr2 = new int[5]; System.arraycopy(arr1, 0, arr2, 0, 5); for(int x: arr2) System.out.println(x); arr1[0] = 2; arr1[1] = 4; for(int x: arr2) System.out.println(x); } }
  • #18 import java.util.*; public class ArrayCopyMethod { public static void main(String [] args) { int [] arr1 = {5 ,8 , 10, 15, 20}; //int [] arr2 = new int[5]; //System.arraycopy(arr1, 0, arr2, 0, 5); int [] arr2 = Arrays.copyOf(arr1, 5); for(int x: arr2) System.out.println(x); // arr1[0] = 2; arr1[1] = 4; // // for(int x: arr2) // System.out.println(x); } }
  • #20 Line 2 and 3 will not work
  • #22 Its pass by Value
  • #23 Pass by reference
  • #25 Reference types are always passed by reference.
  • #26 X refers to new object.