开发者

how to make an array of arrays

how would you write an array of values to other array. In instance, I have a list of IPs and list of requests. I want to have something like [{ip1, request1}, {ip2, request2}, ....].

It's how I would do it, but sure obj will change every time and array wil开发者_高级运维l have all the time the same values.

ArrayList array = new ArrayList();
    Object[] obj = new Object[2];

    for (int i=0; i<listSize; i++){

        obj[0] = ipList.get(i).toString();
        obj[1] = requestList.get(i);

        array.add(obj);


I think this should be:

ArrayList array = new ArrayList();

for (int i=0; i<listSize; i++){
    Object[] obj = {ipList.get(i).toString(), requestList.get(i)};

    array.add(obj);
}

Also consider creating a new class for obj. (I do not know what it should be called because you did not say what it is for.)


Just move the line where you create obj into the loop...

ArrayList array = new ArrayList();

for (int i=0; i<listSize; i++){
    Object[] obj = new Object[2];

    obj[0] = ipList.get(i).toString();
    obj[1] = requestList.get(i);

    array.add(obj);


You are better off with a LinkedHashMap

  Map map = new LinkedHashMap();
  for (int i=0; i<listSize; i++){

    map.put( ipList.get(i).toString(), requestList.get(i) );

    }
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜