Printing array values in Java or values of an array element in Java would
have been much easier if arrays are
allowed to directly prints its values whenever used inside System.out.println() or format
and printf method, Similar to various classes in Java do this by overriding
toString() method. Despite being an object, array in Java doesn't print any
meaningful representation of its content when passed to System.out.println() or any
other print methods. If you are using array in method argument or any other prominent place in code and actually interested in values of an array then you
don't have much choice than for loop until Java 1.4.
Things have been changed since Java 5 because it introduced two extremely convenient methods for printing values of both primitive and object arrays in Java. Arrays.toString(array) and Arrays.deepToString(twoDimensionArray) can print values of an array.
The main difference between Arrays.toString() and Arrays.deepToString is that deepToString is used to print values of a multidimensional array which is far more convenient than nesting of multiple for loops. In this Java tutorial, we will see 3 different ways of printing array values in Java or value of element from Array in Java.
Things have been changed since Java 5 because it introduced two extremely convenient methods for printing values of both primitive and object arrays in Java. Arrays.toString(array) and Arrays.deepToString(twoDimensionArray) can print values of an array.
The main difference between Arrays.toString() and Arrays.deepToString is that deepToString is used to print values of a multidimensional array which is far more convenient than nesting of multiple for loops. In this Java tutorial, we will see 3 different ways of printing array values in Java or value of element from Array in Java.
3 ways to print array values in Java
As I said there is no direct way to print values of the array in Java if you
directly pass primitive or object array to System.out.println() you will
receive the following output:
System.out.println("Print array values in Java 1.4 :" + Arrays.asList(sArray));
System.out.println("Print array values in Java 1.4 :" + Arrays.asList(iArray));
Output:
Printing Integer array in Java: [I@15b7986
Printing String array in Java: [Ljava.lang.String;@87816d
System.out.println("Print array values in Java 1.4 :" + Arrays.asList(iArray));
Output:
Printing Integer array in Java: [I@15b7986
Printing String array in Java: [Ljava.lang.String;@87816d
You can't decipher anything until you are quite familiar with this array
format and even then it doesn't tell anything about the contents of an array. It just
prints the type of element and hashcode.
In order to print values of the array you can use any of the following 3 examples:
1) Use enhanced for loop or classic for loop with a length of the array.
3) Use Java 5 Arrays.toString() and Arrays.deepToString() methods
Print Array Value Example 1: Using for loop
for
loop is the classical way of printing or displaying values of both one
dimension and multidimensional arrays in Java. before Java 5 you can use array.length to iterate
overall array elements and printing values for each of them.
From Java 5 onwards you can use much cleaner enhanced for loop which doesn't require any counter from moving one element to another in Java.
Enhanced for loop in Java 5 is added with other popular language features e.g. Enum, Autoboxing and Generics. Here is sample code example to print value of element from array using classical and enhanced for loop in Java:
From Java 5 onwards you can use much cleaner enhanced for loop which doesn't require any counter from moving one element to another in Java.
Enhanced for loop in Java 5 is added with other popular language features e.g. Enum, Autoboxing and Generics. Here is sample code example to print value of element from array using classical and enhanced for loop in Java:
// Classic for loop before Java 5
private static
int[]
iArray = new int[]{1, 2,3,4, 5};
for(int i=0; i< iArray.length; i++){
System.out.print(iArray[i] +", ");
}
Output:
1, 2, 3, 4, 5,
//Enhanced for loop from Java 1.5
for(int i : iArray){
System.out.print(i +", ");
}
for(int i=0; i< iArray.length; i++){
System.out.print(iArray[i] +", ");
}
Output:
1, 2, 3, 4, 5,
//Enhanced for loop from Java 1.5
for(int i : iArray){
System.out.print(i +", ");
}
As you see using enhanced for loop for printing array values is more
concise and clean.
Print Array Values Example 2: Using Arrays.asList
Arrays.asList() method is used to convert
Array into ArrayList and as you know Collection classes
overrides toString
method to print their contents. By converting an array into List we can
leverage that property and print values from ArrayList instead of
Array.
The only limitation of this approach is it doesn't print the contents of an array if an array is of primitive type like int, float or double but works well if Array contains objects like String. Arrays.asList() is also used to create and initialize List in one line.
By the way here is a simple code example of displaying values from an array in Java using Arrays.asList() method:
The only limitation of this approach is it doesn't print the contents of an array if an array is of primitive type like int, float or double but works well if Array contains objects like String. Arrays.asList() is also used to create and initialize List in one line.
By the way here is a simple code example of displaying values from an array in Java using Arrays.asList() method:
System.out.println("Print String array values in Java 1.4 :" +
Arrays.asList(sArray));
System.out.println("Print int array values in Java 1.4 :" + Arrays.asList(iArray));
Output:
Print String array values in Java 1.4 :[abc, bcd, def, efg]
Print int array values in Java 1.4 :[[I@15b7986]
System.out.println("Print int array values in Java 1.4 :" + Arrays.asList(iArray));
Output:
Print String array values in Java 1.4 :[abc, bcd, def, efg]
Print int array values in Java 1.4 :[[I@15b7986]
Print Array Value Example 3: using Arrays.toString and Arrays.deepToString
This is by far the best and recommended way of printing values from Array in
Java. The only caveat is that Arrays.toString() and Arrays.deepToString() are added
from Java 5 onwards along with other features like Generics,
varargs
or static import.
Use Arrays.toString() method to print both primitive and object single or one dimension array and use Arrays.deepToString() method to print values from two dimensional or multidimensional array (array of the array in Java). Here is a simple example of printing array values using Arrays.toString() and Arrays.deepToString() in Java:
Use Arrays.toString() method to print both primitive and object single or one dimension array and use Arrays.deepToString() method to print values from two dimensional or multidimensional array (array of the array in Java). Here is a simple example of printing array values using Arrays.toString() and Arrays.deepToString() in Java:
System.out.println("Print values of Integer array in Java: " +
Arrays.toString(iArray));
System.out.println("Print values of String array in Java: " + Arrays.toString(sArray));
int[][] twoDimensionArray = new int[][]{
{1,2,3},
{10,20,30},
{100,200,300},
};
System.out.println("Print two dimensional array in Java: " + Arrays.deepToString(twoDimensionArray));
Output:
Print values of Integer array in Java: [1, 2, 3, 4, 5]
Print values of String array in Java: [abc, bcd, def, efg]
Print two dimensional array in Java: [[1, 2, 3], [10, 20, 30], [100, 200, 300]]
System.out.println("Print values of String array in Java: " + Arrays.toString(sArray));
int[][] twoDimensionArray = new int[][]{
{1,2,3},
{10,20,30},
{100,200,300},
};
System.out.println("Print two dimensional array in Java: " + Arrays.deepToString(twoDimensionArray));
Output:
Print values of Integer array in Java: [1, 2, 3, 4, 5]
Print values of String array in Java: [abc, bcd, def, efg]
Print two dimensional array in Java: [[1, 2, 3], [10, 20, 30], [100, 200, 300]]
Java program to print array values
Here are the combined examples of printing value of elements from Array in
Java using the above three examples. The best way to print elements of Array is to use
new methods added in java.util.Arrays class in Java 5 e.g. toString() and deepToString().
import java.util.Arrays;
public class PrintArrayExample {
private static int[] intArray = new int[]{1, 2,3,4, 5};
private static String[] strArray = new String[]{"abc", "bcd", "def", "efg"};
public static void main(String args[]) {
System.out.println("Java Example to print int array in Java: " + intArray);
System.out.println("Java Example to print string array in Java: " + strArray);
//generic way of printing values of array before java 5
for(int i=0; i< intArray.length; i++){
System.out.print(intArray[i] +", ");
}
//printing array values using enhanced for loop java 1.5
for(int i : intArray){
System.out.print(i +", ");
}
//another way to print array values in Java 1.4 is using Arrays.asList
System.out.println("Java Example to print String array values in Java 1.4 :"
public class PrintArrayExample {
private static int[] intArray = new int[]{1, 2,3,4, 5};
private static String[] strArray = new String[]{"abc", "bcd", "def", "efg"};
public static void main(String args[]) {
System.out.println("Java Example to print int array in Java: " + intArray);
System.out.println("Java Example to print string array in Java: " + strArray);
//generic way of printing values of array before java 5
for(int i=0; i< intArray.length; i++){
System.out.print(intArray[i] +", ");
}
//printing array values using enhanced for loop java 1.5
for(int i : intArray){
System.out.print(i +", ");
}
//another way to print array values in Java 1.4 is using Arrays.asList
System.out.println("Java Example to print String array values in Java 1.4 :"
+ Arrays.asList(strArray));
System.out.println("Java Example to int array values in Java 1.4 :"
System.out.println("Java Example to int array values in Java 1.4 :"
+ Arrays.asList(intArray));
//better way of printing values of array in java 1.5
System.out.println("Java Example to print values of array in Java: "
//better way of printing values of array in java 1.5
System.out.println("Java Example to print values of array in Java: "
+ Arrays.toString(intArray));
System.out.println("Java Example to print values of array in Java: "
System.out.println("Java Example to print values of array in Java: "
+ Arrays.toString(strArray));
int[][] twoDimensionArray = new int[][]{
{1,2,3},
{10,20,30},
{100,200,300},
};
System.out.println("Java Example to print two dimensional array in Java: "
int[][] twoDimensionArray = new int[][]{
{1,2,3},
{10,20,30},
{100,200,300},
};
System.out.println("Java Example to print two dimensional array in Java: "
+ Arrays.deepToString(twoDimensionArray));
}
}
Output:
Java Example to print int array in Java: [I@1820dda
Java Example to print string array in Java: [Ljava.lang.String;@15b7986
1, 2, 3, 4, 5, 1, 2, 3, 4, 5, Java Example to print String array values in Java 1.4 :[abc, bcd, def, efg]
Java Example to int array values in Java 1.4 :[[I@1820dda]
Java Example to print values of array in Java: [1, 2, 3, 4, 5]
Java Example to print values of array in Java: [abc, bcd, def, efg]
Java Example to print two dimensional array in Java: [[1, 2, 3], [10, 20, 30], [100, 200, 300]]
}
}
Output:
Java Example to print int array in Java: [I@1820dda
Java Example to print string array in Java: [Ljava.lang.String;@15b7986
1, 2, 3, 4, 5, 1, 2, 3, 4, 5, Java Example to print String array values in Java 1.4 :[abc, bcd, def, efg]
Java Example to int array values in Java 1.4 :[[I@1820dda]
Java Example to print values of array in Java: [1, 2, 3, 4, 5]
Java Example to print values of array in Java: [abc, bcd, def, efg]
Java Example to print two dimensional array in Java: [[1, 2, 3], [10, 20, 30], [100, 200, 300]]
That's all on how to print array values in Java. we have seen three
different examples to display the contents of the array in Java and the most easy and convenient approach is using Arrays.toString() and Arrays.deepToString(). if you are
using Java 5.
Related Java Collection tutorials from Javarevisited Blog
I was looking for any easy to print array in Java, didn't know about toString() method of Arrays class. This post gives me lot of insight on How to print array in Java but same time I think Why not use ArrayList in place of array?
ReplyDeleteHow to print primitive array in Java 1.4, As I see toString and deppToString of Arrays class is only available post Java 5.
ReplyDeleteHi, is there anyone who can help me doing a java programming task?
ReplyDeletecurrently I'm trying to create a task named Tarski's World...
It's a program where you make row and column ( 10x10 ) and then inside the empty array spaces it will generate type of objects with attributes colours and shapes of the objects randomly. ( such as; blue square, red square, red rectangle, green rectangle) .
The generated object n colour will fill all the columns and rows...
I had tried few coding that referring to the internet... but none of it is working properly... need help from you guys...
thanks...
how to this value into array[1[2]]?
ReplyDeletenot bad. helped with my project. 9/10
ReplyDeleteThanks.. this is way more cooler in my java journey!
ReplyDelete