成对数组,Java 中的 ArrayList
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/6889838/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
array of pair, ArrayList in java
提问by hongtaesuk
how can I make array of ArrayList or Pair Class which I made myself at the code below.
如何制作我自己在下面的代码中制作的 ArrayList 或 Pair Class 数组。
ex1)
例1)
import java.util.*;
class Pair{
static int first;
static int second;
}
public class Main{
public static void main(String[] args){
Vector<Pair>[] v = new Vector<Pair>[100](); //this gives me an error
}
}
1.why the code above gives me an error? 2.my goal is to make an array of vector so that each index of vector holds one or more Pair classes. How can I make it?
1.为什么上面的代码给我一个错误?2.我的目标是制作一个向量数组,以便向量的每个索引都包含一个或多个 Pair 类。我怎样才能做到?
another example) : array of ArrayList
另一个例子):ArrayList 的数组
import java.util.*;
public class Main{
public static void main(String[] args){
ArrayList<Integer> arr = ArrayList<Integer>(); //I know this line doesn't give error
ArrayList<Integer>[] arr = ArrayList<integer>[500]; // this gives me an error
}
}
3.why does the code above give me an error? 4.my goal is to make an array of ArrayList so that each index of Array has ArrayList/Queue/Vector/Deque whatever. How can I make it?
3.为什么上面的代码给我一个错误?4.我的目标是制作一个ArrayList数组,以便Array的每个索引都有ArrayList/Queue/Vector/Deque。我怎样才能做到?
回答by MByD
How about a full generic solution:
一个完整的通用解决方案怎么样:
ArrayList<ArrayList<Integer>> arr = new ArrayList<ArrayList<Integer>>();
回答by adamjmarkham
The syntax you have used is not what Java uses. If you want to have an array of ArrayLists then do:
您使用的语法不是 Java 使用的。如果你想要一个 ArrayLists 数组,那么执行:
ArrayList[] arr = new ArrayList[100];
for(int i=0; i<arr.length; i++)
{
arr[i] = new ArrayList<Pair>(); // add ArrayLists to array
}
Here the type argument <Pair>
specifies that the ArrayList should contain items of type Pair
. But you can specify any type you wish to use. The same goes for ArrayList, you could replace ArrayList
with Vector
in the example.
这里的 type 参数<Pair>
指定 ArrayList 应该包含 type 的项目Pair
。但是您可以指定您希望使用的任何类型。ArrayList 也是如此,您可以在示例中替换ArrayList
为Vector
。
It would be best to use an ArrayList instead of an array in the example. Its much easier to maintain without worrying about the changing length and indexes.
在示例中最好使用 ArrayList 而不是数组。它更容易维护而无需担心更改的长度和索引。
Hope this helps.
希望这可以帮助。
回答by RiaD
public static void main(String[] args){
Vector[] v = new Vector[5];
for(int i=0;i<5;++i){
v[i]= new Vector<Pair>();
}
}
回答by skaz
I don't know java that well, but don't you want to do:
ArrayList<ArrayList<Pair>> v = new ArrayList<ArrayList<Pair>>();
我不太了解java,但你不想这样做:
ArrayList<ArrayList<Pair>> v = new ArrayList<ArrayList<Pair>>();
Try to break down what containers you need in your question. Your goal is to make a ArrayList (ok, the outer ArrayList satisfies that purpose) that has one or more pair classes in that. "That has" means that "each item in the ArrayList is this type". What would we use to store one or more Pair
classes? Another Vector/List of tyoe Pair
. So each item in the outer ArrayList is another ArrayList of Pairs.
尝试分解您在问题中需要的容器。您的目标是制作一个 ArrayList(好吧,外部 ArrayList 满足该目的),其中包含一个或多个配对类。“that has”表示“ArrayList 中的每一项都是这种类型”。我们将使用什么来存储一个或多个Pair
类?tyoe 的另一个向量/列表Pair
。所以外层 ArrayList 中的每一项都是另一个 ArrayList of Pairs。
Note: I moved everything to ArrayList because I read that Vector is somewhat deprecated and they serve similar functions. You may want to check on this.
注意:我将所有内容都移到了 ArrayList 中,因为我读到 Vector 已被弃用,并且它们提供类似的功能。你可能想检查一下。
This example should help with with the next part of your question, but let me know if it doesn't,
这个例子应该有助于解决您问题的下一部分,但如果没有,请告诉我,