为了将数组转换为一维和二维数组,让我们首先创建一维和二维数组-
String str[] = {"p", "q", "r", "s", "t", "u", "v", "w","x", "y", "z"};
doubled [][]= { {1.2, 1.3, 2.1, 4.1}, {1.5, 2.3}, {2.5, 4.4}, {3.8}, {4.9}, {3.2, 2.1, 3.2, 7.2} };
将数组转换为一维数组的字符串-
Arrays.toString(str);
将数组转换为二维数组的字符串-
Arrays.deepToString(d);
import java.util.Arrays; public class Demo { public static void main(String args[]) { String str[] = {"p", "q", "r", "s", "t", "u", "v", "w","x", "y", "z"}; doubled [][]= { {1.2, 1.3, 2.1, 4.1}, {1.5, 2.3}, {2.5, 4.4}, {3.8}, {4.9}, {3.2, 2.1, 3.2, 7.2} }; System.out.println("One dimensional = "+Arrays.toString(str)); System.out.println("Two dimensional = "+Arrays.deepToString(d)); } }
输出结果
One dimensional = [p, q, r, s, t, u, v, w, x, y, z] Two dimensional = [[1.2, 1.3, 2.1, 4.1], [1.5, 2.3], [2.5, 4.4], [3.8], [4.9], [3.2, 2.1, 3.2, 7.2]]