0

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

3 Answers 3

2

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

Sign up to request clarification or add additional context in comments.

Comments

1

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

Comments

0

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

    }

1 Comment

That will only work if the IP addresses are unique (the OP did not specify this.)

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.