Java – Program to display array elements using for each loop
- For-each loop is used to process Array or Collection elements easily.
- For-each loop process elements from start to end without using index.
- For-each loop process elements only in forward direction.
class Code { public static void main(String[] args) { int[] arr = {4, 2, 8, 9, 1, 6, 7, 5}; System.out.println(“Array elements using for-each loop : “); for (int x : arr) { System.out.println(x); } } } |
Java – Replace the first element and last element with 10
class Code { public static void main(String[] args) { int[] arr = {4, 2, 8, 9, 1, 6, 7, 5}; arr[0] = 10; arr[arr.length-1] = 10; System.out.println(“Array elements : “); for (int x : arr) { System.out.println(x); } } } |