Re: instantiating a object
Use a container. Java has a bunch of containers for different applications. If you just want a list of players, try using an ArrayList. If you want to associate each player with some "key" (for example, the player's name), use a HashMap.
Code Java:
ArrayList<Player> players = new ArrayList<Player>();
// add a few players. Note that you can add as many as you want
players.add(new Player());
players.add(new Player());
players.add(new Player());
For more on ArrayLists, see the JavaDoc: ArrayList (Java Platform SE 6)
Re: instantiating a object
would just adding a new player not overwrite the previous player created??? thats my current problem, it will create the one player object fine, but when you go and select add another player it overwrites it. so how would i let the program know that the next player is to be added to the list not overwriting the list. and sorry to sound stupid, but what's a hashmap? haven't learned about them yet, i don't think
Re: instantiating a object
You create new and unique objects rather than changing the properties of the same object.
Code java:
class Foo {
int value;
public void set(int v) {
value = v;
}
public int get() {
return value;
}
public static void main(String[] args) {
// incorrect
Foo[] fooList = new Foo[3];
Foo f = new Foo();
f.set(10);
fooList[0] = f;
f.set(20);
fooList[1] = f;
f.set(30);
fooList[2] = f;
print(fooList);
// correct
fooList = new Foo[3];
Foo f1 = new Foo();
f1.set(10);
fooList[0] = f1;
Foo f2 = new Foo();
f2.set(20);
fooList[1] = f2;
Foo f3 = new Foo();
f3.set(30);
fooList[2] = f3;
print(fooList);
}
public static void print(Foo[] arr) {
for(Foo f : arr) {
System.out.println(f.get());
}
System.out.println();
}
}
Re: instantiating a object
ok but suppose you don't know how many instances of Foo you need. say that fact is left up to the programs user. how would you allow for foo to be updated, also keeping in mind that the information being put in is unknown, other than what type of information you will be recieving.... i.e. string int double etc.....this is the part i'm stuck on. i could hard code in as many players as i deem necessary, but when the grader looks at the source and sees the program can only recieve say 5 players he will dock me points for having a falliable program. so i need to update player to an unknown number n and have it create each new player as n+1. and on top of that i have to be able to recall any of the players from the list at a given time and be able to remove that specific player from the list. these are the concepts i'm not finding in our book or in our notes.....
Re: instantiating a object
and thank you both for taking the time, i always hate to ask others for help when i don't understand stuff. makes me feel dumb >.<
Re: instantiating a object
"ok but suppose you don't know how many instances of Foo you need. say that fact is left up to the programs user"
You use a Collection to hold your objects as they can increase their size unlike an array. To stop getting user input you would use a flag. As soon as user enters the flag you stop looping.
"also keeping in mind that the information being put in is unknown, other than what type of information you will be recieving.... i.e. string int double etc"
This is impossible. You need to know what the input will be and in what format. Besides you seem to have it backwards. If your program needs to deal with people then you think about what characteristics a person has: name, address, date of birth etc. Then create a class to represent that data. Then your program prompts the user to input values to enable an object to be create. Adding some validation avoids you program crashing if user inputs junk. You don't do it the other way and try and create a class based on unknown input.
Re: instantiating a object
See the initial reply by helloworld.
Quote:
For more on ArrayLists, see the JavaDoc: ArrayList (Java Platform SE 6)
Arraylist is a dynamic array/list, it will expand as it needs to.