什么';打印Java数组的最简单方法是什么?
在Java中,数组不重写什么';打印Java数组的最简单方法是什么?,java,arrays,printing,Java,Arrays,Printing,在Java中,数组不重写toString(),因此如果您试图直接打印一个数组,则会得到className+'@'+数组的十六进制,如Object.toString()所定义: // 1) toString() int[] arrayInt = new int[] {10, 20, 30, 40, 50}; System.out.println(Arrays.toString(arrayInt)); // 2 for loop() for (int number
toString()
,因此如果您试图直接打印一个数组,则会得到className
+'@'+数组的十六进制,如Object.toString()
所定义:
// 1) toString()
int[] arrayInt = new int[] {10, 20, 30, 40, 50};
System.out.println(Arrays.toString(arrayInt));
// 2 for loop()
for (int number : arrayInt) {
System.out.println(number);
}
// 3 for each()
for(int x: arrayInt){
System.out.println(x);
}
但通常情况下,我们想要的东西更像[1,2,3,4,5]
。最简单的方法是什么?以下是一些输入和输出示例:
// Array of primitives:
int[] intArray = new int[] {1, 2, 3, 4, 5};
//output: [1, 2, 3, 4, 5]
// Array of object references:
String[] strArray = new String[] {"John", "Mary", "Bob"};
//output: [John, Mary, Bob]
// 1) toString()
int[] arrayInt = new int[] {10, 20, 30, 40, 50};
System.out.println(Arrays.toString(arrayInt));
// 2 for loop()
for (int number : arrayInt) {
System.out.println(number);
}
// 3 for each()
for(int x: arrayInt){
System.out.println(x);
}
由于Java5,您可以在数组中使用或来表示数组。请注意,Object[]
version对数组中的每个对象调用.toString()
。输出甚至按照您要求的方式进行装饰
// 1) toString()
int[] arrayInt = new int[] {10, 20, 30, 40, 50};
System.out.println(Arrays.toString(arrayInt));
// 2 for loop()
for (int number : arrayInt) {
System.out.println(number);
}
// 3 for each()
for(int x: arrayInt){
System.out.println(x);
}
示例:
// 1) toString()
int[] arrayInt = new int[] {10, 20, 30, 40, 50};
System.out.println(Arrays.toString(arrayInt));
// 2 for loop()
for (int number : arrayInt) {
System.out.println(number);
}
// 3 for each()
for(int x: arrayInt){
System.out.println(x);
}
- 简单数组:
输出:
[John, Mary, Bob]
[[John, Mary], [Alice, Bob]]
[7.0, 9.0, 5.0, 1.0, 3.0 ]
[7, 9, 5, 1, 3 ]
item 1 item 2 item 3
// 1) toString() int[] arrayInt = new int[] {10, 20, 30, 40, 50}; System.out.println(Arrays.toString(arrayInt)); // 2 for loop() for (int number : arrayInt) { System.out.println(number); } // 3 for each() for(int x: arrayInt){ System.out.println(x); }
{1,2,3,4,5} {John,Mary,Bob}
- 嵌套数组:
输出:
[John, Mary, Bob]
[[John, Mary], [Alice, Bob]]
[7.0, 9.0, 5.0, 1.0, 3.0 ]
[7, 9, 5, 1, 3 ]
item 1 item 2 item 3
// 1) toString() int[] arrayInt = new int[] {10, 20, 30, 40, 50}; System.out.println(Arrays.toString(arrayInt)); // 2 for loop() for (int number : arrayInt) { System.out.println(number); } // 3 for each() for(int x: arrayInt){ System.out.println(x); }
{1,2,3,4,5} {John,Mary,Bob}
数组: 输出:double
[John, Mary, Bob]
[[John, Mary], [Alice, Bob]]
[7.0, 9.0, 5.0, 1.0, 3.0 ]
[7, 9, 5, 1, 3 ]
item 1 item 2 item 3
// 1) toString() int[] arrayInt = new int[] {10, 20, 30, 40, 50}; System.out.println(Arrays.toString(arrayInt)); // 2 for loop() for (int number : arrayInt) { System.out.println(number); } // 3 for each() for(int x: arrayInt){ System.out.println(x); }
{1,2,3,4,5} {John,Mary,Bob}
数组: 输出:int
[John, Mary, Bob]
[[John, Mary], [Alice, Bob]]
[7.0, 9.0, 5.0, 1.0, 3.0 ]
[7, 9, 5, 1, 3 ]
item 1 item 2 item 3
// 1) toString() int[] arrayInt = new int[] {10, 20, 30, 40, 50}; System.out.println(Arrays.toString(arrayInt)); // 2 for loop() for (int number : arrayInt) { System.out.println(number); } // 3 for each() for(int x: arrayInt){ System.out.println(x); }
{1,2,3,4,5} {John,Mary,Bob}
Object[]
version对数组中的每个对象调用.toString()
。输出甚至按照您要求的方式进行装饰
// 1) toString()
int[] arrayInt = new int[] {10, 20, 30, 40, 50};
System.out.println(Arrays.toString(arrayInt));
// 2 for loop()
for (int number : arrayInt) {
System.out.println(number);
}
// 3 for each()
for(int x: arrayInt){
System.out.println(x);
}
示例:
// 1) toString()
int[] arrayInt = new int[] {10, 20, 30, 40, 50};
System.out.println(Arrays.toString(arrayInt));
// 2 for loop()
for (int number : arrayInt) {
System.out.println(number);
}
// 3 for each()
for(int x: arrayInt){
System.out.println(x);
}
- 简单数组:
输出:
[John, Mary, Bob]
[[John, Mary], [Alice, Bob]]
[7.0, 9.0, 5.0, 1.0, 3.0 ]
[7, 9, 5, 1, 3 ]
item 1 item 2 item 3
// 1) toString() int[] arrayInt = new int[] {10, 20, 30, 40, 50}; System.out.println(Arrays.toString(arrayInt)); // 2 for loop() for (int number : arrayInt) { System.out.println(number); } // 3 for each() for(int x: arrayInt){ System.out.println(x); }
{1,2,3,4,5} {John,Mary,Bob}
- 嵌套数组:
输出:
[John, Mary, Bob]
[[John, Mary], [Alice, Bob]]
[7.0, 9.0, 5.0, 1.0, 3.0 ]
[7, 9, 5, 1, 3 ]
item 1 item 2 item 3
// 1) toString() int[] arrayInt = new int[] {10, 20, 30, 40, 50}; System.out.println(Arrays.toString(arrayInt)); // 2 for loop() for (int number : arrayInt) { System.out.println(number); } // 3 for each() for(int x: arrayInt){ System.out.println(x); }
{1,2,3,4,5} {John,Mary,Bob}
数组: 输出:double
[John, Mary, Bob]
[[John, Mary], [Alice, Bob]]
[7.0, 9.0, 5.0, 1.0, 3.0 ]
[7, 9, 5, 1, 3 ]
item 1 item 2 item 3
// 1) toString() int[] arrayInt = new int[] {10, 20, 30, 40, 50}; System.out.println(Arrays.toString(arrayInt)); // 2 for loop() for (int number : arrayInt) { System.out.println(number); } // 3 for each() for(int x: arrayInt){ System.out.println(x); }
{1,2,3,4,5} {John,Mary,Bob}
数组: 输出:int
[John, Mary, Bob]
[[John, Mary], [Alice, Bob]]
[7.0, 9.0, 5.0, 1.0, 3.0 ]
[7, 9, 5, 1, 3 ]
item 1 item 2 item 3
// 1) toString() int[] arrayInt = new int[] {10, 20, 30, 40, 50}; System.out.println(Arrays.toString(arrayInt)); // 2 for loop() for (int number : arrayInt) { System.out.println(number); } // 3 for each() for(int x: arrayInt){ System.out.println(x); }
{1,2,3,4,5} {John,Mary,Bob}
import java.util.Arrays;
// 1) toString()
int[] arrayInt = new int[] {10, 20, 30, 40, 50};
System.out.println(Arrays.toString(arrayInt));
// 2 for loop()
for (int number : arrayInt) {
System.out.println(number);
}
// 3 for each()
for(int x: arrayInt){
System.out.println(x);
}
然后尝试:
System.out.println(Arrays.toString(array));
// 1) toString()
int[] arrayInt = new int[] {10, 20, 30, 40, 50};
System.out.println(Arrays.toString(arrayInt));
// 2 for loop()
for (int number : arrayInt) {
System.out.println(number);
}
// 3 for each()
for(int x: arrayInt){
System.out.println(x);
}
或者,如果您的数组包含其他数组作为元素:
System.out.println(Arrays.deepToString(array));
// 1) toString()
int[] arrayInt = new int[] {10, 20, 30, 40, 50};
System.out.println(Arrays.toString(arrayInt));
// 2 for loop()
for (int number : arrayInt) {
System.out.println(number);
}
// 3 for each()
for(int x: arrayInt){
System.out.println(x);
}
始终首先检查标准库
import java.util.Arrays;
// 1) toString()
int[] arrayInt = new int[] {10, 20, 30, 40, 50};
System.out.println(Arrays.toString(arrayInt));
// 2 for loop()
for (int number : arrayInt) {
System.out.println(number);
}
// 3 for each()
for(int x: arrayInt){
System.out.println(x);
}
然后尝试:
System.out.println(Arrays.toString(array));
// 1) toString()
int[] arrayInt = new int[] {10, 20, 30, 40, 50};
System.out.println(Arrays.toString(arrayInt));
// 2 for loop()
for (int number : arrayInt) {
System.out.println(number);
}
// 3 for each()
for(int x: arrayInt){
System.out.println(x);
}
或者,如果您的数组包含其他数组作为元素:
System.out.println(Arrays.deepToString(array));
// 1) toString()
int[] arrayInt = new int[] {10, 20, 30, 40, 50};
System.out.println(Arrays.toString(arrayInt));
// 2 for loop()
for (int number : arrayInt) {
System.out.println(number);
}
// 3 for each()
for(int x: arrayInt){
System.out.println(x);
}
如果您使用的是Java 1.4,则可以执行以下操作:
System.out.println(Arrays.asList(array));
// 1) toString()
int[] arrayInt = new int[] {10, 20, 30, 40, 50};
System.out.println(Arrays.toString(arrayInt));
// 2 for loop()
for (int number : arrayInt) {
System.out.println(number);
}
// 3 for each()
for(int x: arrayInt){
System.out.println(x);
}
(当然,这也适用于1.5+)如果您使用的是Java 1.4,您可以改为:
System.out.println(Arrays.asList(array));
// 1) toString()
int[] arrayInt = new int[] {10, 20, 30, 40, 50};
System.out.println(Arrays.toString(arrayInt));
// 2 for loop()
for (int number : arrayInt) {
System.out.println(number);
}
// 3 for each()
for(int x: arrayInt){
System.out.println(x);
}
(当然,这也适用于1.5+版本。)知道这一点很好,但是,至于“总是先检查标准库”,我永远不会偶然发现
数组的诀窍。toString(myarray)
// 1) toString()
int[] arrayInt = new int[] {10, 20, 30, 40, 50};
System.out.println(Arrays.toString(arrayInt));
// 2 for loop()
for (int number : arrayInt) {
System.out.println(number);
}
// 3 for each()
for(int x: arrayInt){
System.out.println(x);
}
--因为我专注于myarray的类型,想看看如何做到这一点。我不想重复这件事:我想要一个简单的调用,使它与我在Eclipse调试器和myarray中看到的类似。toString()只是没有这样做
import java.util.Arrays;
.
.
.
System.out.println( Arrays.toString( myarray ) );
// 1) toString()
int[] arrayInt = new int[] {10, 20, 30, 40, 50};
System.out.println(Arrays.toString(arrayInt));
// 2 for loop()
for (int number : arrayInt) {
System.out.println(number);
}
// 3 for each()
for(int x: arrayInt){
System.out.println(x);
}
然而,知道这一点很好,至于“总是先检查标准库”,我永远不会偶然发现
array.toString(myarray)
// 1) toString()
int[] arrayInt = new int[] {10, 20, 30, 40, 50};
System.out.println(Arrays.toString(arrayInt));
// 2 for loop()
for (int number : arrayInt) {
System.out.println(number);
}
// 3 for each()
for(int x: arrayInt){
System.out.println(x);
}
--因为我专注于myarray的类型,想看看如何做到这一点。我不想重复这件事:我想要一个简单的调用,使它与我在Eclipse调试器和myarray中看到的类似。toString()只是没有这样做
import java.util.Arrays;
.
.
.
System.out.println( Arrays.toString( myarray ) );
// 1) toString()
int[] arrayInt = new int[] {10, 20, 30, 40, 50};
System.out.println(Arrays.toString(arrayInt));
// 2 for loop()
for (int number : arrayInt) {
System.out.println(number);
}
// 3 for each()
for(int x: arrayInt){
System.out.println(x);
}
Arrays.deepToString(arr)
仅在一行上打印
int[][] table = new int[2][2];
// 1) toString()
int[] arrayInt = new int[] {10, 20, 30, 40, 50};
System.out.println(Arrays.toString(arrayInt));
// 2 for loop()
for (int number : arrayInt) {
System.out.println(number);
}
// 3 for each()
for(int x: arrayInt){
System.out.println(x);
}
要将表格打印为二维表格,我必须执行以下操作:
System.out.println(Arrays.deepToString(table).replaceAll("],", "]," + System.getProperty("line.separator")));
// 1) toString()
int[] arrayInt = new int[] {10, 20, 30, 40, 50};
System.out.println(Arrays.toString(arrayInt));
// 2 for loop()
for (int number : arrayInt) {
System.out.println(number);
}
// 3 for each()
for(int x: arrayInt){
System.out.println(x);
}
看起来像是数组。deepToString(arr)方法应该使用分隔符字符串,但不幸的是它没有。数组。deepToString(arr)只在一行上打印
int[][] table = new int[2][2];
// 1) toString()
int[] arrayInt = new int[] {10, 20, 30, 40, 50};
System.out.println(Arrays.toString(arrayInt));
// 2 for loop()
for (int number : arrayInt) {
System.out.println(number);
}
// 3 for each()
for(int x: arrayInt){
System.out.println(x);
}
要将表格打印为二维表格,我必须执行以下操作:
System.out.println(Arrays.deepToString(table).replaceAll("],", "]," + System.getProperty("line.separator")));
// 1) toString()
int[] arrayInt = new int[] {10, 20, 30, 40, 50};
System.out.println(Arrays.toString(arrayInt));
// 2 for loop()
for (int number : arrayInt) {
System.out.println(number);
}
// 3 for each()
for(int x: arrayInt){
System.out.println(x);
}
这看起来像是
数组。deepToString(arr)
方法应该使用分隔符字符串,但不幸的是它没有。在我看来,使用常规for循环是打印数组的最简单方法。
// 1) toString()
int[] arrayInt = new int[] {10, 20, 30, 40, 50};
System.out.println(Arrays.toString(arrayInt));
// 2 for loop()
for (int number : arrayInt) {
System.out.println(number);
}
// 3 for each()
for(int x: arrayInt){
System.out.println(x);
}
这里有一个基于intArray的示例代码
for (int i = 0; i < intArray.length; i++) {
System.out.print(intArray[i] + ", ");
}
// 1) toString()
int[] arrayInt = new int[] {10, 20, 30, 40, 50};
System.out.println(Arrays.toString(arrayInt));
// 2 for loop()
for (int number : arrayInt) {
System.out.println(number);
}
// 3 for each()
for(int x: arrayInt){
System.out.println(x);
}
for(int i=0;i
它的输出和你的一样
1,2,3,4,5在我看来,使用常规的for循环是打印数组的最简单方法。
// 1) toString()
int[] arrayInt = new int[] {10, 20, 30, 40, 50};
System.out.println(Arrays.toString(arrayInt));
// 2 for loop()
for (int number : arrayInt) {
System.out.println(number);
}
// 3 for each()
for(int x: arrayInt){
System.out.println(x);
}
这里有一个基于intArray的示例代码
for (int i = 0; i < intArray.length; i++) {
System.out.print(intArray[i] + ", ");
}
// 1) toString()
int[] arrayInt = new int[] {10, 20, 30, 40, 50};
System.out.println(Arrays.toString(arrayInt));
// 2 for loop()
for (int number : arrayInt) {
System.out.println(number);
}
// 3 for each()
for(int x: arrayInt){
System.out.println(x);
}
for(int i=0;i
它的输出和你的一样
在JDK1.8中,可以使用聚合运算和lambda表达式:
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
*/
// 1) toString()
int[] arrayInt = new int[] {10, 20, 30, 40, 50};
System.out.println(Arrays.toString(arrayInt));
// 2 for loop()
for (int number : arrayInt) {
System.out.println(number);
}
// 3 for each()
for(int x: arrayInt){
System.out.println(x);
}
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
*/
在JDK1.8中,可以使用聚合操作和lambda表达式:
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
*/
// 1) toString()
int[] arrayInt = new int[] {10, 20, 30, 40, 50};
System.out.println(Arrays.toString(arrayInt));
// 2 for loop()
for (int number : arrayInt) {
System.out.println(number);
}
// 3 for each()
for(int x: arrayInt){
System.out.println(x);
}
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
*/
要添加到所有答案中,还可以选择将对象打印为JSON字符串
// 1) toString()
int[] arrayInt = new int[] {10, 20, 30, 40, 50};
System.out.println(Arrays.toString(arrayInt));
// 2 for loop()
for (int number : arrayInt) {
System.out.println(number);
}
// 3 for each()
for(int x: arrayInt){
System.out.println(x);
}
使用Jackson:
ObjectWriter ow = new ObjectMapper().writer().withDefaultPrettyPrinter();
System.out.println(ow.writeValueAsString(anyArray));
// 1) toString()
int[] arrayInt = new int[] {10, 20, 30, 40, 50};
System.out.println(Arrays.toString(arrayInt));
// 2 for loop()
for (int number : arrayInt) {
System.out.println(number);
}
// 3 for each()
for(int x: arrayInt){
System.out.println(x);
}
使用Gson:
Gson gson = new Gson();
System.out.println(gson.toJson(anyArray));
// 1) toString()
int[] arrayInt = new int[] {10, 20, 30, 40, 50};
System.out.println(Arrays.toString(arrayInt));
// 2 for loop()
for (int number : arrayInt) {
System.out.println(number);
}
// 3 for each()
for(int x: arrayInt){
System.out.println(x);
}
要添加到所有答案中,还可以选择将对象打印为JSON字符串
// 1) toString()
int[] arrayInt = new int[] {10, 20, 30, 40, 50};
System.out.println(Arrays.toString(arrayInt));
// 2 for loop()
for (int number : arrayInt) {
System.out.println(number);
}
// 3 for each()
for(int x: arrayInt){
System.out.println(x);
}
使用Jackson:
ObjectWriter ow = new ObjectMapper().writer().withDefaultPrettyPrinter();
System.out.println(ow.writeValueAsString(anyArray));
// 1) toString()
int[] arrayInt = new int[] {10, 20, 30, 40, 50};
System.out.println(Arrays.toString(arrayInt));
// 2 for loop()
for (int number : arrayInt) {
System.out.println(number);
}
// 3 for each()
for(int x: arrayInt){
System.out.println(x);
}
使用Gson:
Gson gson = new Gson();
System.out.println(gson.toJson(anyArray));
// 1) toString()
int[] arrayInt = new int[] {10, 20, 30, 40, 50};
System.out.println(Arrays.toString(arrayInt));
// 2 for loop()
for (int number : arrayInt) {
System.out.println(number);
}
// 3 for each()
for(int x: arrayInt){
System.out.println(x);
}
如果数组是char[]类型,还有一种方法:
char A[] = {'a', 'b', 'c'};
System.out.println(A); // no other arguments
// 1) toString()
int[] arrayInt = new int[] {10, 20, 30, 40, 50};
System.out.println(Arrays.toString(arrayInt));
// 2 for loop()
for (int number : arrayInt) {
System.out.println(number);
}
// 3 for each()
for(int x: arrayInt){
System.out.println(x);
}
印刷品
abc
// 1) toString()
int[] arrayInt = new int[] {10, 20, 30, 40, 50};
System.out.println(Arrays.toString(arrayInt));
// 2 for loop()
for (int number : arrayInt) {
System.out.println(number);
}
// 3 for each()
for(int x: arrayInt){
System.out.println(x);
}
如果数组是char[]类型,还有一种方法:
char A[] = {'a', 'b', 'c'};
System.out.println(A); // no other arguments
// 1) toString()
int[] arrayInt = new int[] {10, 20, 30, 40, 50};
System.out.println(Arrays.toString(arrayInt));
// 2 for loop()
for (int number : arrayInt) {
System.out.println(number);
}
// 3 for each()
for(int x: arrayInt){
System.out.println(x);
}
印刷品
abc
// 1) toString()
int[] arrayInt = new int[] {10, 20, 30, 40, 50};
System.out.println(Arrays.toString(arrayInt));
// 2 for loop()
for (int number : arrayInt) {
System.out.println(number);
}
// 3 for each()
for(int x: arrayInt){
System.out.println(x);
}
公共类打印机{
公共静态void main(字符串[]args){
字符串a[]=新字符串[4];
扫描仪sc=新的扫描仪(System.in);
System.out.println(“输入数据”);
对于(int i=0;i<4;i++){
a[i]=sc.nextLine();
}
System.out.println(“输入的数据为”);
用于(字符串i:a){
系统输出打印LN(i);
}
}
}
公共类打印机{
公共静态void main(字符串[]args){
字符串a[]=新字符串[4];
扫描仪sc=新的扫描仪(System.in);
System.out.println(“输入数据”);
对于(int i=0;i<4;i++){
a[i]=sc.nextLine();
}
System.out.println(“输入的数据为”);
用于(字符串i:a){
系统输出打印LN(i);
}
}
}
我尝试过的简化快捷方式是:
int x[] = {1,2,3};
String printableText = Arrays.toString(x).replaceAll("[\\[\\]]", "").replaceAll(", ", "\n");
System.out.println(printableText);
// 1) toString()
int[] arrayInt = new int[] {10, 20, 30, 40, 50};
System.out.println(Arrays.toString(arrayInt));
// 2 for loop()
for (int number : arrayInt) {
System.out.println(number);
}
// 3 for each()
for(int x: arrayInt){
System.out.println(x);
}
它会打印出来
1
2
3
// 1) toString()
int[] arrayInt = new int[] {10, 20, 30, 40, 50};
System.out.println(Arrays.toString(arrayInt));
// 2 for loop()
for (int number : arrayInt) {
System.out.println(number);
}
// 3 for each()
for(int x: arrayInt){
System.out.println(x);
}
这种方法不需要循环,最好只用于小型阵列我尝试过的简化快捷方式是:
int x[] = {1,2,3};
String printableText = Arrays.toString(x).replaceAll("[\\[\\]]", "").replaceAll(", ", "\n");
System.out.println(printableText);
// 1) toString()
int[] arrayInt = new int[] {10, 20, 30, 40, 50};
System.out.println(Arrays.toString(arrayInt));
// 2 for loop()
for (int number : arrayInt) {
System.out.println(number);
}
// 3 for each()
for(int x: arrayInt){
System.out.println(x);
}
它会打印出来
1
2
3
// 1) toString()
int[] arrayInt = new int[] {10, 20, 30, 40, 50};
System.out.println(Arrays.toString(arrayInt));
// 2 for loop()
for (int number : arrayInt) {
System.out.println(number);
}
// 3 for each()
for(int x: arrayInt){
System.out.println(x);
}
这种方法不需要循环,最好只用于Java 8之前的小型阵列
// 1) toString()
int[] arrayInt = new int[] {10, 20, 30, 40, 50};
System.out.println(Arrays.toString(arrayInt));
// 2 for loop()
for (int number : arrayInt) {
System.out.println(number);
}
// 3 for each()
for(int x: arrayInt){
System.out.println(x);
}
我们可以使用array.toString(array)
打印一维数组,而array.deepToString(array)
打印多维数组
// 1) toString()
int[] arrayInt = new int[] {10, 20, 30, 40, 50};
System.out.println(Arrays.toString(arrayInt));
// 2 for loop()
for (int number : arrayInt) {
System.out.println(number);
}
// 3 for each()
for(int x: arrayInt){
System.out.println(x);
}
爪哇8
现在我们有了Stream
和lambda
选项来打印数组
// 1) toString()
int[] arrayInt = new int[] {10, 20, 30, 40, 50};
System.out.println(Arrays.toString(arrayInt));
// 2 for loop()
for (int number : arrayInt) {
System.out.println(number);
}
// 3 for each()
for(int x: arrayInt){
System.out.println(x);
}
打印一维数组:
public static void main(String[] args) {
int[] intArray = new int[] {1, 2, 3, 4, 5};
String[] strArray = new String[] {"John", "Mary", "Bob"};
//Prior to Java 8
System.out.println(Arrays.toString(intArray));
System.out.println(Arrays.toString(strArray));
// In Java 8 we have lambda expressions
Arrays.stream(intArray).forEach(System.out::println);
Arrays.stream(strArray).forEach(System.out::println);
}
// 1) toString()
int[] arrayInt = new int[] {10, 20, 30, 40, 50};
System.out.println(Arrays.toString(arrayInt));
// 2 for loop()
for (int number : arrayInt) {
System.out.println(number);
}
// 3 for each()
for(int x: arrayInt){
System.out.println(x);
}
输出为:
// 1) toString()
int[] arrayInt = new int[] {10, 20, 30, 40, 50};
System.out.println(Arrays.toString(arrayInt));
// 2 for loop()
for (int number : arrayInt) {
System.out.println(number);
}
// 3 for each()
for(int x: arrayInt){
System.out.println(x);
}
[1,2,3,4,5][约翰、玛丽、鲍勃]
1
2
3
4
5