3 Replies Latest reply: Mar 6, 2013 8:12 PM by J2EE_Life RSS

    Trying to create a loop that stores x variables of a class I created

    970521
      I created the class Dice. To create a new Dice with 6 sides, I do
       Dice name = new Dice(6); 
      Next, I have the class DiceN which is supposed to create N dices and store them inside an array or ArrayList. The constructor of DiceN´s takes in a number n that the constructor uses to create n dices. This is what I´d like the class DiceN to be like:
      import java.util.*;  // to use ArrayList
      
      public class DiceN {
        
        ArrayList dices = new ArrayList(); // holds all the dices the constructor will construct
        
        public DiceN( int n ) { // the constructor that creates n dices
          for ( int i = 0; i < n; i++ ) {
            Dice x_i = new Dice(6); // if n = 3, the variables x_0, x_1 and x_2 will be created: 3 dices with 6 sides each
            
            dices.add(x_i); // each time a dice is created it is stored in the arrayList for further usage
          }
        }
        
      }
      Obviously, one can´t dynamically create instances of Dice by using the code in the for loop, but how can I create n many dices and store them into the ArrayList, or even better, in an array without knowing beforehand how many dices will be constructed?
        • 1. Re: Trying to create a loop that stores x variables of a class I created
          gimbal2
          How exactly is your current code not doing what you want? Forget about variables, you want to create 'n' dices which is what you are doing now. After this code runs, the arraylist wil have 'n' dices in it.

          BTW: stick to the New to Java forum when you are in the learning phase. That way people know to not make any assumptions about your current skill level and put in extra effort to guide you.
          • 2. Re: Trying to create a loop that stores x variables of a class I created
            J2EE_Life
            Dice x_i = new Dice(6); // if n = 3, the variables x_0, x_1 and x_2 will be created: 3 dices with 6 sides each
            Offcourse instances with names like x_0, x_1 and x_2 are not created here. But Different instances are indeed created, each time having same name x_i ( there is no relation between 'i' in x_i and 'i' in for loop, so x_0, x_1, etc won't be created).
            Here new Dice(6) will each time create new instances, so it serves the purpose as required by you.

            For your better understanding try running below sample code and see value of 'a' is different for each instance because they indeed different instances stored in ArrayList:
            import java.util.ArrayList;
            public class DiceCheck
            {
                 public static void main(String[] args)
                 {
                      DiceN diceN = new DiceN(3);
                      diceN.showDices();
                 }
            }
            
            class Dice
            {
                 int sides;
                 int a=0;
                 public Dice(int sides)
                 {
                 this.sides = sides;
                 }
                 public int getA()
                 {
                      return a;
                 }
                 public void setA(int a)
                 {
                      this.a = a;
                 }
            }
            
            class DiceN
            {
                 ArrayList dices = new ArrayList();
                 
                 public DiceN(int n)
                 {
                      for(int i=0;i<n;i++)
                      {
                           Dice x_i = new Dice(6);
                           x_i.setA(i);
                           dices.add(x_i);
                      }
                      
                 }
                 
                 public void showDices()
                 {
                      for(int i=0;i<dices.size();i++)
                      {
                           Dice x_i = (Dice)dices.get(i);
                           System.out.println(x_i.getA());
                      }
                 }
            }
            • 3. Re: Trying to create a loop that stores x variables of a class I created
              J2EE_Life
              Isn't your question answered or you need some more help?