How to Print an Array in Java
If you are working on Java and have an array with a large amount of data, you may want to print certain elements in order to view them conveniently. There are multiple ways you can print arrays in Java and the examples given below will...
Method 1 of 3:
Using the toString Command
- Setting the elements in your array. Enter
String[] array = new String[] {"Elem1", "Elem2", "Elem3"}
where "ElemX" are the individual elements in your array. - Use the standard library static method:
Arrays.toString(array)
. This will give you a string representation of one dimensional arrays. In other words, because it is one dimensional, you can present the data in either rows or columns. This method will print the data in a row, or string.[1] - Run the program. Different compilers have different ways to accomplish this task. You may be able to go to "File" and then "Run." You may also have the option to simply click the "Run" icon in your toolbar. Your elements will be printed out in a string in the lower window of Java.
Method 2 of 3:
Using the asList Command
- Setting the elements in your array. Enter
String[] array = new String[] {"Elem1", "Elem2", "Elem3"}
where "ElemX" are the individual elements you want in your array. - Use the standard library static method:
Arrays.asList()
for one dimensional arrays that you want to print as a list. - Run the program. Different compilers have different ways to accomplish this task. You may be able to go to "File" and then "Run." You may also have the option to simply click the "Run" icon in your toolbar. Your elements will be printed out in a list or column in the lower window of Java.
Method 3 of 3:
Printing Multidimensional Arrays
- Setting the elements in your array. For a two-dimensional array, you will have both rows and columns that need to be printed out. Enter
for ( i = 0; i < rows; i++)
for the rows andfor ( j = 0; j < columns; j++)
for the columns. - Use the standard library static method:
System.out.print(aryNumbers[i][j] + " " );
followed bySystem.out.println( "" );
to print arrays within arrays and multidimensional arrays as a line.[2] - Run the program. Different compilers have different ways to accomplish this task. You may be able to go to "File" and then "Run." You may also have the option to simply click the "Run" icon in your toolbar. Your elements will be printed out in a line or column in the lower window of Java.
Update 05 March 2020
You should read it
- Array data structure
- Array in Python
- Array in Language C
- Array (Array) in PHP
- Array (Array) in C #
- Array (Array) in C / C ++
- Introduction to 2D Array - 2-dimensional array in JavaScript
- Cursor and Array in C ++
- Array (Array) in JavaScript
- Array of pointers in C ++
- Learn about Collection of Record in JavaScript
- Overload subscript operator [] in C ++
Maybe you are interested
The printer cannot display default settings and what you need to do How to Add or Change the Default Port on Linux Bloomberg: The Covid-19 epidemic could cause most airlines to go bankrupt until May How to Make Adobe Acrobat Reader the Default PDF Viewer on PC or Mac How to Enable AutoRecover in Microsoft Office 2010 How to Set Default Programs in Windows 8