Home > Java > Javagetting Started > body text

How to optimize the output form of arrays in java

王林
Release: 2020-01-03 17:02:19
forward
2330 people have browsed it

How to optimize the output form of arrays in java

One-dimensional array:

Define an array:

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

(1) Traditional for loop method, sample code 1:

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

(Free learning video tutorial sharing: java video tutorial)

Output result:

How to optimize the output form of arrays in java

(2) for each loop, sample code 2:

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

Output result:

How to optimize the output form of arrays in java

(3) Using Arrays toString method in the class, sample code 3:

System.out.println(array);// 这样打印的是数组的首地址。
System.out.println(Arrays.toString(array));
Copy after login

Output result:

How to optimize the output form of arrays in java

Two-dimensional array:

These three methods also correspond to two-dimensional arrays. Define a two-dimensional array:

int[][] arrays = { { 16, 3, 2, 13 }, { 5, 10, 11, 8 }, { 9, 6, 7, 3 } };
Copy after login

Java actually does not have multi-dimensional arrays, only one-dimensional arrays. Multi-dimensional arrays are interpreted as "Array of arrays", for example, the two-dimensional array arrays is a one-dimensional array containing three elements {arrays[0], arrays[1], arrays[2]}, arrays[0] is a one-dimensional array containing {16,3,2, 13} A one-dimensional array of four elements, the same goes for arrays[1] and arrays[2].

(1) Traditional for loop method, sample code 1:

for (int i = 0; i < arrays.length; i++) {
for (int j = 0; j < arrays[i].length; j++) {
System.out.print(arrays[i][j] + " ");
}
System.out.println(); // 换行
}
Copy after login

Output result:

How to optimize the output form of arrays in java

(2) for each loop , Sample code 2:

for (int[] a : arrays) {
for (int b : a) {
System.out.print(b + " ");
}
System.out.println();// 换行
}
Copy after login

Output result:

How to optimize the output form of arrays in java

(3) Use the toString method in the Arrays class, Sample code 3:

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

Output results:

How to optimize the output form of arrays in java

Recommended related articles and tutorials: java introductory tutorial

The above is the detailed content of How to optimize the output form of arrays in java. For more information, please follow other related articles on the PHP Chinese website!

Related labels:
source:csdn.net
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