0

I have a class that implements an interface and I think when I try to insert an element into the array more than once the first insertions are forgotten. I really can't figure this one out. This is what I have:

public void insertElementAt(int index, E el)
                 throws IllegalArgumentException {

    Object temp[] = new Object[data.length + 1];
    for (int i = 0; i < data.length; i++) {
        if (i == index){
            temp[index] = el;
            temp[i + 1] = data[i];
            i++;
        }

        temp[i] = data[i];
    }

    data = temp;

    if (index > data.length || index < 0) {
        throw new IllegalArgumentException();
    }
}

Then my test reports null instead of first at he last assertion.

@Test
public void testInsertToLeft() {
    PriorityList<String> list = new ArrayPriorityList<String>();
    list.insertElementAt(0, "First");
    // Must shift array elements in this case
    list.insertElementAt(0, "New First");

    assertEquals("New First", list.getElementAt(0));
    assertEquals("First", list.getElementAt(1));
}
4
  • 1
    Looks like you need two indexes - one for the old array and one for the new array. As it stands, when you skip along one element because of the insert you skip along both arrays. P.S. just use a List. Commented Oct 8, 2013 at 20:31
  • it seems you need a continue at the end of if block. Commented Oct 8, 2013 at 20:33
  • What is the first size of data? Commented Oct 8, 2013 at 20:37
  • Is there any reason you can't use an ArrayList= Commented Oct 8, 2013 at 21:39

5 Answers 5

1

You must do like this:

public void insertElementAt(int index, E el) throws IllegalArgumentException {

    Object temp[] = new Object[data.length + 1];
    for (int i = 0; i < data.length; i++) {
        if (i >= index){
            temp[i + 1] = data[i];
        } else {
            temp[i] = data[i];
        }
    }
    temp[index] = el;
    data = temp;

    if (index > data.length || index < 0) {
        throw new IllegalArgumentException();
    }
}

To remove it:

public void removeElementAt(int index) throws IllegalArgumentException {

    Object temp[] = new Object[data.length - 1];
    for (int i = 0; i < temp.length; i++) {
        if (i > index){
            temp[i - 1] = data[i];
        } else {
            temp[i] = data[i];
        }
    }
    data = temp;

    if (index > data.length || index < 0) {
        throw new IllegalArgumentException();
    }
}
Sign up to request clarification or add additional context in comments.

7 Comments

I figured there was an easy fix without changing too much of his existing code (But I couldn't seem to figure it out), nice answer +1.
perfect. would I do something similar to remove an element out of the array and shift it down?
you are awesome. does this similar concept apply when taking an element out and moving it to the end and shifting everything down? also taking an element out and putting it at the start and moving everything up?
It would be something, but considering that in this case both arrays keep the same size ;-)
@Tejas, If my answer has been helpful to you, please do not forget to mark it as correct. This may be useful for other developers ;-) Thanks!
|
0

Try changing your for loop to either:

for (int i = 0; i < data.length; i++) {
            if (i == index){
                temp[index] = el;
                temp[i + 1] = data[i];
                i++;
            }else{
                temp[i] = data[i];
            }

}

or

for (int i = 0; i < data.length; i++) {
        if (i == index){
            temp[index] = el;
            temp[i + 1] = data[i];
            i++;
            continue;
        }
        temp[i] = data[i];
    }

Comments

0

What is data.length when the list is empty? If it is empty at first insertion you will not enter the for loop but copy the temp array, it will go into the loop at next insertion with length 1. The first insertion will be skipped.

Comments

0

I would do this:

public static void insertElementAt(int index, E el)
    throws IllegalArgumentException {

if (index > data.length || index < 0) {
    throw new IllegalArgumentException();
}

Object temp[] = new Object[data.length + 1];
for (int i = index; i < data.length; i++) {
    temp[i+1] = data[i];
}
temp[index] = el;
data = temp;

}

Comments

0

You should have the test for valid parameters first ("fail early"), and you can make good use of the JDK's utility methods do the lifting for you:

public static void insertElementAt(int index, E el) {
    if (index > data.length || index < 0) {
        throw new IllegalArgumentException();
    }

    data = Arrays.copyOf(data, data.length + 1);
    System.arrayCopy(data, index, data, index + 1, data.length - index);
    data[index] = el;
}

Also note you don't need to declare a throws, becauseIllegalArgumentException is an unchecked exception, so I removed it. Normally, one follows this pattern.

Comments

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.