Accessing ArrayList<ArrayList<SomeObject>> elements

两盒软妹~` 提交于 2019-12-06 13:32:29

You would need to basically have the the ArrayList members on your new wrapper class and implement them in a different manner. I whipped up an example that demonstrates the correct index being calculated in get().

import java.util.ArrayList;

public class ListHolder<T> {
    public ArrayList<ArrayList<T>> list = new ArrayList<ArrayList<T>>();

    public int size() {
        int size = 0;
        for (int i = 0; i < list.size(); i++) {
            size += list.get(i).size();
        }
        return size;
    }

    public T get(int i) {
        if (i >= size())
            return null;

        int listIndex = 0;
        int valueIndex = i;

        while (valueIndex >= list.get(listIndex).size()) {
            valueIndex -= list.get(listIndex++).size();
        }

        return list.get(listIndex).get(valueIndex);
    }
}

What I used to verify my methods:

public static void main(String[] args)
{
    ListHolder<Object> listHolder = new ListHolder<Object>();

    listHolder.list.add(new ArrayList<Object>());
    listHolder.list.get(0).add("hello");
    listHolder.list.get(0).add("world");

    listHolder.list.add(new ArrayList<Object>());
    listHolder.list.get(1).add("a");
    listHolder.list.get(1).add("b");
    listHolder.list.get(1).add("c");

    System.out.println("Size: " + listHolder.size());
    System.out.println("listHolder[0]: " + listHolder.get(0)); // "hello"
    System.out.println("listHolder[1]: " + listHolder.get(1)); // "world"
    System.out.println("listHolder[2]: " + listHolder.get(2)); // "a"
    System.out.println("listHolder[3]: " + listHolder.get(3)); // "b"
    System.out.println("listHolder[4]: " + listHolder.get(4)); // "c"
    System.out.println("listHolder[5]: " + listHolder.get(5)); // "null"
}

You don't provide many details about what these lists are, and if they're mutable or not. But you could probably use an additional list containing all the elements of all the sublists:

private class Generation
    private List<List<Element>> populations = new ArrayList<>();
    private List<Element> allElements = new ArrayList<>();

    public Element getElementAt(int elementIndex) {
        return allElements.get(elementIndex);
    }

    public void addPopulation(List<Element> population) {
        populations.add(new ArrayList<>(population));
        allElements.addAll(population);
    }

    public List<Element> getPopulationAt(int populationIndex) {
        return Collections.unmodifiableList(populations.get(populationIndex));
    }
}
class Plot {
 class Point {
   int x;
   int y;
 }

 List<List<Point>> area = new ArrayList<List<Point>>();

 Point getPoint (int x, int y) throws IndexOutOfBoundsException {
   if (x < 0 && x >= area.size())
     throw new IndexOutOfBoundsException();
   int l = area.get(x).size();
   int i = (int)y/l;
   int j = y % l;
   return area.get(x+i).get(j);
 }

 void setPoint (int x, int y, Point p) throws IndexOutOfBoundsException {
   if (x < 0 && x >= area.size())
     throw new IndexOutOfBoundsException();
   int l = area.get(x).size();
   int i = (int)y/l;
   int j = y % l;
   area.get(x+i).set(j, p);
 }
}
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!