Home > Java > javaTutorial > body text

How to output an array in java

(*-*)浩
Release: 2019-05-31 10:07:07
Original
35495 people have browsed it

Three ways to output arrays in Java.

How to output an array in java

Define an int type array for output

int[] array={1,2,3,4,5};
Copy after login

The first way, the traditional for loop way

for(int i=0;i<array.length;i++)
{
    System.out.println(a[i]);
}
Copy after login

The second way, for each loop

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

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

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!

Related labels:
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