Introduction to Print Array in Java
An Array is basically a data structure where we can store similar types of elements. For example, an array of integers stores multiple integers, an array of strings stores multiple strings, etc. So if you have an Array with a large amount of data, you might need to print those to view them at your convenience with Print Array in Java. There are several ways that we can follow to print an array in Java. You can follow any of those methods to print an array. For each of the methods of Print Array in Java I will be discussing here, I have given examples of code for better understanding and hands-on purpose. I have also added comments inside the codes for better readability. Moreover, I have given screenshots of the output of each code. Go through the codes line by line and understand those. Then write and run those codes on yourself in java compilers and match those outputs with the given one.
Techniques to Print Array in Java
Below are the Techniques to Print Array in Java:
Method 1: Using for loop
As we know, a loop is used to execute a set of statements repeatedly until a particular condition is fulfilled. We will use this functionality of for loop to print the array here.
Example: 1
Here we will create an array of four elements and will use for loop to fetch the values from the array and print them.
Code:
Output:
The above example is for the one-dimensional array.
Example: 2
For a two-dimensional array, you will have both rows and columns those need to be printed out. So you will need to run two for loops in a nested fashion. One for rows and inside it, the other for columns.
Code:
for ( k = 0; k< rows; k++)
for ( m = 0; m< columns; m++)
For print: System.out.print(arr[k][m] + " " )
Try it yourself and see the magic.
Method 2: Using the for-each loop
A for-each loop is also used to traverse over an array. As output, it will return elements one by one in the defined variable.
Example
We will create an array of four strings and iterate and print those using a for-each loop.
Code:
Output:
So far, we have used for and for-each sloops to print array. But from the next methods onwards, we will use classes related to the array under java. util packages which are specifically provided in java for the handling of arrays. We will use various static methods of those classes to deal with arrays. This will make our coding simple and hassle-free.
Let’s have a look at those ones by one.
Method 3: Using Java Arrays.toString()
The java.util.Arrays package has a static method Arrays.toString(). Hence, to use this static method, we need to import that package. Arrays.toString() accepts an array of any primitive type (for example, int, string) as its argument and returns the output as a string type.
Example: 1
This string type representation is a one-dimensional array. Hence, you can represent data in either rows or columns.
Code
Output:
Note the square brackets on the output. Square brackets denote the level of dimension. Thus, one pair (opening and closing pair) of the square bracket here denotes that the array is one dimensional.
- Limitation of Arrays.toString()
Example: 2
For arrays with dimension two or larger, we cannot use Arrays.toString() method. Below is one example code:
Code:
Output:
This is happening as the method does not do a deep conversion. It will only iterate on the first dimension and call the toString() method of each item. Hence we are getting undesired results. What is the solution, then? Let’s have a look at our next method.
Method 4: Using Arrays.deep string() method
For arrays of dimension two or more, we will use the static method Arrays.deepToString(), which belongs to java.util.Arrays package. This method will do a deep conversion into a string of an array. Here also, the dimension of the array will be represented as a representation of square brackets.
Example
Three-dimensional arrays
Code:
Output:
Note the square brackets representation. The square brackets are also 3 levels deep, which confirms the dimension of the array as three.
Method 5: Using Arrays.asList() method
The java.util.Arrays package has a static method Arrays.asList(). Hence, to use this static method, we need to import the package.
Example
Arrays.asList() accepts an array as its argument and returns the output as a list of an array.
Code:
Output:
Method 6: Using Iterator interface
The java.util.The iterator package has an interface Iterator. Hence, to use this interface for array printing, we need to import the package. We will create an Iterator object by calling the iterator() method. We will first convert the array into the list then invoke the iterator() method to create the collection. Then we will traverse through the collection using a while loop and print the values.
Example
As we need to convert the array into the list, we also need to use Arrays.asList() method and hence, also need to import java.util.Arrays.
Code:
Output:
Method 7: Using ArrayList( ) method
A normal array in Java is a static data structure because the initial size of the array is fixed. An ArrayList is a dynamic data structure where items can be added and removed from the list. So if you are not sure about how many elements will be there in your array, this dynamic data structure will save you. You need to import java.util.ArrayList package to use ArrayList() method to create ArrayList object. Once you have a new ArrayList object, you can add/remove elements to it with the add() /remove() method:
Example
Similar to Method 6. Here also, we will first convert the array into the list then invoke the iterator() method to create the collection. Then we will traverse through the collection using a while loop and print the values.
Code:
Output:
Method 8: Using Java Stream API
Example
We also can convert array to stream using the Arrays.stream() method. Then we iterate through the stream using foreach() and print them.
Code:
Output:
This concludes our learning for the topic “Print Array in Java”. Practice the examples by writing the codes mentioned in the above examples. Learning of codes will be incomplete if you will not do hands-on by yourself. Happy coding!!
Recommended Articles
This is a guide to Print Array in Java. Here we have discussed the basic concept, Techniques to Print Array in Java with different methods along with codes and outputs. You can also go through our other related articles to learn more –