PrintArrayMethod.java
package com.example.demo.util;
import java.util.Arrays;
public class PrintArrayMethod {
public static void main(String[] args) {
String[] text = {"a","b","c"};
System.out.println("传统的for循环方式: ");
test1(text);
System.out.println("\nfor each 方式: ");
test2(text);
System.out.println("\nArrays.toString(Object[] a) 方式: ");
test3(text);
System.out.println("\n lambda表达式方式: ");
test4(text);
System.out.println("\n Arrays.deepToString()方式: ");
test5(text);
}
public static void test1(String[] array) {
for (int i = 0; i < array.length; i++) {
System.out.println(array[i]);
}
}
public static void test2(String[] array) {
for (String str:array) {
System.out.println(str);
}
}
public static void test3(String[] array) {
System.out.println(Arrays.toString(array));
}
public static void test4(String[] array) {
Arrays.stream(array).forEach(System.out::println);
System.out.println("\n");
Arrays.stream(array).forEach(str -> {
System.out.println(str);
});
}
public static void test5(String[] array) {
System.out.println(Arrays.deepToString(array));
System.out.println("\n多维数组");
int[][] magicSquare = { {16,3,2,13}, {5,10,11,8}, {9,6,7,3} };
System.out.println(Arrays.deepToString(magicSquare));
}
}
输出
传统的for循环方式:
a
b
c
for each 方式:
a
b
c
Arrays.toString(Object[] a) 方式:
[a, b, c]
lambda表达式方式:
a
b
c
a
b
c
Arrays.deepToString()方式:
[a, b, c]
多维数组
[[16, 3, 2, 13], [5, 10, 11, 8], [9, 6, 7, 3]]
转载请注明原文地址:https://ipadbbs.8miu.com/read-46071.html