Storing object with int values in Arraylist.
I have made this class to create the stats of game characters from random numbers. But if I store the object in an ArrayList, everytime I reference the object the stats re-randomise. How can I make it so the stats just randomise once, and referencing the object always gives the same stats that it made the first time? Thanks.
Code:
package Main;
import java.util.ArrayList;
import java.util.List;
public class Main {
public static void main (String[] args){
new DrawArena();
List<Gladiators> cars= new ArrayList<Gladiators>();
for (int i= 0; i < 2; i++){
cars.add(new Gladiators());
System.out.println("Recruit: " + (i+1) + " \t" + cars.get(i));
System.out.println(cars.get(i));
}
}
}
Code:
package Main;
import java.util.Random;
public class Gladiators {
Random generator = new Random();
int roll = generator.nextInt(100) + 1;
int str;
int agility;
int stamina;
int speed;
int price;
int check;
int sword;
int shield;
int spear;
int bow;
int mace;
int mstar;
int axe;
public static void main(String[] args){
}
public String getStats(int roll){
if (roll<=40 && roll>0){
str = generator.nextInt(10) + 1;
agility = generator.nextInt(10) + 1;
stamina = generator.nextInt(10) + 1;
speed = generator.nextInt(10) + 1;
return "Strength: " + str + "\tAgility: " + agility + "\tStamina: " + stamina + "\tSpeed: " + speed + "\tPrice: " + getPrice();
}
if (roll<=70 && roll>40){
str = generator.nextInt(15) + 1;
agility = generator.nextInt(15) + 1;
stamina = generator.nextInt(15) + 1;
speed = generator.nextInt(15) + 1;
return "Strength: " + str + "\tAgility: " + agility + "\tStamina: " + stamina + "\tSpeed: " + speed + "\tPrice: " + getPrice();
}
if (roll<=100 && roll>70){
str = generator.nextInt(20) + 1;
agility = generator.nextInt(20) + 1;
stamina = generator.nextInt(20) + 1;
speed = generator.nextInt(20) + 1;
return "Strength: " + str + "\tAgility: " + agility + "\tStamina: " + stamina + "\tSpeed: " + speed + "\tPrice: " + getPrice();
}
return null;
}
public int getPrice(){
if ((str + agility + stamina + speed) <= 30 && (str + agility + stamina + speed)>0){
double x = generator.nextDouble() * 100.0 + 1.0;
int price = (int)x;
return price;
}
if ((str + agility + stamina + speed <= 50) && (str + agility + stamina + speed > 30)){
double x = generator.nextDouble() * 200.0 + 100.0;
int price = (int)x;
return price;
}
if ((str + agility + stamina + speed) <= 65 && (str + agility + stamina + speed > 50)){
double x = generator.nextDouble() * 300.0 + 200.0;
int price = (int)x;
return price;
}
else if ((str + agility + stamina + speed <= 80)){
double x = generator.nextDouble() * 1000.0 + 500.0;
int price = (int)x;
return price;
}
return 0;
}
public String toString(){
return getStats(roll);
}
}
Re: Storing object with int values in Arraylist.
Because you only calculate your attributes in the getStats method, which regenerates them everytime it is called.
I would suggest using the constructor for generating the stats for the character, get rid of the getStats method (or call that from the constructor), and change the toString to simply construct a String containing the attributes for the character.
Re: Storing object with int values in Arraylist.
I want each Gladiator instance to have a different set of random stats/attributes (str, agility, stamina, speed), so I can add the gladiator objects to an arraylist, and then retrieve the info for each of the gladiators stats later.
@Tolls: How do I use the constructor to generate the stats? Which constructor are you talking about?
Re: Storing object with int values in Arraylist.
I'd write a no-args constructor:
Code:
public Gladiators () {
// either generate the attributes here or simple call getStats().
}
That way when you create a new Gladiator it will have all it's attributes set.
Re: Storing object with int values in Arraylist.
Is this what you mean? How do I access the stats from outside this class? Cheers.
Code:
package Main;
import java.util.Random;
public class Gladiators {
Random generator = new Random();
int roll = generator.nextInt(100) + 1;
int x = roll;
int stat;
int price;
int sword;
int shield;
int spear;
int bow;
int mace;
int mstar;
int axe;
public Gladiators () {
int str = getStat(x);
int agility = getStat(x);
int stamina = getStat(x);
int speed = getStat(x);
//System.out.println("Strength: " + str + "\tAgility: " + agility + "\tStamina: " + stamina + "\tSpeed: " + speed);
}
public int getStat(int roll){
if (roll<=40 && roll>0){
stat = generator.nextInt(10) + 1;
return stat;
}
if (roll<=70 && roll>40){
stat = generator.nextInt(15) + 1;
return stat;
}
if (roll<=100 && roll>70){
stat = generator.nextInt(20) + 1;
return stat;
}
return 0;
}
}
Quote:
Recruit: 1 Main.Gladiators@36b98809
Main.Gladiators@36b98809
Recruit: 2 Main.Gladiators@4e9222f0
Main.Gladiators@4e9222f0
784
784
784
Re: Storing object with int values in Arraylist.
Quote:
How do I access the stats from outside this class?
There are two main ways. If all your stats are public or (package-private in your case), then other classes can access them directly (with the exception of classes outside your 'Main' package for package-private scope). So, from outside the Gladiators code, you would create an instance of Gladiator, like so:
Code:
//...
Gladiators glads = new Gladiators();
//and from there, you can access fields:
glads.sword = 7;
System.out.println(glads.bow);
//...
... and so on.
However, often it is not desirable to leave fields public (in object oriented programming), in which case you can just make accessor methods for any fields for which you'd like access. You would access them in a similar manner to above, but you would reference the accessor method you write instead of the field, so something like:
Code:
//...
Gladiators glads = new Gladiators();
glads.setAxe(7);
System.out.println(glads.getSword());
//...
Re: Storing object with int values in Arraylist.
And since you're trying to print the Gladiator objects you ought to do the last part of my original post and write a toString method that returns the attributes in a format to print somewhere.
Re: Storing object with int values in Arraylist.
Re: Storing object with int values in Arraylist.
If I am adding Gladiators to an ArrayList:
Code:
package Main;
import java.util.ArrayList;
import java.util.List;
public class Main {
public static void main (String[] args){
new DrawArena();
List<Gladiators> gladiators= new ArrayList<Gladiators>();
for (int i= 0; i < 2; i++){
gladiators.add(new Gladiators());
System.out.println("Recruit: " + (i+1) + " \t" + gladiators.get(i));
System.out.println(gladiators.get(i));
}
}
}
How do I access the ints of each object in the arraylist?
Re: Storing object with int values in Arraylist.
Quote:
Originally Posted by
Zigster
How do I access the ints of each object in the arraylist?
Do you read the answers?
Quote:
Originally Posted by
Tolls
And since you're trying to print the Gladiator objects you ought to do the last part of my original post and write a toString method that returns the attributes in a format to print somewhere.
Re: Storing object with int values in Arraylist.
Yes, I read the posts, I don't believe that actually answers my question. I didn't ask how to print out a string that contains the ints (my original code already does this), I want to access the ints so I can use them elsewhere. If I have an object in an arraylist, and I want to use a specific int stored in that object, say I want to compare int attack from object gladiator at 0 in the arraylist to the int defence of object gladiator at 1 in the arraylist, something like that, how can I do it?
Re: Storing object with int values in Arraylist.
Thankyou quad64bit and Tolls, especially for your examples, took me awhile to understand what you were doing, but it's exactly what I wanted.
Code:
package Main;
import java.util.ArrayList;
import java.util.List;
public class Main {
public static void main (String[] args){
new DrawArena();
List<Gladiators> gladiators= new ArrayList<Gladiators>();
for (int i= 0; i < 2; i++){
gladiators.add(new Gladiators());
System.out.println("Recruit: " + (i+1) + " \t" + gladiators.get(i));
}
System.out.println("Str: " + gladiators.get(0).str);
System.out.println("Str: " + gladiators.get(1).getStr());
}
}
Code:
package Main;
import java.util.Random;
public class Gladiators {
Random generator = new Random();
int roll = generator.nextInt(100) + 1;
int x = roll;
int stat;
int str, agility, stamina, speed;
int price;
int sword;
int shield;
int spear;
int bow;
int mace;
int mstar;
int axe;
public Gladiators () {
str = getStat(x);
agility = getStat(x);
stamina = getStat(x);
speed = getStat(x);
}
public int getStat(int roll){
if (roll<=40 && roll>0){
stat = generator.nextInt(10) + 1;
return stat;
}
if (roll<=70 && roll>40){
stat = generator.nextInt(15) + 1;
return stat;
}
if (roll<=100 && roll>70){
stat = generator.nextInt(20) + 1;
return stat;
}
return 0;
}
public int getStr(){
return str;
}
public int getStamina(){
return stamina;
}
public int getSpeed(){
return speed;
}
public int getAgility(){
return agility;
}
public String toString(){
return "Strength: " + getStr() + "\tAgility: " + agility + "\tStamina: " + stamina + "\tSpeed: " +speed;
}
}
It prints: Quote:
Recruit: 1 Strength: 4 Agility: 2 Stamina: 9 Speed: 1
Recruit: 2 Strength: 9 Agility: 11 Stamina: 3 Speed: 13
Str: 4
Str: 9
Re: Storing object with int values in Arraylist.