Home  >  Article  >  Java  >  What are the methods to print one-dimensional array in java

What are the methods to print one-dimensional array in java

王林
王林Original
2019-11-23 16:40:233361browse

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

(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!

Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn