-1

I have the following object:

public class Person {

    private String id;

    private Score[] scores;

    public Person() {
    }

    //getters and setters etc


}

I would like to create a method that adds another object into the Scores array.

I was planning to do so like below:

public void addScore(Score score){
    scores[0] = score;
}

Is this the best way to do so?

dumbPotato21
  • 5,669
  • 5
  • 21
  • 34
java123999
  • 6,974
  • 36
  • 77
  • 121
  • 1
    you have to update the index too... Also, I would prefer a `List` here. – dumbPotato21 May 26 '17 at 14:45
  • For such a requirement it would be better to use some List object instead of Array. If Array is a must then you need to manage its creation, current length, etc. properly. – Galcoholic May 26 '17 at 14:48

3 Answers3

4

Creating a setter method is a good idea. Yet somehow you'll have to keep track of the number of scores added into your list. By always assigning your set value to array index 0 you'll end up replacing first value over and over.

I suggest you use some List scores instead - you could then delegate the add to the list:

protected List<Score> scores = new ArrayList<Score>();

public void addScore(Score score) {
  scores.add(score)
} 

If you need to stick with an array, you have to keep one additional value of your last insert location like

protected int lastItem = 0;

protected Score[] scores = new Score[100]; //any initial size

public void addScore(Score score) {
  scores[lastItem++] = score;
  //check if you need to make the array larger
  //maybe copying elements with System.arraycopy()
}
Jan
  • 13,738
  • 3
  • 30
  • 55
  • he is not allowed to move away from array. read his previous question – XtremeBaumer May 26 '17 at 14:46
  • @XtremeBaumer it'd help if there were a link to his previous question. – Andy Turner May 26 '17 at 14:50
  • @AndyTurner here https://stackoverflow.com/questions/44204221/how-do-i-empty-a-primitive-array/44204290#44204290 – dumbPotato21 May 26 '17 at 14:53
  • I am getting an IndexOutOfBounds Exception using this code when I try to add more than 2 objects? Please see this Question: https://stackoverflow.com/questions/44305231/java-lang-arrayindexoutofboundsexception-when-adding-new-elements-to-an-array – java123999 Jun 01 '17 at 10:36
  • Did you make your initial Array large enough? Did you see my comments in code about checking length and making array larger on demand? – Jan Jun 01 '17 at 11:01
0

Either you store the current index of the array and you do like this:

private String id;

private Score[] scores;
private int index;

public Person()){
    index = 0;
    scores = new Score[10];
}
public void addScore(Score score){
    if(index<scores.length){
       scores[index] = score;
       index++;
    }else{
       Score[] temp = new Score[index+10];
       for(int i=0;i<scores.length;i++){
          temp[i] = scores[i];
       }
       scores = temp;
       scores[index] = score;
       index++;
    }
}

Or as someone already said, you use a list, that is basically an ADT that do something similar depending on which list you use.

GioGio
  • 448
  • 2
  • 5
  • 22
0

         You need to care the array size, if you still want to use it. First, you need to apply the array size when the space is not enough to put your value. Secondly, you need to copy the origin elements to new array.
         So,The way for using array is not the best way. As someone already said, the best way is to use list of java class. The list's size can grow dynamically and you don't need to care space.

YuMinghua
  • 1
  • 1