Home >Java >Javagetting Started >What are the methods to print one-dimensional array in java
1. Three ways to output arrays
One-dimensional array:
Define an array int[] array = {1,2, 3,4,5};
(1) Traditional for loop method
for(int i=0;i<array.length;i++) { System.out.println(a[i]); }
(2) for each loop
for(int a:array) System.out.println(a);
(3) Use the toString method in the Array class
to call Array.toString(a) to return a string containing array elements, which are placed within brackets. And separate them with commas
int[] array = {1,2,3,4,5}; System.out.println(Arrays.toString(array));
Output: [1, 2, 3, 4, 5]
Explanation: System.out.println(array); This is not possible, print like this Yes, it is the first address of the array.
Recommended tutorial: java introductory tutorial
The above is the detailed content of What are the methods to print one-dimensional array in java. For more information, please follow other related articles on the PHP Chinese website!