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
package com.javatutorialhq.tutorial; /** * This java sample source code that shows * how to print an array in java using advance for loop * Property of javatutorialhq.com * Feel free to modify for your own personal use * All Rights Reserved * Version 1.0 * 08/04/2013 */ public class ArrayBasicLoopString { public static void main(String[] args) { String[]arrayNames = new String[]{"andrew","juan","william","carnegie","will","anne"}; System.out.println("Direct access value :"+arrayNames[0]); System.out.println("Length of the array:"+arrayNames.length); for(int index=0;index<arrayNames.length;index++){ System.out.println("Value at index "+index +" is "+arrayNames[index]); } } }
Sample Output:
Direct access value :andrew Length of the array:6 Value at index 0 is andrew Value at index 1 is juan Value at index 2 is william Value at index 3 is carnegie Value at index 4 is will Value at index 5 is anne
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.
package com.javatutorialhq.tutorial; public class ArrayForEachString { public static void main(String[] args) { Integer[] age = new Integer[]{55,65,68,10,35}; for(Integer value: age){ System.out.println(value); } } }
Sample Output:
55 65 68 10 35
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.
package com.javatutorialhq.tutorial; import java.util.Arrays; public class ArrayToString { public static void main(String[] args) { Float[] randNumber = new Float[6]; randNumber[0]=1.32f; randNumber[1]=0.65f; randNumber[2]=32.102f; randNumber[4]=12.43f; randNumber[5]=13f; System.out.println("Example on how to print an array in java"); System.out.println(Arrays.toString(randNumber)); } }
Sample Output:
Example on how to print an array in java [1.32, 0.65, 32.102, null, 12.43, 13.0]
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.
package com.javatutorialhq.tutorial; public class MultiDimensionArrayLoop { public static void main(String[] args) { // assigning a character array with the use of toCharArray() method char[][] charAray = new char[][]{{'o','n','e'},{'t','w','o'},"three".toCharArray()}; //printing a multi dimensional array in java using for loop for(int x =0;x<charAray.length;x++){ for(int y=0;y<charAray[x].length;y++){ System.out.println("char["+x+"]["+y+"] = "+charAray[x][y]); } } } }
Sample Output
char[0][0] = o char[0][1] = n char[0][2] = e char[1][0] = t char[1][1] = w char[1][2] = o char[2][0] = t char[2][1] = h char[2][2] = r char[2][3] = e char[2][4] = e
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.
package com.javatutorialhq.tutorial; /** * Code how to print an array in java * Multi dimensional array using for each loop */ public class MultiArrayForEach { public static void main(String[] args) { String[][] intArray = new String[][]{{"one"},{"two","three"},{"four"}}; // Print all elements of a multi-dimensional array for(String[]firstLoop:intArray){ for(String s: firstLoop){ System.out.println(s); } } } }
Sample Output:
one two three four
Multi dimensional Array – Print an array in java : Using deepToString()
package com.javatutorialhq.tutorial; import java.util.Arrays; /** * print an array in java using deepToString() * multi dimensional array example */ public class DeepPrintArray { public static void main(String[] args) { //multi dimensional array declaration char[][] name = new char[][] { "william".toCharArray(), "agnes".toCharArray(), "theodore".toCharArray() }; //Print the contents of a multi dimensional array System.out.println(Arrays.deepToString(name)); } }
Sample Output:
[[w, i, l, l, i, a, m], [a, g, n, e, s], [t, h, e, o, d, o, r, e]]