Warning: file_get_contents(/data/phpspider/zhask/data//catemap/3/arrays/14.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Java 如何更新二维阵列中的单个行?(爪哇)_Java_Arrays_Multidimensional Array - Fatal编程技术网

Java 如何更新二维阵列中的单个行?(爪哇)

Java 如何更新二维阵列中的单个行?(爪哇),java,arrays,multidimensional-array,Java,Arrays,Multidimensional Array,我正在尝试制作一个可以动态更新的2D数组。假设我有一个10行3列的2D数组。我想向特定行添加一个int值,从而向该行(仅该行)添加一个额外的列,这样它就可以有4列。这是我到目前为止所拥有的 public class DynamicArray { private int[][] array; private int size; public DynamicArray(int initialSize) { array = new int[10][initia

我正在尝试制作一个可以动态更新的2D数组。假设我有一个10行3列的2D数组。我想向特定行添加一个int值,从而向该行(仅该行)添加一个额外的列,这样它就可以有4列。这是我到目前为止所拥有的

public class DynamicArray {
    private int[][] array;
    private int size;

    public DynamicArray(int initialSize) {
        array = new int[10][initialSize];
        size = 0;
    }

    public int get(int j) {
        return array[0][j];
    }

    public int getSize() {
        return size;
    }

    public void put(int N) {
        if (size < array[0].length)
            array[0][size] = N;
        else // need to create a bigger array
        {
            int[][] temp = new int[10][2 * size];
            for (int i = 0; i < array.length; i++)
                for (int j = 0; j < array[i].length; j++)
                    temp[i][j] = array[i][j];
            temp[0][size] = N;
            array = temp;
        }
        size = size + 1;
    }

    public static void main(String[] args) {
        DynamicArray da = new DynamicArray(3);
        da.put(2);
        da.put(1);
        da.put(3);
        da.put(1);
        da.put(4);
        da.put(5);
        for (int i = 0; i < da.getSize(); i++) {
            for (int j = 0; j < 9; j++) {
                System.out.print((da.get(i) + "\t"));
            }
            System.out.println("\n");
        }
    }

}
公共类动态卡雷{
私有int[][]数组;
私有整数大小;
公共动态卡雷(int initialSize){
数组=新整数[10][initialSize];
尺寸=0;
}
公共int-get(int-j){
返回数组[0][j];
}
公共int getSize(){
返回大小;
}
公开作废认沽期权(整数N){
if(大小<数组[0]。长度)
数组[0][size]=N;
否则//需要创建一个更大的数组
{
int[][]temp=新int[10][2*大小];
for(int i=0;i
问题在于,使用此代码,程序会将新值添加到每一行,而不是仅添加指定的一行(在本例中为第0行)。 我怎样才能解决这个问题


此外,我如何让程序也执行相反的操作->从单个行中删除一个值并缩短该行?

如果您只想将新元素添加到新数组[0]

public void put(int N) {
    if (size < array[0].length)
        array[0][size] = N;
    else { // need to create a bigger array

        int[] temp = new int[2 * size]; // Temporary create a new array with double size

        // fill the empty array with array[0] existing elements
        for (int i = 0; i < size; i++) {
            temp[i] = array[0][i];
        }

        // Change the array[0] to point to the new array
        array[0] = temp;

        // Add the new element to the new array
        array[0][size] = N;
    }
    size = size + 1;
}
您还应该将size元素更改为跟踪每行大小的数组

int[]size=newint[10]

因此,仅当特定行达到其限制时才更改行的大小

检查下面的代码

public class DynamicArray {
    private int[][] array;
    private int[] size;

    public DynamicArray(int initialSize) {
        array = new int[10][initialSize];
        size = new int[10];
    }

    public int get(int rowNum, int colNum) {
        return array[rowNum][colNum];
    }

    public int getSize(int rowNum) {
        return size[rowNum];
    }

    public void put(int N, int rowNum) {
        if (size[rowNum] < array[0].length)
            array[rowNum][size[rowNum]] = N;
        else { // need to create a bigger array

            int[] temp = new int[2 * size[rowNum]];
            for (int i = 0; i < size[rowNum]; i++) {
                temp[i] = array[rowNum][i];
            }
            array[0] = temp;
            array[0][size[rowNum]] = N;
        }
        size[rowNum] = size[rowNum] + 1;
    }

    public static void main(String[] args) {

        DynamicArray da = new DynamicArray(3);
        da.put(2, 0);
        da.put(1, 0);
        da.put(3, 0);
        da.put(1, 0);
        da.put(4, 0);
        da.put(5, 1);
        da.put(2, 4);
        for (int i = 0; i < 10; i++) {
            for (int j = 0; j < da.getSize(i); j++) {
                System.out.print((da.get(i, j) + "\t"));
            }
            System.out.println("\n");
        }
    }

}
公共类动态卡雷{
私有int[][]数组;
私有int[]大小;
公共动态卡雷(int initialSize){
数组=新整数[10][initialSize];
大小=新整数[10];
}
公共int get(int rowNum,int colNum){
返回数组[rowNum][colNum];
}
公共int getSize(int rowNum){
返回大小[rowNum];
}
公共作废put(int N,int rowNum){
if(大小[rowNum]
如果您只想将新元素添加到新数组[0]

public void put(int N) {
    if (size < array[0].length)
        array[0][size] = N;
    else { // need to create a bigger array

        int[] temp = new int[2 * size]; // Temporary create a new array with double size

        // fill the empty array with array[0] existing elements
        for (int i = 0; i < size; i++) {
            temp[i] = array[0][i];
        }

        // Change the array[0] to point to the new array
        array[0] = temp;

        // Add the new element to the new array
        array[0][size] = N;
    }
    size = size + 1;
}
您还应该将size元素更改为跟踪每行大小的数组

int[]size=newint[10]

因此,仅当特定行达到其限制时才更改行的大小

检查下面的代码

public class DynamicArray {
    private int[][] array;
    private int[] size;

    public DynamicArray(int initialSize) {
        array = new int[10][initialSize];
        size = new int[10];
    }

    public int get(int rowNum, int colNum) {
        return array[rowNum][colNum];
    }

    public int getSize(int rowNum) {
        return size[rowNum];
    }

    public void put(int N, int rowNum) {
        if (size[rowNum] < array[0].length)
            array[rowNum][size[rowNum]] = N;
        else { // need to create a bigger array

            int[] temp = new int[2 * size[rowNum]];
            for (int i = 0; i < size[rowNum]; i++) {
                temp[i] = array[rowNum][i];
            }
            array[0] = temp;
            array[0][size[rowNum]] = N;
        }
        size[rowNum] = size[rowNum] + 1;
    }

    public static void main(String[] args) {

        DynamicArray da = new DynamicArray(3);
        da.put(2, 0);
        da.put(1, 0);
        da.put(3, 0);
        da.put(1, 0);
        da.put(4, 0);
        da.put(5, 1);
        da.put(2, 4);
        for (int i = 0; i < 10; i++) {
            for (int j = 0; j < da.getSize(i); j++) {
                System.out.print((da.get(i, j) + "\t"));
            }
            System.out.println("\n");
        }
    }

}
公共类动态卡雷{
私有int[][]数组;
私有int[]大小;
公共动态卡雷(int initialSize){
数组=新整数[10][initialSize];
大小=新整数[10];
}
公共int get(int rowNum,int colNum){
返回数组[rowNum][colNum];
}
公共int getSize(int rowNum){
返回大小[rowNum];
}
公共作废put(int N,int rowNum){
if(大小[rowNum]
您不想使用其中一种列表类型有什么特殊原因吗?他们会很容易解决这个问题。麦克是对的——ArrayList也适用于这类事情。如果你下定决心要做;我建议将二维数组复制到另一个二维数组,然后重新初始化原始二维数组,使其具有适当的维数,然后相应地添加额外的值。@Mic这是一个赋值过程,我们只使用数组。请注意,我对java非常陌生,只是在学习,我们应该只使用到目前为止我们在本课程中介绍的内容。我们还没有涵盖任何列表类型。@Pr0metheus-好吧,这是一个合理的理由。正如Woodrow所说,您正在考虑定义一个新数组,将现有数组复制到其中,然后替换它。但我认为它可以在一个维度上完成,而不是在w维度上完成