如何在循环中创建多个对象以唯一地记住每个设备

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

像这样创建对象,所有唯一对象都可以使用多种方式来根据需要处理所有设备。

当前代码(此代码手动创建对象)

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

我需要在循环中创建这两个对象

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

请帮助我在此环境中循环创建这些对象 (d1,d2,d3...d100) 和 (l1,l2,l3...l100)。

    /*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)));
            }
        }
    }

那么,差不多就是这样吧

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

Link也是如此。

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

顺便说一句,new link(parameter); 是错字吗?相反,您打算写 new Link(parameter) ;?