How to Print an Array in Java

Method 1 of 3:

Using the toString Command

  1. How to Print an Array in Java Picture 1
    Setting the elements in your array. Enter String[] array = new String[] {"Elem1", "Elem2", "Elem3"} where "ElemX" are the individual elements in your array.
  2. How to Print an Array in Java Picture 2
    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]
  3. How to Print an Array in Java Picture 3
    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

  1. How to Print an Array in Java Picture 4
    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.
  2. How to Print an Array in Java Picture 5
    Use the standard library static method: Arrays.asList() for one dimensional arrays that you want to print as a list.
  3. How to Print an Array in Java Picture 6
    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

  1. How to Print an Array in Java Picture 7
    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 and for ( j = 0; j < columns; j++) for the columns.
  2. How to Print an Array in Java Picture 8
    Use the standard library static method: System.out.print(aryNumbers[i][j] + " " ); followed by System.out.println( "" ); to print arrays within arrays and multidimensional arrays as a line.[2]
  3. How to Print an Array in Java Picture 9
    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.
3.9 ★ | 7 Vote