Part 19
Part 19
In Java, you can declare, instan ate, and ini alize an array in a single line, as demonstrated below:
3. class Testarray1{
6. //prin ng array
8. System.out.println(a[i]);
9. }}
Output:
33
We can also print the Java array using for-each loop. The Java for-each loop prints the array elements
one by one. It holds an array element in a variable, then executes the body of the loop.
1. for(data_type variable:array){
3. }
Let's see the example of prin ng the elements of the Java array using the for-each loop.
AD
Testarray1.java
2. class Testarray1{
6. for(int i:arr)
7. System.out.println(i);
8. }}
Output:
33
AD
We can pass the Java array to the method so that we can reuse the same logic on any array. When
we pass an array to a method in Java, we are essen ally passing a reference to the array. It means
that the method will have access to the same array data as the calling code, and any modifica ons
made to the array within the method will affect the original array.
Let's see the simple example to get the minimum number of an array using a method.