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.
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.
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
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.