Results 1 to 13 of 13
5Likes Thread: Storing object with int values in Arraylist.
- 05-25-2012, 05:53 AM #1
Member
- Join Date
- Apr 2012
- Posts
- 74
- Rep Power
- 0
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.
Java 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)); } } }Java 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); } }
- 05-25-2012, 09:55 AM #2
Moderator
- Join Date
- Apr 2009
- Posts
- 10,484
- Rep Power
- 16
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.Please do not ask for code as refusal often offends.
- 05-25-2012, 10:23 AM #3
Member
- Join Date
- Apr 2012
- Posts
- 74
- Rep Power
- 0
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?
- 05-25-2012, 10:34 AM #4
Moderator
- Join Date
- Apr 2009
- Posts
- 10,484
- Rep Power
- 16
Re: Storing object with int values in Arraylist.
I'd write a no-args constructor:
That way when you create a new Gladiator it will have all it's attributes set.Java Code:public Gladiators () { // either generate the attributes here or simple call getStats(). }Please do not ask for code as refusal often offends.
- 05-25-2012, 02:40 PM #5
Member
- Join Date
- Apr 2012
- Posts
- 74
- Rep Power
- 0
Re: Storing object with int values in Arraylist.
Is this what you mean? How do I access the stats from outside this class? Cheers.
Java 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; } }Recruit: 1 Main.Gladiators@36b98809
Main.Gladiators@36b98809
Recruit: 2 Main.Gladiators@4e9222f0
Main.Gladiators@4e9222f0
784
784
784
- 05-25-2012, 02:59 PM #6
Re: Storing object with int values in Arraylist.
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:How do I access the stats from outside this class?
... and so on.Java Code://... Gladiators glads = new Gladiators(); //and from there, you can access fields: glads.sword = 7; System.out.println(glads.bow); //...
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:
Java Code://... Gladiators glads = new Gladiators(); glads.setAxe(7); System.out.println(glads.getSword()); //...
- 05-25-2012, 03:54 PM #7
Moderator
- Join Date
- Apr 2009
- Posts
- 10,484
- Rep Power
- 16
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.
Please do not ask for code as refusal often offends.
- 05-25-2012, 06:50 PM #8
Re: Storing object with int values in Arraylist.
Agreed!
- 05-26-2012, 04:02 AM #9
Member
- Join Date
- Apr 2012
- Posts
- 74
- Rep Power
- 0
Re: Storing object with int values in Arraylist.
If I am adding Gladiators to an ArrayList:
How do I access the ints of each object in the arraylist?Java 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)); } } }
- 05-26-2012, 07:33 AM #10
Re: Storing object with int values in Arraylist.
- 05-26-2012, 09:51 AM #11
Member
- Join Date
- Apr 2012
- Posts
- 74
- Rep Power
- 0
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?
- 05-26-2012, 04:36 PM #12
Member
- Join Date
- Apr 2012
- Posts
- 74
- Rep Power
- 0
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.
Java 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()); } }It prints:Java 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; } }Recruit: 1 Strength: 4 Agility: 2 Stamina: 9 Speed: 1
Recruit: 2 Strength: 9 Agility: 11 Stamina: 3 Speed: 13
Str: 4
Str: 9
- 05-26-2012, 09:49 PM #13
Similar Threads
-
problem with storing the returned values in an array
By amrmb09 in forum Advanced JavaReplies: 1Last Post: 11-26-2011, 01:17 AM -
Storing objects into an arraylist in a different class?
By tepichi09 in forum New To JavaReplies: 2Last Post: 11-06-2011, 07:19 PM -
XML read and storing values
By LiveStrong-2011 in forum New To JavaReplies: 1Last Post: 05-31-2011, 11:04 AM -
Problem in storing Socket class object in ArrayList array
By Anoop in forum New To JavaReplies: 2Last Post: 10-28-2010, 02:33 PM -
question on how to check values before storing
By SMHouston in forum New To JavaReplies: 19Last Post: 09-21-2009, 10:54 PM


LinkBack URL
About LinkBacks
Reply With Quote

Bookmarks