bluej - How do you make an Arraylist of RandomNumber integers? java -


so relatively new programming scene , confused why code doesn't work. trying make arraylist of flowers , use random number generator create random number of flowers, , store them in array. in logic, thought created variable store numbers (ex randomroses) , stored number in array print out how many of each flower there calling arraylist , position. (ex flowerarray[0] print out 8 roses) sadly not.

public class flower {      private int randomroses;     private int randomtulips;     private int randomorchids;     public arraylist <integer> flowerarray;      public flower()     {         r = new random();         t = new random();         o = new random();         int randomroses = (r.nextint(10) + 0);         int randomtulips = (t.nextint(10) + 0);         int randomorchids = (o.nextint(10) + 0);         flowerarray = new arraylist<integer>     }      public void add2array ()     {         flowerarray.add(randomroses); //flowerarray[0] # of roses         flowerarray.add(randomtulips); //flowerarray[1] # of tulips         flowerarray.add(randomorchids); //flowerarray[2] # of orchids     }      public void printarray()     {         system.out.println(flowerarray[0]);     } 

you can use same random object, no need create 3 instances of random integer generation,

random r = new random(); (int = 0; < 3; i++) {     flowerarray.add(r.nextint(10)); } system.out.println(flowerarray); 

you can not flowerarray[0] because have arraylist , not array. can instead do: flowerarray.get(0) getting integer @ pos zero


Comments

Popular posts from this blog

java - Run spring boot application error: Cannot instantiate interface org.springframework.context.ApplicationListener -

python - pip wont install .WHL files -

Excel VBA "Microsoft Windows Common Controls 6.0 (SP6)" Location Changes -