Reputation: 13
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?
Upvotes: 1
Views: 7529
Reputation: 1949
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");
}
}
}
Upvotes: 4