-3

I'm looking for a way in java (not sql), to store a few inputs. And be able to refer each entry.

So for example the Id of person entering info, date entered, a score of some kind (int).

All I can find is a multi-dimensional array.

Brian Tompsett - 汤莱恩
  • 5,753
  • 72
  • 57
  • 129
Kyle Harris
  • 137
  • 1
  • 8
  • 3
    Voting to close as it is too broad. I suggest read http://stackoverflow.com/help/how-to-ask – StackFlowed Nov 24 '15 at 19:56
  • 3
    Dedicate some time to:[Java Data Structures Reference](http://stackoverflow.com/questions/1274711/java-data-structures-reference) – Perdomoff Nov 24 '15 at 19:57
  • 2
    Why don't you create your own class with fields you want to store? You can halter create array or list of instances of your class. – Pshemo Nov 24 '15 at 19:59

2 Answers2

1

A potential solution is to create a class for the data you want, for example:

public class Data {
    private int score;
    private String info, date;

    public Data(String info, String date, int score) {
        this.score = score;
        this.info = info;
        this.data = data;
    }
}

Now you can create these objects with the data that the user inputs, and add them to a collection (i.e an ArrayList) or simply to an array. Here's an example with an array:

Data[] myData = new Data[4]; // 4 is an arbitrary maximum number of entries 

// Get the data into variables

// Now create a new Data object with the information and add it to the array
myData[0] = new Data(info, date, score);

// Repeat this process for all the input
Bimde
  • 722
  • 8
  • 20
  • Really good thank you. – Kyle Harris Nov 24 '15 at 20:20
  • TO ANYONE WHO COMES ACROSS THIS. What i was looking for at the time and now have come across is an ARRAY OF OBJECTS. you can create say a person and then have an array of person objects each with their own name etc. accessed via arrayname[1].getname(). This is what all these so called experts have mentioned. – Kyle Harris Dec 11 '15 at 19:14
1

What you are looking for can be done in many ways, the most accessible and understandable one probably being a List or ArrayList.

You seem to want to keep a list of Players, each having a few attributes. You could start by creating a Player object, like so

Class Player {
  Public Player(int score, String name) {
  this.score = score;
  this.name = name;

}

  private int score;
  private String name; 

 public void setScore(int score){
   this.score = score; 
 }

public int getScore(){
  return this.score; 
} 
// Repeat for all class variables

Then, you would declare an ArrayList in one of your other classes. Note that it has the parameter type Player.

ArrayList<Player> playerList = new ArrayList<Player>(); 

You can then add Player objects to the playerList like so:

Player p = new Player(345, "randomName"); 
playerList.add(p); 

If you then want to access one of your Players, and one of their scores specifically, you would do this:

int currentPlayerScore = playerList.get(0).getScore(); 

Naturally, 0 here stands for the number of the ArrayList entry.

Good luck.

Jochem Kleine
  • 186
  • 1
  • 10