What's the simplest way to print a Java array?
Since Java 5 you can use Arrays.toString(arr)
or Arrays.deepToString(arr)
for arrays within arrays. Note that the Object[]
version calls .toString()
on each object in the array. The output is even decorated in the exact way you're asking.
Examples:
Simple Array:
String[] array = new String[] {"John", "Mary", "Bob"};
System.out.println(Arrays.toString(array));Output:
[John, Mary, Bob]
Nested Array:
String[][] deepArray = new String[][] {{"John", "Mary"}, {"Alice", "Bob"}};
System.out.println(Arrays.toString(deepArray));
//output: [[Ljava.lang.String;@106d69c, [Ljava.lang.String;@52e922]
System.out.println(Arrays.deepToString(deepArray));Output:
[[John, Mary], [Alice, Bob]]
double
Array:double[] doubleArray = { 7.0, 9.0, 5.0, 1.0, 3.0 };
System.out.println(Arrays.toString(doubleArray));Output:
[7.0, 9.0, 5.0, 1.0, 3.0 ]
int
Array:int[] intArray = { 7, 9, 5, 1, 3 };
System.out.println(Arrays.toString(intArray));Output:
[7, 9, 5, 1, 3 ]
What's the simplest way to print a Java array?
Since Java 5 you can use Arrays.toString(arr)
or Arrays.deepToString(arr)
for arrays within arrays. Note that the Object[]
version calls .toString()
on each object in the array. The output is even decorated in the exact way you're asking.
Examples:
Simple Array:
String[] array = new String[] {"John", "Mary", "Bob"};
System.out.println(Arrays.toString(array));Output:
[John, Mary, Bob]
Nested Array:
String[][] deepArray = new String[][] {{"John", "Mary"}, {"Alice", "Bob"}};
System.out.println(Arrays.toString(deepArray));
//output: [[Ljava.lang.String;@106d69c, [Ljava.lang.String;@52e922]
System.out.println(Arrays.deepToString(deepArray));Output:
[[John, Mary], [Alice, Bob]]
double
Array:double[] doubleArray = { 7.0, 9.0, 5.0, 1.0, 3.0 };
System.out.println(Arrays.toString(doubleArray));Output:
[7.0, 9.0, 5.0, 1.0, 3.0 ]
int
Array:int[] intArray = { 7, 9, 5, 1, 3 };
System.out.println(Arrays.toString(intArray));Output:
[7, 9, 5, 1, 3 ]
Printing all elements of an array in one println statement in Java
That's the default implementation of toString()
in Object
you're seeing. You can use Arrays.toString
for a readable result (make sure to import java.util.Arrays
):
System.out.println(Arrays.toString(a));
How can I print an array easily?
You could use: Arrays.toString(arr)
for normal arrays and/or Arrays.deepToString(arr)
for arrays within arrays.
Both these methods return the string representation of the array.
See the Arrays docs for more.
How to print Array smart in java?
If you really want to customize your printing for variable arrays then why not do the following:
public static String s(Object s) {
return s.toString();
}
public static String s(Object[] s) {
return Arrays.toString(s);
}
public static String s(Object[][] s) {
return Arrays.deepToString(s);
}
// For each primitive type of your single or multi dimension
// declare the following for either single or multi dimensional
public static String s(int[][] s) {
return Arrays.deepToString(s);
}
public static String s(int[] s) {
return Arrays.toString(s);
}
But keep in mind you are basically repackaging what the API already does.
how do I print my array using enhanced loops
Your code contains a few mistakes, and major are:
Fib[2] = Fib[0] + Fib[1];
this should be Fib[i] = Fib[i - 1] + Fib[i - 2];
Another mistake is nesting for loops. The enhanced for loop should be after the loop with index i
.
int[] Fib = new int[20];
Fib[0] = 0;
Fib[1] = 1;
for (int i = 2; i < Fib.length; i++) {
Fib[i] = Fib[i - 1] + Fib[i - 2];
}
for (int number : Fib) {
System.out.print(" " + number);
}
You can use Arrays.toString(Fib)
instead of using a loop for printing the array.
Related Topics
Java.Lang.Classnotfoundexception: Didn't Find Class on Path: Dexpathlist
Why Does My Function That Calls an API or Launches a Coroutine Return an Empty or Null Value
How to Save the Current Date/Time When I Add New Value to Firebase Realtime Database
Working Post Multipart Request With Volley and Without Httpentity
How to Create Android Facebook Key Hash
Limit Jvm Process Memory on Ubuntu
Why Am I Getting a Noclassdeffounderror in Java
Reading a Plain Text File in Java
Including All the Jars in a Directory Within the Java Classpath
Converting Json Data to Java Object
How to Simulate the C++ 'Friend' Concept in Java
How to Return a Documentsnapshot as a Result of a Method
Singletons Vs. Application Context in Android
How to Implement Custom Action Bar With Custom Buttons in Android
Why Maven Uses Jdk 1.6 But My Java -Version Is 1.7