-1

I'm having problems remembering how to fill an ArrayList and I can't seem to find how to do it. I want to use it to keep a HighScore. I currently have a Matrix for that, it looks like this:

if(score>record){
    System.out.println("Congrats, high score! Please type your name:");
    String name=sc.next();
    highScore[1][0] = name;
    highScore[0][0] = ("Player");
    record=score;
    highScore[0][1]=("Current");
    highScore[1][1]=(score+"");
    highScore[0][2]=("Highest");
    highScore[1][2]=(record+"");
}

I want to use a list because the display will be the same and I can add as many high scores as I want. If you can tell me how to do it, or any other method it would be great.

  • 3
    Maybe you should take a minute to read the [ArrayList](http://docs.oracle.com/javase/7/docs/api/java/util/ArrayList.html) documentation. – Jyr May 04 '15 at 19:05
  • I would have created a class to store highscore information ( probably singleton), and populated all the fields as required. Adding one method to update highscore() would not harm either on that class. – Jimmy May 04 '15 at 19:06
  • Jyr- I've taken a look at it, and from what i saw, it talks mainly about the methods and constructors, which I understand, my problem is when i do the list.add(); and write for example list.add("player"); it marks it wrong, i have no idea what to do. I'm a begginer. – Denisse Reyes May 04 '15 at 19:13
  • Jimmy, can you explain what you said you'd do? I'm a begginer, so I don't understand what you mean. – Denisse Reyes May 04 '15 at 19:14
  • So find out why it 'marks it wrong'. Did you create an ArrayList object? Did you specify the type? Your compiler gives you a clear error. Also, use @Username to address people in your comments, in that way they get notified. – Jyr May 04 '15 at 19:22
  • I did this: ArrayList list= new ArrayList(); list.add("Player"); It says "The method add(String[]) in the type ArrayList is not applicable for the arguments (String)" and I don't know how to write it in String[] I don't remember how to write it so it looks like a matrix. @Jyr – Denisse Reyes May 04 '15 at 19:34
  • You create an `ArrayList` of `String` array(s), but then you try to add a single `String`, which is not a `String` array. If you are trying to create a two-dimensional ArrayList, try `ArrayList>`. – Jyr May 04 '15 at 19:39

1 Answers1

0

As this image points out you allocate data to specific cells of your 2D array:

enter image description here

Assuming that:

highScore[p] is the player and [d] is the data (score, name etc.), then updating a specific players data should always look like this:

player = p;
data = d;

highScore[p][d] = data goes here; //in the above image row = p, column = d!

And updating multiple players could be done using a for loop.

better yet as mentioned in the comments you would be better creating a method and calling it with parameters p and d.

If you want to increase the size of your array that is impossible but you can move all of the data to a new array if it gets to large, is that what you where asking?

Daedric
  • 502
  • 4
  • 23
  • This 2D array you say, would be in a Matrix or in an ArrayList of String Arrays(ArrayList)? if it's on a matrix, how would they be updated because the matrix would be declared with an initial size [2][3] and i don't know how to add more rows and columns, i believe you can't, that's why i want a list. – Denisse Reyes May 04 '15 at 19:29
  • Sorry I got confused about what you were asking for but his should definitely help:http://stackoverflow.com/questions/21696784/how-to-declare-an-arraylist-with-values It also has a link to the java documentation regarding arrayLists – Daedric May 04 '15 at 19:34