java中的数组里面的数组

我想通过访问父数组来制作多个数组。例如。如何制作这个数组?


在这里,在这段代码中,用户输入了以下数据,代码将简单地打印数组,即 2-> 父数组的总大小(它会告诉编译器用户将输入两个数组作为输入) 6->第一个子数组的大小等等。


2

6

5 1 3 4 18 56

8

8 7 3 1 34 72 89 11

并且它们都是用户输入的,甚至是数组的大小。


 public static void main(String[] args) { 

  Scanner sc = new Scanner(System.in);

   int sarr[] = new int[30];

   int arr[] =  new int[30];

   int n =sc.nextInt();

   for (int i=0;i<n;i++)

   { sarr[i] = sc.nextInt();

     for (int j=0;j<=sarr[i];j++)

     { 

      arr[j]=sc.nextInt();

     }

   }

   for (int i=0;i<n;i++)

   { 

     for (int j=0;j<=sarr[i];j++)

     { 

        System.out.println(arr[i]);

     }

     System.out.println();

   }


神不在的星期二
浏览 211回答 2
2回答

慕森王

如果你想在java中创建一个多维数组,你应该使用这个语法:int n = ...;&nbsp;int array[][] = new int[n][n];例如:try (Scanner sc = new Scanner(System.in)) {&nbsp; &nbsp; &nbsp; &nbsp; int n = sc.nextInt();&nbsp; &nbsp; &nbsp; &nbsp; int arr[][] = new int[n][n];&nbsp; &nbsp; &nbsp; &nbsp; for (int i = 0; i < n; i++) {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; for (int j = 0; j < n; j++) {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; arr[i][j] = sc.nextInt();&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; }&nbsp; &nbsp; &nbsp; &nbsp; }&nbsp; &nbsp; &nbsp; &nbsp; for (int i = 0; i < n; i++) {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; for (int j = 0; j < n; j++) {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; System.out.println(arr[i][j]);&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; }&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; System.out.println();&nbsp; &nbsp; &nbsp; &nbsp; }&nbsp; &nbsp; }一些最后的笔记:完成后应该关闭流您不会“告诉编译器”,因为编译器不会在运行时执行。无论如何,您的意思是JVM。

噜噜哒

这是一个关于如何构建具有不同大小行的二维数组的解决方案。每个数组都是在我们询问其大小后创建的Scanner sc = new Scanner(System.in);System.out.println("Number of arrays");String str1 = sc.nextLine();int numberOfArrays = Integer.valueOf(str1);int[][] array = new int[numberOfArrays][];&nbsp; &nbsp; for (int i = 0; i < numberOfArrays; i++) {&nbsp; &nbsp; &nbsp; &nbsp; System.out.println("Size of array");&nbsp; &nbsp; &nbsp; &nbsp; String str2 = sc.nextLine();&nbsp; &nbsp; &nbsp; &nbsp; int size = Integer.valueOf(str2);&nbsp; &nbsp; &nbsp; &nbsp; int[] row = new int[size];&nbsp; &nbsp; &nbsp; &nbsp; for (int j = 0; j < size; j++) {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; System.out.println("Value:");&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; String str3 = sc.nextLine();&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; int value = Integer.valueOf(str3);&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; row[j] = value;&nbsp; &nbsp; &nbsp; &nbsp; }&nbsp; &nbsp; &nbsp; &nbsp; array[i] = row;&nbsp; &nbsp; &nbsp; &nbsp;&nbsp;&nbsp; &nbsp; }}更新这是一个允许在一行中输入数组中的所有数字的版本。请注意,这里没有错误处理检查给定值与预期值的数量等。for (int i = 0; i < numberOfArrays; i++) {&nbsp; &nbsp; System.out.println("Size of array");&nbsp; &nbsp; String str2 = sc.nextLine();&nbsp; &nbsp; int size = Integer.valueOf(str2);&nbsp; &nbsp; int[] row = new int[size];&nbsp; &nbsp; System.out.println("Values:");&nbsp; &nbsp; String str3 = sc.nextLine();&nbsp; &nbsp; String[] numbers = str3.split("\\s");&nbsp; &nbsp; for (int j = 0; j < size; j++) {&nbsp; &nbsp; &nbsp; &nbsp; row[j] = Integer.valueOf(numbers[j]);&nbsp; &nbsp; }&nbsp; &nbsp; array[i] = row;}
打开App,查看更多内容
随时随地看视频慕课网APP

相关分类

Java