繁体   English   中英

向 3x3 二维数组添加更多元素

[英]Add more element to a 3x3 2D array

我正在编写一个库存列表程序,它为用户提供了将项目添加到退出程序列表的选项。 要添加项目,用户输入 1 并被提示输入项目详细信息(项目名称、价格和项目数量)。 用户输入的信息被输入到一个 3x3 2D 数组表中,其中已经有一些值。 这里的问题是我发现很难将新元素更新/添加到 3x3 2D 数组表中。 我已经在互联网上搜索了解决方案,但我找不到任何解决方案。 下面是我编写的 3x3 2D 数组表的代码片段。

import java.util.Arrays;
import java.util.Scanner;

public class Main {
    public static void main(String[] args) {
        // put your code here
        int[][] one_d_array = {{2, 4, 6, 8}, {3, 5, 7, 9}};
        int newArr[][] = new int[one_d_array.length + 1][one_d_array.length + 1];
        Scanner sc = new Scanner(System.in);
        System.out.println("Enter your desired number:");
        int num = sc.nextInt();
        System.out.println("Enter your desired number:");
        int num2 = sc.nextInt();
        //System.out.println(Arrays.toString(oned_array));
        for (int i = 0; i < one_d_array.length; i++) {
            for (int j = 0; j < one_d_array.length; j++) {
                newArr[i][j] = one_d_array[i][j];
            }
        }
        newArr[one_d_array.length][one_d_array.length] = ;
        System.out.println(Arrays.toString(newArr));
    }
}

PS 我是 arrays 的新手,真的不知道如何使用它们。

这是可以完成的一种方法的分解。 阅读代码中的注释:

// A 2x4 int[] array (2 Rows by 4 Columns in Each Row):
int[][] one_d_array = { // C0 C1 C2 C3      C = Column
                         { 2, 4, 6, 8 },    // Row 0
                         { 3, 5, 7, 9 }     // Row 1  
                      };
    
// Display the current Array into the Console window:
System.out.println("Your Current 2D Array (one_d_array):");
for (int[] ary : one_d_array) {
    System.out.println(Arrays.toString(ary));
}
System.out.println();
    
/* Adding an additional Column to each Row:
   One way to do this is to recreate the original 
   Array with the help of a temporary array:   */
/* If you don't want to change the number of Rows
   then remove the `+ 1` from newNumberOfRows:  */
int newNumberOfRows    = one_d_array.length;      // one_d_array.length + 1;
int newNumberOfColumns = one_d_array[0].length + 1;
    
/* Declare and initialize a new 2D Array to accommodate 
   your desired new size (in this case columns +1).   */
int[][] newArr = new int[newNumberOfRows][newNumberOfColumns];
    
/* Copy the Original Array (one_d_array) into the new Array (newArr). 
   We'll use the `System.arraycopy()` method to do this. This will fill
   your new 2D array with the data from the Original 2D Array except 
   for the new Column(s) you added.                             */
for (int i = 0; i < one_d_array.length; i++) {
    System.arraycopy(one_d_array[i], 0, newArr[i], 0, one_d_array[i].length);
}
    
Scanner sc = new Scanner(System.in); // Open a Stream for keyboard input.
   
// User to supply Data values to the new columns in each row:
int num;
for (int i = 0; i < newNumberOfRows; i++) {
    System.out.print("Enter your desired number for Column #" 
                 + newNumberOfColumns + " of Row #" + (i+1) + ": -> ");
    num = sc.nextInt();
    /* We subtract 1 from newNumberOfColumns because array 
       indexes start from 0. newNumberOfColumns holds a literal
       value.                             */
    newArr[i][newNumberOfColumns - 1] = num; 
}
    
// Copy the new Array (newArr) into the Original Array (one_d_array)
one_d_array = new int[newArr.length][newArr[0].length];
for (int i = 0; i < newArr.length; i++) {
    System.arraycopy(newArr[i], 0, one_d_array[i], 0, newArr[i].length);
}
    
// Print the Modified Original Array to the Console Window:
System.out.println();
System.out.println("Your Modified 2D Array (one_d_array):");
for (int[] ary : one_d_array) {
    System.out.println(Arrays.toString(ary));
}

运行时,您的控制台 output 应如下所示:

Your Current 2D Array (one_d_array):
[2, 4, 6, 8]
[3, 5, 7, 9]

Enter your desired number for Column #5 of Row #1: -> 24
Enter your desired number for Column #5 of Row #2: -> 36

Your Modified 2D Array (one_d_array):
[2, 4, 6, 8, 24]
[3, 5, 7, 9, 36]

考虑使用ListArrayList ,因为它们可以动态增长和收缩。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM