1

I have a large number of ArrayLists, and I want to be able to manipulate them using for loops. If they are named p1, p2, p3 ... pn, how can I perform a task such as .get(0) for all of them using a loop instead of

p1.get(0);
p2.get(0);
p3.get(0);
pn.get(0);
3
  • The solution is to not do this. Use an array/collection instead of lots of individual variables. Commented Aug 14, 2013 at 23:45
  • Well, you'd need an actual loop first. What have you tried so far? Commented Aug 14, 2013 at 23:45
  • Create an arrayList of arrayList's. Commented Aug 14, 2013 at 23:45

4 Answers 4

2
public class ListsExample {

    public static void main(String[] args) {
        List<Integer> p1 = Arrays.asList(1,2,3,4,5);
        List<Integer> p2 = Arrays.asList(1,2,3,4,5);
        List<Integer> p3 = Arrays.asList(1,2,3,4,5);
        List<List<Integer>> lists = new ArrayList<List<Integer>>();
        lists.add(p1);
        lists.add(p2);
        lists.add(p3);

        for(List<Integer> list : lists){
            System.out.println(list.get(0));
        }
    }
}
Sign up to request clarification or add additional context in comments.

Comments

1

Place your ArrayLists in an ArrayList. Access them in a for loop, then call get(0) on your retrieved ArrayList.

Comments

1

You could put them all into a list and loop over them

List<ArrayList> lists = new LinkedList<ArrayList>();
lists.add(p1);
lists.add(p2);

for ( ArrayList list : lists ) {
   list.get(0);
}

Comments

0

Your question is unclear. If you mean how to access the four separate arrays in a loop...

for (int i = 0; i < ...; ++ i) {
    p1.get(i);
    p2.get(i);
    p3.get(i);
    pn.get(i);
}

If you mean you have four equal-sized arrays and you want to group the data together more conveniently, then ditch the four areas, make a class with the relevant fields to store your data in instead, and use a single array of that:

class Item {
    Whatever v1;
    Whatever v2;
    Whatever v3;
    Whatever vn;
}

ArrayList<Item> items = ...;
items.get(0);

If you mean you have a variable number of those arrays and you want to select which one you access dynamically, you can use an ArrayList of the original ArrayLists:

List<ArrayList<Whatever>> p = getListOfAllMyArrayLists();
p.get(n).get(0);

If you want to keep all your separate arrays for some reason (maybe you can't refactor) and just want a convenient way to access all the data at once, you can write a method to do that and pack the results into their own array, e.g.:

public List<Whatever> getAll (int x) {
    List<Whatever> values = new ArrayList<Whatever>();
    values.add(p1.get(x));
    values.add(p2.get(x));
    values.add(p3.get(x));
    values.add(pn.get(x));
    return values;
}

You could also implement getAll() to take a variable number of source arrays if you use an ellipses parameter.

I hope one of these is helpful.

Comments

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.