Arrays in Java
Arrays in Java
Arrays in java are objects which belongs to class Array. Array objects implements only static arrays. i.e once you create an array, its length is fixed. User can not change the length after array creation. Java follows strict bound checking for referencing array elements. If an attempt is made to reference the elements outside the bounds then ArrayIndexOutOfBoundsException will be thrown at run time. Array elements can be referenced from 0 as LB to length-1 as UB. Java also follows strict type checking for Array elements. If an attempt is made to store the elements of another type then ArrayStoreException will be thrown at run time. Length is the attribute of each array which can be referenced by <arrayreference> . <length>
4. Variable Size Array: int x[][] = new int[3][]; x[0] = new int[2]; x[1] = new int[4]; x[2] = new int[3];
Examples
int a[] = { 10,8,6}; for(int i=0;i<a.length;i++) System.out.println(a[i]);
10 8 6
//int table[][]={0,0,0,1,1,1}; //int table[2][3]={0,0,0,1,1,1}; int table[ ][ ]={{0,0,0},{1,1,1}}; for(int i=0;i<2;i++) { for(int j=0;j<3;j++) { System.out.print(table[i][j]);} System.out.println(); }
WRONG
000 111
class arrTest { public static void main(String args[]) { int data[][] ={{0,1},{0,1,2},{1},{1,2,3,4,5}}; System.out.println(data[0][0]); //System.out.println(data[0][2]); System.out.println(data[1][2]); System.out.println(data[2][0]); //System.out.println(data[2][1]); } // End of main() Method } // End of class arrTest
0
ArrayIndexOutofBounds Exception
2 1
ArrayIndexOutofBounds Exception
Array of Objects
BOX box[] = new BOX[6];
BOX[ ]
box[0] box[1] box[2] box[3] box[4] box[5] null null null null null null
box
SELF STUDY
static boolean
static boolean static boolean static boolean static boolean static boolean static boolean static boolean
double data[] = { 12.5,34.6,90.56,12.34,12.56}; double array Size = 5 LB=0 UB =4 float values[] = { 10.45f,23.56f,12.67f}; int arrays Size = 3 LB=0 UB =2 double data1[] = new double[10]; double array Size = 10 LB=0 UB =9 boolean flags[] = new boolean[5]; boolean array Size = 5 LB=0 UB =4 int x1[] = {10,6,8,20}; int array Size = 4 LB=0 UB =3
System.out.println(Arrays.binarySearch(x,20));
Arrays.sort(x); for(int i=0;i<x.length;i++) System.out.print(x[i]+" "); System.out.println(); Prints index of 20 in x Sorts elements of x 3
Prints Elements of x 6 8 10 20
12.56 12.56 12.56 12.56 12.56 12.56 12.56 12.56 12.56 12.56
Arrays.fill(flags,2,5,true); //Arrays.fill(flags,2,6,true); for(int i=0;i<flags.length;i++) System.out.print(flags[i]+" "); System.out.println(); System.out.println(Arrays.equals(x,x1)); Arrays.sort(values); for(int i=0;i<values.length;i++) System.out.print(values[i]+" "); System.out.println();
Fills true value in boolean array flags from index 2 to 4 ArrayIndexOutOfBoundsEx ception false false true true true
} } // End of class
OUTPUT
D:\java\bin>java ArrayExample 3 6 8 10 20 12.56 12.56 12.56 12.56 12.56 12.56 12.56 12.56 12.56 12.56 false false true true true false 10.45 12.67 23.56
Predict OutPut
int x[] = new int[10]; for(int i=0;i<x.length;i++) System.out.print(x[i]+" "); boolean flags[] = new boolean[10]; for(int i=0;i<flags.length;i++) System.out.print(flags[i]+" "); false false false false false false false false false false boolean flags[] = new boolean[10]; Arrays.fill(flags,2,8,true); for(int i=0;i<flags.length;i++) System.out.print(flags[i]+" "); false false true true true true true true false false 0000000000
Exception in thread "main" java.lang.ArrayIndexOutOfBoundsException: Array index out of range: 11 at java.util.Arrays.rangeCheck(Arrays.java:1325) at java.util.Arrays.fill(Arrays.java:2213) at ArrayExample2.main(ArrayExample2.java:7)
Exercise1: class Student { private String name; private String idno; private double marks[ ][ ]; // Assume all accessor and mutator methods defined earlier
// Define a method which returns a Student with highest total in all subjects
Student getStudentWithHighestScore(Student[] students) { . }
// Define a method which returns a Student with highest total in a given subject
Student getStudentwithSubjectHighest(Student[] students , int subject) { . } }
Exercise 2: class Matrix { private int ROWS; private int COLS; double values[][]; Matrix(int r,int c) { . } // Provide Accessor Marks for getting a element, all elements of a a given row, all elements of a given column // Provide Mutator methods for the same Matrix add(Matrix other) { .. } static Matrix add(Matrix first, Matrix second) { } // Supply opeartions for multiplication as well as subtarction // Methods for matrix equallity (static flavor also) // Transpose of a matrix // boolean isTranspose(Matrix other) returns true if other is transpose of this matrix }