1

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.

Mark Rotteveel
  • 100,966
  • 191
  • 140
  • 197
  • in what series `link(d1,d3)` ,`link(d1,d2)`and so on goes? – sittsering Jun 26 '21 at 08:43
  • this is not any series. d1 is object of device 1 and d2 is object of device 2 and .........d100 is object of device 100. if device 1 will be connected from device 2 then link will generate between these two device link(d1,d2); in need the solution for creating the object of Class of device (Device d1, Device d2,.......... ) how to create this object run time using any of loop – Rajan sharma Jun 26 '21 at 11:53
  • What I meant is, it seems you are linking two object randomly. I was wondering if you are linking two object in any pattern(like d1 and d2 first , then d2 and d3 , and so on, i.e d[i] and d[i+1]). – sittsering Jun 26 '21 at 12:09
  • Same goes for ` Device d1 = Graph.create("pc1","", 100, 100, 200, 52) ;` Here, you are giving different 3rd and 4th argument each time. Does it have any pattern or not? – sittsering Jun 26 '21 at 12:12
  • ok, so for x cord and y cord, are you taking it from user? or entering it manually?If manually, does it changes in pattern? cause it needs a pattern to loop on. If not, others have shown you few ways to do. – sittsering Jun 27 '21 at 06:10
  • Device d1 = Graph.create("device name","description", X coordinate,Y coordinate, height, width). in my program i am checking my database table, if count is coming 10 then i will have to create 10 device that means Device d1.....Device d10. and in count is coming 200 then Device d1....device d200. Graph.create(forget about parameters) this is creating a device like PC or laptop or switch on jpanel. and Device d1 is for storing a device as an object for further operation – Rajan sharma Jun 27 '21 at 06:19

2 Answers2

1

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) ;?

sittsering
  • 1,219
  • 2
  • 6
  • 13
  • Device[] d = new Device[100]; //declare for(int i=0;i<100;i++) { d[i] = new Device(); //initilize d[i] = Graph.create(parameters); //store } after completion of this loop i have one more line Panel.add(d[i]) but in this d is not accepting – Rajan sharma Jun 27 '21 at 07:07
  • instead of `d[i]`, i think individual parameter should be mention. like `Pannel.add(d[i].devicename, d[i].desciption, ... d[i].width)` whatever its name is. – sittsering Jun 27 '21 at 07:20
  • What values is assign to d[i] ?`d[i] = Graph.create(parameters);` – sittsering Jun 27 '21 at 07:22
  • problem solved. thanks buddy. Panel.add(d[i]) accepted. my program working fine now. thanks – Rajan sharma Jun 27 '21 at 07:30
  • only one error was in your code by mistake, Link l[i] = new link(parameter); //initialize and store. there should be only l[i] = new link(parameter); (inside of loop). – Rajan sharma Jun 27 '21 at 07:34
0
    /*First you will need to create 2D arrayList of all inputs --
    [{"pc1","",  100, 100, 200, 52},{"pc2","",  300, 100 , 200, 52}...]
    and put in inputArrayList :
    */
    //Example:
    ArrayList inputArrayList=new ArrayList();
    ArrayList tempInputList=new ArrayList();
    tempInputList.add("pc1");
    tempInputList.add("");
    tempInputList.add(100);
    tempInputList.add(100);
    tempInputList.add(200);
    tempInputList.add(52);
    inputArrayList.add(tempInputList);
/*  You cannot use dynamic variable naming in java but it is not restricted to it you can use HashMap */
ArrayList tempInputArr = new ArrayList();
HashMap<String,Object> deviceMap=new HashMap<String,Object>();
for(int l=1;l<=100;l++){
        tempInputArr = (ArrayList) inputArrayList.get(l);
        deviceMap.put("D"+l,Graph.create(tempInputArr.get(0), tempInputArr.get(1), tempInputArr.get(2), tempInputArr.get(3),
            tempInputArr.get(4))));

}
    
    /*You can also loop to add inputs*/
    
    /*After you have prepared your inputs below code can help in creation of objects you want : */
    
    /* Below code will prepare your device object list */
    
    List<Device> deviceList=new ArrayList<Device>();
    
    ArrayList<Device> panel = new ArrayList<Device>();
    ArrayList tempInputArr = new ArrayList();
    for (int i = 0; i < inputArrayList.size(); i++) {
        tempInputArr = (ArrayList) inputArrayList.get(i);
        panel.add(Graph.create(tempInputArr.get(0), tempInputArr.get(1), tempInputArr.get(2), tempInputArr.get(3),
                tempInputArr.get(4)));
    }
    
    /* Below code will prepare your link object list  in manner link(d1,d2), link(d1,d3)..link(d1,d100),..link(d98,d99),link(d98,d100),link(d99,d100)*/
    
    ArrayList<Link> linkList = new ArrayList<Link>();
    
    for (int p = 0; p < panel.size(); p++) {
        if (p < panel.size() - 1) {
            for (int q = p + 1; q < panel.size(); q++) {
                linkList.add(new Link(panel.get(p), panel.get(q)));
            }
        }
    }
  • [{"pc1","", 100, 100, 200, 52},{"pc2","", 300, 100 , 200, 52}...] i dont need for solution of this inputs. Device d1 = Graph.create("pc1","", 100, 100, 200, 52) ; i need solution for the object of Device calss Device D1 and Device D2 and so on. how can i generate that D1 and D2 object instance in loop – Rajan sharma Jun 26 '21 at 11:31
  • So as far as I understand from above clarification that is you want to create objects of Device D1,D2....D100 in a loop which will require dynamic variable naming but since Java is first compiled and then interpreted therefore its variable names must be resolved at compile time. So dynamically we CANNOT assign variable name BUT we can use HASHMAP to store D1,D2,D3.. as Key and Device Objects as values. I will edit my answer to help you as best as I can. – Ashutosh Mishra Jun 26 '21 at 14:20