Home >Java >javaTutorial >How to output an array in java

How to output an array in java

(*-*)浩
(*-*)浩Original
2019-05-31 10:07:0735538browse

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};

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!

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