如何在循环中创建多个对象以保持对每个设备的唯一记忆



创建这样的对象,即所有唯一的对象都可以通过多种方式根据需要处理所有设备。

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

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(。

那么,它或多或少会是这样的。

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

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

最新更新