This article was co-authored by wikiHow Staff. Our trained team of editors and researchers validate articles for accuracy and comprehensiveness. wikiHow's Content Management Team carefully monitors the work from our editorial staff to ensure that each article is backed by trusted research and meets our high quality standards.
The wikiHow Tech Team also followed the article's instructions and verified that they work.
This article has been viewed 417,186 times.
Learn more...
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 walk you through the process. Assume the name of the array to be printed is "array" and the elements you are seeking to print are named "Elem."
Steps
Using the toString Command
-
1Setting the elements in your array. Enter
String[] array = new String[] {"Elem1", "Elem2", "Elem3"}
where "ElemX" are the individual elements in your array. -
2Use 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]Advertisement -
3Run 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.
Using the asList Command
-
1Setting the elements in your array. Enter
String[] array = new String[] {"Elem1", "Elem2", "Elem3"}
where "ElemX" are the individual elements you want in your array. -
2Use the standard library static method:
Arrays.asList()
for one dimensional arrays that you want to print as a list. -
3Run 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.
Printing Multidimensional Arrays
-
1Setting 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. -
2Use 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] -
3Run 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.
Community Q&A
-
QuestionHow can I learn Java easily?Community AnswerUse online courses like from Code Academy or Udacity.
-
QuestionCan you put string row and column headers in front of int array list output?Marrium SharifCommunity AnswerWith pandas.DataFrame.to_csv you can write the columns and the index to a file.