3

I am trying to insert an element in an array. My method is supposed to increase the the size of the array by one and insert the element in the proper spot. The issue: It adds the element in the proper spot and extends the array, but it gets rid of the original element in the spot and inserts a null. My code:

 public void insert(int point, Person person){
    Person [] newList = new Person[this.size()+1];
    for(int i = 0; i < point; i++){
      newList[i] = list[i];
    }
    newList[point] = person;

    for(int i = point+1; i<this.size(); i++){
      newList[i] = list[i];

    }
   this.list = new Person[this.size()+1];
    for(int i = 0; i <this.size(); i++){
      list[i] = newList[i];
    }
  }

The array output:

> FBArrayList name = new FBArrayList()
 [DrJava Input Box]
> name.list[0] = new Person("Emma", 7)
Person@20a3d02a
> name.list[1] = new Person("Daniel", 8)
Person@6e8a93de
> name.list[2] = new Person("Bradley", 9)
Person@327556d1
> name.list[3] = new Person("Monica", 1)
Person@3d886c83
> name.list[4] = new Person("Connor", 2)
Person@76b41f9c
> name.list[5] = new Person("Fedor", 3)
Person@462a5d25
> name.insert(3, new Person("David", 4))
> for(int i = 0; i<7; i++){
System.out.println(name.list[i].getName());
}
Emma
Daniel
Bradley
David
Connor
Fedor
java.lang.NullPointerException
> name.list
{ Person@20a3d02a, Person@6e8a93de, Person@327556d1, Person@1d1a8b9, Person@76b41f9c, Person@462a5d25, null }

Any suggestions about why I am losing Monica or how I might go about fixing it.

2
  • Random thought - instead of recreating list at the end and copying over the contents of newList, why not just do list = newList? Commented Feb 7, 2014 at 3:48
  • That is a good question. I am not sure why I'm not doing it that way. Thanks @Chris Commented Feb 7, 2014 at 5:00

2 Answers 2

4
public void insert(int point, Person person){
  Person [] newList = new Person[this.size()+1];
  for(int i = 0; i < point; i++){
    newList[i] = list[i];
  }
  newList[point] = person;

  // this part copies the remainder of the original list to the new list after
  // your inserted entry
  for(int i = point; i < this.size(); i++){
    newList[i+1] = list[i];

  }

  this.list = newList;
}
Sign up to request clarification or add additional context in comments.

1 Comment

Thx for adding the clarification on where the fix is exactly and why it fixes it :)
-1

You did a:

name.insert(3, new Person("David", 4))

which inserted "David" in position 3 and overwrote "Monica"!

7 Comments

But it's supposed to insert, not replace...how should it be fixed?
If I am understanding you correctly: am I overwriting Monica before I move it in to the next array?
Hi @Chris. Thanks for commenting, you bring up a good point. Can the author give us more information on what the FBArrayList() is and how the insert method works. Maybe this is just a standard Java ArrayList; is that correct?
Hi @user3242955. That's what it looks like when you do the name.insert(3.... But I'm not sure how the FBArrayList() is implemented...
Hi @user3242955. Jason's answer is the one you should use!
|

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.