Java - Comment imprimer un tableau
Dans cet article, nous allons vous montrer quelques façons d'imprimer un tableau Java.
1. Arrays.toString
C'est le moyen le plus simple d'imprimer un tableau -Arrays.toString (depuis JDK 1.5)
PrintArray.java
package com.example.utils.print;
import java.util.Arrays;
public class PrintArray {
public static void main(String[] args) {
// array
String[] arrayStr = new String[]{"Java", "Node", "Python", "Ruby"};
System.out.println(Arrays.toString(arrayStr));
// Output : [Java, Node, Python, Ruby]
int[] arrayInt = {1, 3, 5, 7, 9};
System.out.println(Arrays.toString(arrayInt));
// Output : [1, 3, 5, 7, 9]
// 2d array, need Arrays.deepToString
String[][] deepArrayStr = new String[][]{{"example1", "example2"}, {"example3", "example4"}};
System.out.println(Arrays.toString(deepArrayStr));
// Output : [[Ljava.lang.String;@23fc625e, [Ljava.lang.String;@3f99bd52]
System.out.println(Arrays.deepToString(deepArrayStr));
// Output : [[example1, example2], [example3, example4]
int[][] deepArrayInt = new int[][]{{1, 3, 5, 7, 9}, {2, 4, 6, 8, 10}};
System.out.println(Arrays.toString(deepArrayInt));
// Output : [[I@3a71f4dd, [I@7adf9f5f]
System.out.println(Arrays.deepToString(deepArrayInt));
// Output : [[1, 3, 5, 7, 9], [2, 4, 6, 8, 10]]
}
}
Sortie
[Java, Node, Python, Ruby] [1, 3, 5, 7, 9] [[Ljava.lang.String;@23fc625e, [Ljava.lang.String;@3f99bd52] [[example1, example2], [example3, example4]] [[I@3a71f4dd, [I@7adf9f5f] [[1, 3, 5, 7, 9], [2, 4, 6, 8, 10]]
2. Flux JDK 8
Dans JDK 8, nous pouvons le convertir enStream et l'imprimer.
PrintArray.java
package com.example.utils.print;
import java.util.Arrays;
public class PrintArray {
public static void main(String[] args) {
// array
String[] arrayStr = new String[]{"Java", "Node", "Python", "Ruby"};
Arrays.stream(arrayStr).forEach(System.out::println);
int[] arrayInt = {1, 3, 5, 7, 9};
Arrays.stream(arrayInt).forEach(System.out::println);
//2d array
String[][] deepArrayStr = new String[][]{{"example1", "example2"}, {"example3", "example4"}};
Arrays.stream(deepArrayStr).flatMap(x -> Arrays.stream(x)).forEach(System.out::println);
int[][] deepArrayInt = new int[][]{{1, 3, 5, 7, 9}, {2, 4, 6, 8, 10}};
Arrays.stream(deepArrayInt).flatMapToInt(x -> Arrays.stream(x)).forEach(System.out::println);
}
}
Sortie
Java Node Python Ruby 1 3 5 7 9 example1 example2 example3 example4 1 3 5 7 9 2 4 6 8 10
3. Json n'importe quoi
C'est ma méthode préférée, utilisez la bibliothèqueJackson2 pour imprimer quoi que ce soit dans une chaîne au format JSON.
PrintUtils.java
package com.example.utils.print;
import com.fasterxml.jackson.core.JsonProcessingException;
import com.fasterxml.jackson.databind.ObjectMapper;
public class PrintUtils {
private static final ObjectMapper om = new ObjectMapper();
public static void print(Object object) {
String result;
try {
result = om.writerWithDefaultPrettyPrinter().writeValueAsString(object);
System.out.println(result);
} catch (JsonProcessingException e) {
e.printStackTrace();
}
}
}
PrintArray.java
package com.example.utils.print;
public class PrintArray {
public static void main(String[] args) {
//array
String[] arrayStr = new String[]{"Java", "Node", "Python", "Ruby"};
PrintUtils.print(arrayStr);
int[] arrayInt = {1, 3, 5, 7, 9};
PrintUtils.print(arrayInt);
//2d array
String[][] deepArrayStr = new String[][]{{"example1", "example2"}, {"example3", "example4"}};
PrintUtils.print(deepArrayStr);
int[][] deepArrayInt = new int[][]{{1, 3, 5, 7, 9}, {2, 4, 6, 8, 10}};
PrintUtils.print(deepArrayInt);
}
}
Sortie
[ "Java", "Node", "Python", "Ruby" ] [ 1, 3, 5, 7, 9 ] [ [ "example1", "example2" ], [ "example3", "example4" ] ] [ [ 1, 3, 5, 7, 9 ], [ 2, 4, 6, 8, 10 ] ]