Search code examples
javaclassobjectarraylistinstance

How to create multiple object in loop to keep remember each device uniquely


Creating objects something like this, that all unique objects can be used many ways for handle the all device as require.

Current code (this code creates object manually)

    Device d1 =   Graph.create("pc1","",  100, 100, 200, 52) ;
    Device d2 =   Graph.create("pc2","",  300, 100 , 200, 52) ;
    Device d3 =   Graph.create("pc3","",  100, 300 , 200, 52) ;
    Device d4 =   Graph.create("pc4","",  600, 250 , 200, 52) ;
    ----------------------------------------------------------
    Device d100 =   Graph.create("pc100","",  800, 600 , 200, 52) ;
    
    Panel.add(d1);
    Panel.add(d2);
    Panel.add(d3);
    Panel.add(d4);
   ----------------
    Panel.add(d100);

   Link l1 = new link(d1,d3) ;
   Link l2 = new link(d1,d2) ;
   Link l3 = new link(d2,d3) ;
   Link l4 = new link(d4,d100) ;
   -----------------------------
   Link l100 = new link(d2,d4) ;

I need this two object creation in loop

Device d1 =   Graph.create("pc1","",  100, 100, 200, 52) ;
Link l1 = new link(d1,d3) ;

Please help me to creating these object (d1,d2,d3...d100) and (l1,l2,l3...l100) in loop in this environment.


Solution

  • well then, it'll be more or less like this.

    Device[] d = new Device[100]; //declare
    for(int i=0;i<100;i++)
    {
        d[i] = new Device(); //initilize
        d[i] = Graph.create(parameters); //store
    }
    

    Same goes for Link.

    Link[] l= new Link[100]; //declare
    for(int i=0;i<100;i++)
    {
        l[i] = new link(parameter); //initialize and store
    }
    

    btw, is new link(parameter); a typo? Instead, you meant to write new Link(parameter) ;?