|
| 1 | +package JavaEntry; |
| 2 | + |
| 3 | +public class ArrayEx8 { |
| 4 | + public static void main(String[] arg){ |
| 5 | + String[] fruits = {"Mango", "Apple", "Watermelon", "Orange"}; |
| 6 | + fruits[0] = "Grapes"; //Replace the first element from Mango to Grapes; |
| 7 | + System.out.println(fruits[0] + ", Total no of element is: " + fruits.length); //Print the first element and total element |
| 8 | + |
| 9 | + // ForLoop: for the Array, which will continue till the last element. |
| 10 | + System.out.println("For Loop Iteration -"); |
| 11 | + for(int i=0; i < fruits.length; i++){ |
| 12 | + System.out.println(fruits[i]); |
| 13 | + } |
| 14 | + |
| 15 | + // ForEach: for the Array, which will print all element. |
| 16 | + System.out.println("For Each Iteration -"); |
| 17 | + for(String i:fruits){ |
| 18 | + System.out.println(i); |
| 19 | + } |
| 20 | + |
| 21 | + // Multidimensional Arrays |
| 22 | + int [][] numArr = {{1,2,3,4,5}, {6,7,8,9,10}}; |
| 23 | + |
| 24 | + // Printing each element in Multidimension Arrays |
| 25 | + System.out.println("Multidimension Arrays with ForLoop - "); //For Loop |
| 26 | + for(int i=0; i < numArr.length; i++ ){ |
| 27 | + for(int j=0; j<numArr[i].length; j++){ |
| 28 | + System.out.println(numArr[i][j]); |
| 29 | + } |
| 30 | + } |
| 31 | + |
| 32 | + System.out.println("Multidimension Arrays with ForEach - "); //For Each |
| 33 | + for(int[] arrs: numArr){ |
| 34 | + for(int elem:arrs){ |
| 35 | + System.out.println(elem); |
| 36 | + } |
| 37 | + } |
| 38 | + |
| 39 | + System.out.println("Accessing Arrays element - "); |
| 40 | + System.out.println(numArr[1][0]); // Accessing Second array, first element. |
| 41 | + numArr[0][0] = 11; // Replace First array, first element value. |
| 42 | + System.out.println(numArr[0][0]); // Accessing First array, first element. |
| 43 | + |
| 44 | + } |
| 45 | +} |
0 commit comments