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

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

王林
Release: 2019-11-23 16:40:23
Original
3576 people have browsed it

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]);
}
Copy after login

(2) for each loop

for(int a:array)
    System.out.println(a);
Copy after login

(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));
Copy after login

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!

source:php.cn
Statement of this Website
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
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template