I’m writing an inventory list program, that provides the user with option of adding an item to the list of exiting the program. To add an item the user enters 1 and is promoted to enter the item details( item name, price and quantity of the items). The information enter by the user is entered into a 3×3 2D array table, which already has a few values in it. The problem here is that I am finding it difficult to update/add new elements to the 3×3 2D array table. I’ve searched the internet for solutions, but I couldn’t find any. Below is a code snippet of the 3×3 2D array table I wrote.
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));
}
}
P.S I’m new to arrays and don’t really know how to use them.
Advertisement
Answer
Here is a break-down of one way this can be done. Read the comments in code:
// 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));
}
When run, your console output should look something like this:
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]
Consider using a List
or an ArrayList
as these can grow and shrink dynamically.