Home >Java >javaTutorial >How to output an array in java
Three ways to output arrays in Java.
Define an int type array for output
int[] array={1,2,3,4,5};
The first way, the traditional for loop way
for(int i=0;i<array.length;i++) { System.out.println(a[i]); }
The second way, for each loop
for(int a:array){ System.out.println(a); }
The third way, use the toString method in the Array class
Call Array.toString(a) to return a string containing array elements, which are placed within brackets and separated by commas
int[] array = {1,2,3,4,5}; System.out.println(Arrays.toString(array));
Description : System.out.println(array); This is not possible. What is printed is the first address of the array.
The above is the detailed content of How to output an array in java. For more information, please follow other related articles on the PHP Chinese website!