如何在Java中操作ArrayList的ArrayList?

如何在Java中操作ArrayList的ArrayList?,java,arraylist,Java,Arraylist,在Java中操作ArrayList的ArrayList时,我面临一个问题。我的密码里有这个东西- ArrayList<ArrayList<Integer>> L1 = new ArrayList<ArrayList<Integer>>(); 但是,由于明显的原因,这在当前的情况下引发了一个错误。TryL1.get(i).add(无论什么)0){ first=L1.get(0);//第一个元素 } 否则{ first=新的ArrayList();

在Java中操作ArrayList的ArrayList时,我面临一个问题。我的密码里有这个东西-

ArrayList<ArrayList<Integer>> L1 = new ArrayList<ArrayList<Integer>>();
但是,由于明显的原因,这在当前的情况下引发了一个错误。

Try
L1.get(i).add(无论什么)L1.get(i)
是否存在,否则首先添加该内部列表

是这样的:

List<List<Integer>> L1 = new ArrayList<List<Integer>>(); //better use interfaces

List<Integer> first = null;
if( L1.size() > 0) {
 first = L1.get(0); //first element
}
else {
  first = new ArrayList<Integer>();
  L1.add(first);      
}

first.add(4711); //or whatever you like to add
List L1=new ArrayList()//更好地使用接口
List first=null;
如果(L1.size()>0){
first=L1.get(0);//第一个元素
}
否则{
first=新的ArrayList();
L1.添加(第一);
}
第一,增加(4711)//或者你想加什么
遍历列表列表

for( List<Integer> list: L1 ){
      for(Integer i:list){
          System.out.println(i);
      }
}
for(列表:L1){
for(整数i:列表){
系统输出打印LN(i);
}
}

只能将ArrayList类型的对象添加到L1。所以你可以这样做:

ArrayList<ArrayList<Integer>> firstList = new ArrayList<ArrayList<Integer>>();

ArrayList<Integer> secondList = new ArrayList<Integer>();
secondList.add(0);

firstList.add(secondList);
ArrayList firstList=新建ArrayList();
ArrayList secondList=新的ArrayList();
第二个列表。添加(0);
firstList.add(第二个列表);

一个
ArrayList
ArrayList
,只要认为外部对象是一个
ArrayList
,就完成了

ArrayList<ArrayList<Integer>> list2d = new ArrayList<ArrayList<Integer>>();
// add an element to the list
list2d.add(new ArrayList<Integer>());
// retrieve a list 
ArrayList<Integer> list1d = list2d.get(0);
// add an integer
list2d.get(0).add(123);
如果要按顶点存储它们,则可以通过将边封装在顶点类本身中来避免使用列表列表,但这将需要两倍的边:

class Vertex {
  int value;
  ArrayList<Vertex> adjacency;
}
类顶点{
int值;
阵列邻接;
}
但是哪一个是最好的取决于您需要在图形上执行什么类型的操作。对于一个小图形,没有实际的区别

另一种可能的实现,如果您只需要知道两个顶点是否连接:

class Edge {
  public final int v1, v2;

  public boolean equals(Object o) { return o != null && o instanceof Edge && o.hashCode() == hashCode(); }

  public int hashCode() { return v1 ^ v2; } // simple hash code, could be more sophisticated
}

Set<Edge> adjacencyList = new HashSet<Edge>();
类边缘{
公共最终int v1、v2;
公共布尔等于(对象o){return o!=null&&o边的实例&o.hashCode()==hashCode();}
public int hashCode(){return v1^v2;}//简单的哈希代码可能更复杂
}
Set adjacencyList=new HashSet();

要将新元素添加到外部数组,请执行以下操作:

ArrayList<Integer> inner = new ArrayList<Integer>();
L1.add(inner);
ArrayList internal=new ArrayList();
L1.添加(内部);
然后,要将元素添加到内部数组,请执行以下操作:

   int exampleInt = 10;
   ArrayList<Integer> inner = L1.get(0);
   inner.add(exampleInt);
int-exampleInt=10;
ArrayList-inner=L1.get(0);
inner.add(exampleInt);
要遍历所有数组中的所有元素,请执行以下操作:

   for (ArrayList<Integer> inner : L1)
   {
      for (Integer element : inner)
      {
         System.out.println(element);
      }
   }
for(ArrayList内部:L1)
{
for(整型元素:内部)
{
系统输出打印项次(元素);
}
}

yes,对不起,没有IDE:X
L1.add(列表1)列表
,否则代码>将不起作用。这可能会产生误导。通过adjacencyList,它也可能意味着某个顶点的相邻顶点(其索引)列表,因此L1.get(i)将返回索引为idx i的所有相邻顶点索引列表。边的逐顶点列表的前进是在O(n(v))时间内获取顶点v的所有相邻n,其中n(v)是v的相邻数。如果只存储边,则需要O(e)时间,其中e是图形中的边数。这种差异在某些应用程序中可能非常关键。@kenor:在这种差异非常关键的上下文中,您根本不会使用
ArrayList
,在任何情况下,您都会使用集合作为顶点内部的邻接列表。如果性能非常关键,可以使用O(1),则无需使用O(n)查找。当然,这更多是一个假设性的注释,因为询问者出于某种原因被指示使用列表列表
class Edge {
  Vertex v1, v2;
}

ArrayList<Edge> adjacencyList;
class Vertex {
  int value;
  ArrayList<Vertex> adjacency;
}
class Edge {
  public final int v1, v2;

  public boolean equals(Object o) { return o != null && o instanceof Edge && o.hashCode() == hashCode(); }

  public int hashCode() { return v1 ^ v2; } // simple hash code, could be more sophisticated
}

Set<Edge> adjacencyList = new HashSet<Edge>();
ArrayList<Integer> inner = new ArrayList<Integer>();
L1.add(inner);
   int exampleInt = 10;
   ArrayList<Integer> inner = L1.get(0);
   inner.add(exampleInt);
   for (ArrayList<Integer> inner : L1)
   {
      for (Integer element : inner)
      {
         System.out.println(element);
      }
   }