0

Overall Goal: Add the provided item to the array. You will need to increase the array size and add the new item to the end of the list. If the name already exists (case does not matter) don't add the item. No External Classes Permitted to Be Used in This Method Apart From String.toUpperClass and String.Equals.

In order to do this I have written the following:

public int addItem(Item newItem) {
    itemList = Arrays.copyOf(itemList, +1);
    int done=-1;
    for (int i=0;i<itemList.length;i++){
        itemList[i]=newItem;
        done=(i+1);
    }
    return done;
}

To test this section of code I am using a JUnit tester which fails with the following exception:

error: AddItem: Put a single item in and make sure its in the right place: expected:<[item2]> but was <[item1]>.

Blake
  • 35
  • 5
  • 3
    Java != javascript – Jens Sep 20 '18 at 08:01
  • 1
    You want to replace every item with the new Item? – Jens Sep 20 '18 at 08:03
  • No, the provided item in this scenario is `newItem`, if `itemList` has a size of `0`, how would I increase its size? – Blake Sep 20 '18 at 08:04
  • I don't think you can get an ArrayIndexOutOfBoundsException from this code. `i` looks to only have valid values. – Thilo Sep 20 '18 at 08:06
  • 1
    @Blake You can not. You have to devine a new Array and copy the old content – Jens Sep 20 '18 at 08:07
  • 1
    So I can define a new array `Item[] newList = new Item[itemList.length + 1];` Correct me if I am wrong but this initiates and increases the array by 1. How would I copy old content. – Blake Sep 20 '18 at 08:09
  • You can use `itemList = java.util.Arrays.copyOf(itemList, newLength)` to get an array with adjusted length. – Thilo Sep 20 '18 at 08:09
  • 1
    The method `System.arraycopy` copies elements from one array to another (or even within the same array...). But since you're not allowed to use it (nor, presumably, `Arrays.copyOf` either), you'll have to write a loop that copies the elements one-by-one from the old array into the new array. – Kevin Anderson Sep 20 '18 at 08:16
  • A number of problems here. 1) You are currently not checking for equality. Find out what it means for one item to be equal to another. Is it when the two item references are equal, or when they are different instances but content is the same? 2) How should the array size be increased? Just one more space added to add the new element, or should the array size be doubled/tripled etc? You will have to a) create a new array b) copy all contents over c) add new element to end of new array – mdewit Sep 20 '18 at 08:17
  • Ok so I have managed to get the code running but it only passes 1/10 tests. The issue it now has is the test software is expecting a different item than the one that exists within the array. – Blake Sep 20 '18 at 08:20
  • Use `List` for that . – Antoniossss Sep 20 '18 at 08:25
  • Use `List` for what? sorry! – Blake Sep 20 '18 at 08:33

1 Answers1

0

so in this for-loop you copy items from itemList to the newList

Item[] newList = new Item[itemList.length + 1];



 for(int i = 0; i<itemList.length; i++){
    newList[i] = itemList[i];
    }

you will have 1 more space that the original itemList.

I would recommend using ArrayList, if you want an array from arrayList.

ArrayList.toArray( T[] a )

the reply here could help you: How to add new elements to an array?