search
HomeJavaJavagetting StartedHow to optimize the output form of arrays in java

How to optimize the output form of arrays in java

Jan 03, 2020 pm 05:02 PM
javaoptimizationarray

How to optimize the output form of arrays in java

One-dimensional array:

Define an array:

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

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

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

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

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

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

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(); // 换行
}

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();// 换行
}

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

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!

Statement
This article is reproduced at:CSDN. If there is any infringement, please contact admin@php.cn delete

Hot AI Tools

Undresser.AI Undress

Undresser.AI Undress

AI-powered app for creating realistic nude photos

AI Clothes Remover

AI Clothes Remover

Online AI tool for removing clothes from photos.

Undress AI Tool

Undress AI Tool

Undress images for free

Clothoff.io

Clothoff.io

AI clothes remover

Video Face Swap

Video Face Swap

Swap faces in any video effortlessly with our completely free AI face swap tool!

Hot Tools

SublimeText3 Chinese version

SublimeText3 Chinese version

Chinese version, very easy to use

MinGW - Minimalist GNU for Windows

MinGW - Minimalist GNU for Windows

This project is in the process of being migrated to osdn.net/projects/mingw, you can continue to follow us there. MinGW: A native Windows port of the GNU Compiler Collection (GCC), freely distributable import libraries and header files for building native Windows applications; includes extensions to the MSVC runtime to support C99 functionality. All MinGW software can run on 64-bit Windows platforms.

Safe Exam Browser

Safe Exam Browser

Safe Exam Browser is a secure browser environment for taking online exams securely. This software turns any computer into a secure workstation. It controls access to any utility and prevents students from using unauthorized resources.

SecLists

SecLists

SecLists is the ultimate security tester's companion. It is a collection of various types of lists that are frequently used during security assessments, all in one place. SecLists helps make security testing more efficient and productive by conveniently providing all the lists a security tester might need. List types include usernames, passwords, URLs, fuzzing payloads, sensitive data patterns, web shells, and more. The tester can simply pull this repository onto a new test machine and he will have access to every type of list he needs.

SAP NetWeaver Server Adapter for Eclipse

SAP NetWeaver Server Adapter for Eclipse

Integrate Eclipse with SAP NetWeaver application server.