java - Fill ArrayList with objects fills with same values -


i trying fill empty arraylist "circles" objects circle random sizes , random locations, later on paint. for loop fills array normally, reason when copy circ.addall(circles) doesn't work. tried use .clone(), circ = circles,... ended either nullpoint exception error or circles having same values. code.

public class board { public static int size = 400; public static arraylist<circle> circles;  public static void fillarray(arraylist<circle> circ){     random r = new random();     int rand = r.nextint(10)+5;     circles = new arraylist<circle>(rand);     system.out.println("random ="+ rand);     for(int = 0; i< rand; i++){           circles.add(new circle(circle.x, circle.y,circle.size));          system.out.println(i+". "+ circles.get(i).x +" "+ circles.get(i).y +" size je "+ circles.get(i).size);         //fills normaly     }         circ.addall(circles);         system.out.println("aaaaa"+circ.get(0).x);         //why same values?      system.out.println("fillarray circles= " +circ.get(0).x+circ.get(0).y+ " "+circ.get(1).x); }  public static void main(string[] args) {     fillarray(circles); } 

my circle class looks this:

public class circle { public static int x,y; public static int size;  public circle(int x, int y, int size) {     this.x = randomloc();     this.y = randomloc();     this.size = randomsize(); } public static int randomsize (){     int min = 15;     int max = 30;     random r = new random();     int rand = r.nextint(max)+min;     return rand;  } public static int randomloc(){     int min = 12;     int max = board.size;     random r = new random();     int rand = r.nextint(max)+min;     return rand;  }} 

i trying fill empty arraylist "circles" objects circle random sizes , random locations

but you're not doing that. line

circles.add(new circle(circle.x, circle.y,circle.size)); 

adds circle static field values. presumably, circle class has this:

public class circle {      public static int x, y, size; // perhaps initialized values      public circle(int x, int y, int size) { /* ... */ } } 

so add same values circles in list. randomize size , location need use random instance created. like:

circles.add(new circle(r.nextint(10)+5, r.nextint(10)+5, r.nextint(10)+5)); 

for loop fills array normally, reason when copy circ.addall(circles) doesn't work.

you confusing 2 lists created - circ , circles. passing reference arraylist<circle> circles method, named circ inside method scope. redundant since can access static circles within method without passing argument. suggest solve design issues before else.

what want initialize circ:

circ = new arraylist<circle>(); 

and note argument passed constructor initial capacity, performance parameter, , shouldn't random.

once that, line circ.addall(circles); meaningless , should removed. print circ see values (@override circle's tostring).

note: it's recommended use interface , not implementation hold reference: list<circle> list = new arraylist<>();. shouldn't care implementation details when list operations.


Comments

Popular posts from this blog

networking - Vagrant-provisioned VirtualBox VM is not reachable from Ubuntu host -

c# - ASP.NET Core - There is already an object named 'AspNetRoles' in the database -

ruby on rails - ArgumentError: Missing host to link to! Please provide the :host parameter, set default_url_options[:host], or set :only_path to true -