How to Print an Array in Java
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.
3.9 ★ | 7 Vote
You should read it
May be interested
- eQuiz - Multiple choice quiz about JAVAfollowing are multiple-choice questions related to java programming language, there will be 23 questions in total with no answer for each time. let's get started!
- Download and install Java on the computerto program java on your computer, you need to install the java environment, this tutorial will show you how to download and install java on your computer.
- Basic Java exercises, with sample decodingto serve your java learning needs, tipsmake.com has synthesized some java exercises from many sources, including sample code (for some articles). hopefully it can be helpful to learn your java programming language.
- Which career Java programming options are waiting for you?java programmers are experts in the java programming language. as of 2018, there are many job opportunities for java programmers. with an expected growth rate of 19% for the period 2014-2024, the career prospects for the java language are really bright.
- How to install Java on a Raspberry Pithere are two different java implementations, oracle java and openjdk. this tutorial explains how to install java (openjdk) on a raspberry pi with the latest raspbian operating system running on it.
- How to fix the error does not install Javamore and more applications and websites require java installation before use. unfortunately, sometimes you can't install java or install it, but it doesn't work. the article will provide ways to fix errors that do not install java on your computer.
- How to Tune a Java Virtual Machine (JVM)the java virtual machine (jvm) runs your java programs. sometimes the default configuration that the jvm comes with may not be the most efficient for your program.
- What is the difference between Go and Java?is golang better than java? is golang harder than java? can golang replace java? this comparison will give you the answer.
- How to Fix Javajava is a computing platform that allows you to play games and view videos on your computer. you can tell that your computer is having problems with java if you see java errors appear when you try to run a program or visit a website that...
- How to Compile a Java Programthis wikihow teaches you how to turn your java source code into an executable app using a local and online compiler. if you're using a computer, the most common way to compile java code is using java software development kit (java sdk)...