Results 1 to 13 of 13
Like Tree5Likes
  • 1 Post By Tolls
  • 1 Post By Tolls
  • 1 Post By quad64bit
  • 1 Post By Tolls
  • 1 Post By quad64bit

Thread: Storing object with int values in Arraylist.

  1. #1
    Zigster is offline Member
    Join Date
    Apr 2012
    Posts
    74
    Rep Power
    0

    Default 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); 
        }
    }

  2. #2
    Tolls is offline Moderator
    Join Date
    Apr 2009
    Posts
    11,450
    Rep Power
    18

    Default 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.
    Zigster likes this.
    Please do not ask for code as refusal often offends.

  3. #3
    Zigster is offline Member
    Join Date
    Apr 2012
    Posts
    74
    Rep Power
    0

    Default 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?

  4. #4
    Tolls is offline Moderator
    Join Date
    Apr 2009
    Posts
    11,450
    Rep Power
    18

    Default Re: Storing object with int values in Arraylist.

    I'd write a no-args constructor:
    Java 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.
    Zigster likes this.
    Please do not ask for code as refusal often offends.

  5. #5
    Zigster is offline Member
    Join Date
    Apr 2012
    Posts
    74
    Rep Power
    0

    Default 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

  6. #6
    quad64bit's Avatar
    quad64bit is offline Moderator
    Join Date
    Jul 2009
    Location
    VA
    Posts
    1,323
    Rep Power
    6

    Default Re: Storing object with int values in Arraylist.

    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:
    Java 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:

    Java Code:
    //...
    Gladiators glads = new Gladiators();
    glads.setAxe(7);
    System.out.println(glads.getSword());
    //...
    Zigster likes this.

  7. #7
    Tolls is offline Moderator
    Join Date
    Apr 2009
    Posts
    11,450
    Rep Power
    18

    Default 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.
    quad64bit likes this.
    Please do not ask for code as refusal often offends.

  8. #8
    quad64bit's Avatar
    quad64bit is offline Moderator
    Join Date
    Jul 2009
    Location
    VA
    Posts
    1,323
    Rep Power
    6

    Default Re: Storing object with int values in Arraylist.

    Agreed!

  9. #9
    Zigster is offline Member
    Join Date
    Apr 2012
    Posts
    74
    Rep Power
    0

    Default Re: Storing object with int values in Arraylist.

    If I am adding Gladiators to an 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));
    		}
    	}
    }
    How do I access the ints of each object in the arraylist?

  10. #10
    j2me64's Avatar
    j2me64 is offline Senior Member
    Join Date
    Sep 2009
    Location
    Zurich, Switzerland
    Posts
    962
    Rep Power
    5

    Default Re: Storing object with int values in Arraylist.

    Quote Originally Posted by Zigster View Post
    How do I access the ints of each object in the arraylist?
    Do you read the answers?

    Quote Originally Posted by Tolls View Post
    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.

  11. #11
    Zigster is offline Member
    Join Date
    Apr 2012
    Posts
    74
    Rep Power
    0

    Default 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?

  12. #12
    Zigster is offline Member
    Join Date
    Apr 2012
    Posts
    74
    Rep Power
    0

    Default 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());
    		
    	}
    }
    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;
        }
        
    }
    It prints:
    Recruit: 1 Strength: 4 Agility: 2 Stamina: 9 Speed: 1
    Recruit: 2 Strength: 9 Agility: 11 Stamina: 3 Speed: 13
    Str: 4
    Str: 9

  13. #13
    quad64bit's Avatar
    quad64bit is offline Moderator
    Join Date
    Jul 2009
    Location
    VA
    Posts
    1,323
    Rep Power
    6

    Default Re: Storing object with int values in Arraylist.

    Glad we could help!
    Zigster likes this.

Similar Threads

  1. problem with storing the returned values in an array
    By amrmb09 in forum Advanced Java
    Replies: 1
    Last Post: 11-26-2011, 01:17 AM
  2. Replies: 2
    Last Post: 11-06-2011, 07:19 PM
  3. XML read and storing values
    By LiveStrong-2011 in forum New To Java
    Replies: 1
    Last Post: 05-31-2011, 11:04 AM
  4. Replies: 2
    Last Post: 10-28-2010, 02:33 PM
  5. question on how to check values before storing
    By SMHouston in forum New To Java
    Replies: 19
    Last Post: 09-21-2009, 10:54 PM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •