Ce paramètre#39;s la manière la plus simple d'imprimer un tableau Java

Mots clés : javaarraysprintingjava

meilleur 5 Réponses Ce paramètre#39;s la manière la plus simple d'imprimer un tableau Java

vote vote

97

String[] array = new String[] {"John", "Mary", "Bob"}; System.out.println(Arrays.toString(array)); 
[John, Mary, Bob] 
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)); 
[[John, Mary], [Alice, Bob]] 
double[] doubleArray = { 7.0, 9.0, 5.0, 1.0, 3.0 }; System.out.println(Arrays.toString(doubleArray)); 
[7.0, 9.0, 5.0, 1.0, 3.0 ] 
int[] intArray = { 7, 9, 5, 1, 3 }; System.out.println(Arrays.toString(intArray)); 
[7, 9, 5, 1, 3 ] 
vote vote

83

import java.util.Arrays; 
System.out.println(Arrays.toString(array)); 
System.out.println(Arrays.deepToString(array)); 
vote vote

79

import java.util.Arrays; . . . System.out.println( Arrays.toString( myarray ) ); 
vote vote

61

String[] strArray = new String[] {"John", "Mary", "Bob"};  // #1 Arrays.asList(strArray).stream().forEach(s -> System.out.println(s));  // #2 Stream.of(strArray).forEach(System.out::println);  // #3 Arrays.stream(strArray).forEach(System.out::println);  /* output: John Mary Bob */ 
vote vote

56

String[] greeting = {"Hey", "there", "amigo!"}; String delimiter = " "; String.join(delimiter, greeting)  

Questions similaires