public static Groceries [] addGrocery(Groceries[] arr, String name, int price) {
int arrayLength = Array.getLength(arr);
System.out.println(arrayLength);
Groceries[] newGrocery = new Groceries[arrayLength + 1];
int arrayLength2 = Array.getLength(newGrocery);
System.out.println(arrayLength2);
int i;
for(i = 0; i < arrayLength; i++) {
newGrocery[i] = arr[i];
System.out.println("New item added" + i);
}
newGrocery[i+1] = new Groceries(name, price);
return newGrocery;
}
I have this method where I input an array containing 4 objects and it creates a new array of objects copying the previous 4 objects and then adding one more to the array. But I keep getting this exception.