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:
(2) for each loop, sample code 2:
for (int a : array) { System.out.println(a); }
Output result:
(3) Using Arrays toString method in the class, sample code 3:
System.out.println(array);// 这样打印的是数组的首地址。 System.out.println(Arrays.toString(array));
Output result:
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:
(2) for each loop , Sample code 2:
for (int[] a : arrays) { for (int b : a) { System.out.print(b + " "); } System.out.println();// 换行 }
Output result:
(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:
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!

Hot AI Tools

Undresser.AI Undress
AI-powered app for creating realistic nude photos

AI Clothes Remover
Online AI tool for removing clothes from photos.

Undress AI Tool
Undress images for free

Clothoff.io
AI clothes remover

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

Hot Article

Hot Tools

SublimeText3 Chinese version
Chinese version, very easy to use

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 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 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
Integrate Eclipse with SAP NetWeaver application server.
