Printing array values in Java or values of 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 array then you
don't have much choice than for loop until Java 1.4. Things has 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 any array. Main difference between Arrays.toString() and Arrays.deepToString is that deepToString is used to
print values of 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 array in Java if you
directly pass primitive or object array to System.out.println() you will
receive 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
You can't decipher anything until you are quite familiar of this array
format and even then it doesn't tell anything about contents of array. It just
print type of element and hashcode.
In order to print values of array you can use any of following 3 examples:
1) Use enhanced for loop or classic for loop with lenth of 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
over all 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 other in Java. Enhanced for loop in Java
5 is added with other popular language feature 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 +", ");
}
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 there contents. By converting array into List we can
leverage that property and print values from ArrayList instead of
Array. Only limitation of this approach is it doesn't print contents of array
if 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 simple code example of displaying values form 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]
Print Array Value Example 3: using
Arrays.toString and Arrays.deepToString
This is by far best and recommended way of printing values from Array in
Java. Only caveat is that Arrays.toString() and Arrays.deepToString() are added
from Java 5 onwards along with other features e.g. 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 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]]
Java program to print
array values
Here is the combined examples of printing value of elements from Array in
Java using above three examples. 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
:"
+ Arrays.asList(strArray));
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: "
+ Arrays.toString(intArray));
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:
"
+ 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]]
That's all on how to print array values in Java. we have seen three
different example to display contents of array in Java and 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
Tidak ada komentar:
Posting Komentar