简体   繁体   English

如何在Java中对ArrayList的ArrayList进行操作?

[英]How to operate on an ArrayList of ArrayList in Java?

I'm facing a problem when operating on an ArrayList of ArrayList in Java. 在Java中操作ArrayList的ArrayList时遇到问题。 I have this thing in my code- 我的代码中有这个东西 -

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

Problem is, I have no idea as to how I should operate on this (addition, removal, traversal etc.). 问题是,我不知道我应该如何对此进行操作(添加,删除,遍历等)。 I wish to create an adjacency list (for implementing simple, undirected graphs), and my instructor suggests that I should create an ArrayList of ArrayList. 我希望创建一个邻接列表(用于实现简单的无向图),我的教师建议我应该创建一个ArrayList的ArrayList。 I know I can do the following to add new element- 我知道我可以做以下添加新元素 -

L1.add(//Something I want to add);

But this throws up an error in the current case for obvious reasons. 但由于显而易见的原因,这在当前案例中引发了错误。

An ArrayList of an ArrayList , just think that the outer object is an ArrayList and you are done. ArrayListArrayList ,只是认为外部对象是一个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);

By the way, an adjacency list is just a list of edges, there's no need to store them for each vertex, especially if the graph is undirected. 顺便说一句,邻接表只是边的列表,不需要为每个顶点存储它们,尤其是在图形无向的情况下。 A list of Edge would be enough: Edge列表就足够了:

class Edge {
  Vertex v1, v2;
}

ArrayList<Edge> adjacencyList;

If you want to store them on a per vertex basis then you could avoid using a list of lists by encapsulating the edges inside the vertex class itself but this will require twice the edges: 如果要基于每个顶点存储它们,则可以通过将边缘封装在顶点类本身内部来避免使用列表列表,但这将需要两倍的边缘:

class Vertex {
  int value;
  ArrayList<Vertex> adjacency;
}

but which one is best depends on what kind of operation you need to perform on the graph. 但是哪种方法最好取决于您需要对图形执行哪种操作。 For a small graph there is no practical difference. 对于小图表,没有实际差异。

Another possible implementation, if you just need to know if two vertex are connected: 如果您只需要知道是否连接了两个顶点,则可以使用另一种可能的实现方式:

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

Try L1.get(i).add(whatever); 试试L1.get(i).add(whatever); , and of course first check whether L1.get(i) exists, otherwise add that inner list first. ,当然首先要检查L1.get(i)是否存在,否则请首先添加该内部列表。

It's something like this: 它是这样的:

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
L1.add(new ArrayList<Integer>());

will create a new List within the first list. 将在第一个列表中创建一个新列表。 Then you can 那么你也能

L1.get(0).add(5)
List<List<Integer>> L1 = new ArrayList<ArrayList<Integer>>();    
List<Integer> list1 = new ArrayList<Integer>();     
list1.add(1);
list1.add(2);
list1.add(3);
list1.add(4);
list1.add(5);

//add list to the list //将列表添加到列表中

L1.add(list1); 

iterate over the list of lists 遍历列表列表

for( List<Integer> list: L1 ){
      for(Integer i:list){
          System.out.println(i);
      }
}

You can only add objects of type ArrayList to L1. 您只能将ArrayList类型的对象添加到L1。 So you could do this: 所以你可以这样做:

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

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

firstList.add(secondList);

To add a new element to the outer array: 要将新元素添加到外部数组:

ArrayList<Integer> inner = new ArrayList<Integer>();
L1.add(inner);

Then to add element to the inner array: 然后将元素添加到内部数组:

   int exampleInt = 10;
   ArrayList<Integer> inner = L1.get(0);
   inner.add(exampleInt);

To traverse all elements in all arrays: 遍历所有数组中的所有元素:

   for (ArrayList<Integer> inner : L1)
   {
      for (Integer element : inner)
      {
         System.out.println(element);
      }
   }

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM