Array Indexing ============== Index/Subscript/Radix ==> Positive number ==> to access individual elements of an array, we can use the index. Syntax: array-name[index-number] Note: ===== index range ==> 0 to n - 1 Array Index Out of Bounds: ========================== ==> a kind of error ==> when we can use the index which is from the out of range of index class ArrayIndexing{ public static void main(String x[]) { char ch[] = {'J','A','V','A', ' ','P','R','O','G','R','A','M'}; System.out.println("The Characters of the given character array are = "); System.out.println(ch[0]); System.out.println(ch[1]); System.out.println(ch[2]); System.out.println(ch[3]); System.out.println(ch[4]); System.out.println(ch[5]); System.out.println(ch[6]); System.out.println(ch[7]); System.out.println(ch[8]); System.out.println(ch[9]); System.out.println(ch[10]); System.out.println(ch[11]); // System.out.println(ch[-1]); } } ================================================== How to iterate over the arrays =============================== looping on arrays using while loop: ================= // WAP TO PRINT ALL THE CHARACTERS OF THE ARRAY ALONG WITH ITS INDEX VALUE. /* THE CHARACTER AT INDEX '0' = J */ class ArrayIteration{ public static void main(String[] args) { // defining the array float[] af = {1.23f,2.34f,1.02f,1.45f,1.55f}; // float array int i = 0; while(i < 5) { System.out.println("The Element at index "+i+" is = "+af[i]); ++i; } } } ======================== finding the length of an array: =============================== 1) using loops 2) using "length" property ========================== ==> built-property which we can use to get the array size/length Syntax: array_name.length // WAP TO PRINT ALL THE CHARACTERS OF THE ARRAY ALONG WITH ITS INDEX VALUE. /* THE CHARACTER AT INDEX '0' = J */ class ArrayIteration{ public static void main(String[] args) { // defining the array float[] af = {1.23f,2.34f,1.02f,1.45f,1.55f}; // float array // System.out.println("The Total Number of elements in the given array = "+(af.length)); int len = af.length; int i = 0; while(i < len) { System.out.println("The Element at index "+i+" is = "+af[i]); ++i; } } } ===================== using for loop: =============== // WAP TO FIND THE TOTAL NUMBER OF ELEMENTS OF AN ARRAY WITHOUT length PROPERTY. class ArrayLength{ public static void main(String[] args) { double[] da = {1,2,3,4,5,6,7,8,9,10}; int count = 0; for(int i = 0;i < da.length;i++) { count++; } System.out.println("The Array Size = "+count); } } ============================ for each loop: ============= Syntax: for(datatype iteration_variable : array_name) { loop body } // WAP TO FIND THE TOTAL NUMBER OF ELEMENTS OF AN ARRAY WITHOUT length PROPERTY. class ArrayLength{ public static void main(String[] args) { double[] da = {1,2,3,4,5,6,7,8,9,10}; int count = 0; for(double i : da) { count++; } System.out.println("The Array Size = "+count); } } ========================== // WAP TO FIND THE SUM OF ELEMENTS OF AN ARRAY. // USING FOR LOOP class ArraySum{ public static void main(String[] args) { int[] a = {1,11,111,1111,11111,1111,111,11,1,0,1,2,3,4,5}; int sum = 0; for(int i = 0;i < a.length;++i) { sum = sum + a[i]; } System.out.println("The sum of array elements = "+sum); } } ===================================== // WAP TO FIND THE SUM OF ELEMENTS OF AN ARRAY. // USING FOR LOOP class ArraySum{ public static void main(String[] args) { int[] a = {1,11,111,1111,11111,1111,111,11,1,0,1,2,3,4,5}; int sum = 0; for(int i:a) { sum += i; } System.out.println("The sum of array elements = "+sum); } } ================================= // WAP TO DEFINE THE CHARACTER ARRAY AND PRINT ALL THE ELEMENTS OF CHARACTER ARRAY ALONG WITH CORRESPONDING INDEX VALUE IN REVERSE. class CharacterArray{ public static void main(String[] args) { char a[] = {'a','b','c','d','e','f'}; for(int i = a.length-1;i >= 0;--i) { System.out.println("The Character at an index "+i+" is = "+a[i]); } } } ================ // WAP TO PRINT THE ELMENTS OF AN ARRAY WHICH ARE AT EVEN INDEX ONLY. class ElementsAtEvens{ public static void main(String[] args) { short s[] = {-1,10,1,0,11,-132,197}; System.out.println("The elements at even indx are:"); for(int i = 0;i < s.length;++i) { if(i % 2 == 0) { System.out.println(s[i]); } } } } ========================= // WAP TO PRINT THE ELMENTS OF AN ARRAY WHICH ARE AT EVEN INDEX ONLY. class ElementsAtEvens{ public static void main(String[] args) { short s[] = {-1,10,1,0,11,-132,197}; System.out.println("The elements at even indx are:"); int index = 0; for(short i:s) { if(index % 2 == 0) { System.out.println(s[index]); } ++index; } } } Assignment: =========== 1) WAP in JAVA to access the array elements in reverse along with its index using the for each loop. 2) WAP TO FIND THE SUM OF ARRAY ELEMENTS AT ODD INDEX PLACES USING FOR EACH LOOP.