How to print an array in java with example

HQ ┬╗ Java Tutorial ┬╗ Example Source Code ┬╗ Java Array Examples ┬╗ How to print an array in java with example
How to print an array in java with example 2018-02-16T07:27:55+00:00

This tutorial will show a source code on how to print an array in java. As mentioned in my java array tutorial post, elements of an array can be accessed through the array index on the format arrayName

[index]. The first element as also previously mentioned is starting at index 0 thus the last index of an array is size of an array – 1. On below source codes you would notice that we will apply these information.

Print an array in java : String Elements

The source code below is for printing all string elements of an array using basic for loop. The code is also showing how to access an array by providing an index. The code provided were also showing on practical usage of the array’s length

Sample Output:

Java Advance For loop : Print an array of Integers

Below is an example on how to print the elements of an array in java using advance for loop (for – each). As you will notice the datatype is different on this example, instead of using an array of Strings we would be using array of Integers.

Sample Output:

Print an array in java : Using Arrays.toString()

The use of static method toString() of Arrays class will print the string representation of objects in an array. Moreover use of this method is only on the sole purpose of printing the contents of an array useful only in debugging. However if you are required to do complicated manipulation of the data on your arrays thus this method is not for you instead use the two methods above.

It would be wise to watch on the direct assignment of values and the use of float on below example. You would notice that if we don’t assign any element to an array index, it would be defaulted to null.

Sample Output:

Multi dimensional Array – Print an array in java : Using for loop

Printing the contents of a multi dimensional array is a bit complicated since it would require multiple for loops. Our example in printing a multi dimensional array would only focus only on two dimensions. Once you get the logic on how to do this, working on more dimensions would be a piece of cake.

Note that we use one interesting method in assigning the value of of character array which is the toCharArray(). This method returns the character representation in array format of a String.

Sample Output

Multi dimensional Array – Print an array in java : Using advance for loop

This scenario would be easy using advance for loop. It would only require our deep understanding on how the for each loop works. Iterating on the first Dimension and then iterate again on the next dimension. And as soon as we are on the last loop we just have to print the contents of the array.

Sample Output:

Multi dimensional Array – Print an array in java : Using deepToString()

Sample Output:

Contact Info


Our Goal

JavaTutorialHQ aims to to be The Ultimate Guide on Java with hundreds of examples from basic to advance Topics.

By continuing to use the site, you agree to the use of cookies. more information

The cookie settings on this website are set to "allow cookies" to give you the best browsing experience possible. If you continue to use this website without changing your cookie settings or you click "Accept" below then you are consenting to this.