1

I am trying to make a 2D array that i can dynamically update. Lets say i have a 2D array with 10 rows and 3 columns. I want to add an int value to a specific row, thereby adding an extra column to that row (and only that row) so that it alone would have 4 columns. Here's what i have so far.

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");
        }
    }

}

The problem is that with this code the program adds the new value to each row instead of only the one specified (in this case row 0). How could I fix this?

Additionally, how could i make the program do the opposite as well -> remove a value from an individual row and shorten that row?

8
  • Is there a particular reason you don't want to use one of the list types? They would easily solve this problem. Commented Jan 18, 2015 at 4:23
  • Mic is right -- ArrayList is good for this sort of thing too. Though if you're hell bent on it; I'd suggest the grueling process of copying the 2-d array to another 2-d array, then re-initializing the original 2-d array to have the proper dimensions, then add to it accordingly with the extra values. Commented Jan 18, 2015 at 4:33
  • @Mic This is for an assignment and we are to use arrays only. Note I am very new to java, just learning and we're only supposed to use what we've covered in the course so far. We havent covered any list types yet. Commented Jan 18, 2015 at 4:35
  • @Pr0metheus - Okay, that's a fair reason. As Woodrow said, you're looking at defining a new array, copying the existing array into it, and then replacing it. But I think it can be done on a single dimension rather than doing the whole thing. I'll fire up my IDE and try to confirm that for you. Commented Jan 18, 2015 at 4:45
  • 2
    Sashwat's answer has already addressed pretty much everything I was going to bring up, and he redefines just one row at a time, which is what I was after. So, not much to add to that. I guess the trick is that you don't just have to think of it as a grid of variables. You could view it as an array of arrays as well. Commented Jan 18, 2015 at 5:15

1 Answer 1

4

In case you just want to add the new element to the new array[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;
}

In case you want to put to a specific row number and you should get that as an argument in the put method.

public void put(int N, int rowNum);

You should also change the size element to be a array which will track the size of each row.

int[] size = new int[10];

Accordingly change the size of the row only when that specific row that reached its limit.

Check the code below

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");
        }
    }

}
Sign up to request clarification or add additional context in comments.

1 Comment

Could you post some code to show me how i would go about implementing that? ( int[] size = new int[]; )

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.