如何在 Java 中创建一个字符串向量数组?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/1111745/
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
How to create an array of string vectors in Java?
提问by Frank
I use the following code try to create an array of string vectors, I hope to have an array of 3 items, each item is a string vector :
我使用以下代码尝试创建一个字符串向量数组,我希望有一个包含 3 个项目的数组,每个项目都是一个字符串向量:
Vector<String> Result_Vector_Array[]=new Vector<String>[3];
But NB highlighted the line as error(generic array creation), what's wrong ? What's the correct way to do it ? I know there is also Arraylist, but it's not synchronized, so I want to use vector.
但是 NB 将该行突出显示为错误(通用数组创建),有什么问题?正确的做法是什么?我知道也有Arraylist,但它不是同步的,所以我想使用vector。
采纳答案by jqno
Due to type erasure, the JVM doesn't know at runtime that you have a Vector
of String
. The best it can do is create a 'raw' Vector. It can't guarantee for you that all Vector
s actually contain String
s. That's why you get a warning from your IDE.
由于类型擦除时,JVM不知道在运行时,你有一个Vector
的String
。它所能做的最好的事情就是创建一个“原始”向量。它不能为你保证所有的Vector
s 实际上都包含String
s。这就是您从 IDE 收到警告的原因。
One way to work around this, it cast it, as jgubby suggests. Another is to put a List
into your Vector
s, instead of an array.
正如 jgubby 建议的那样,解决此问题的一种方法是将其投射。另一种方法是将 aList
放入您的Vector
s 中,而不是数组中。
But, more importantly, why can the array have only 3 items? Wouldn't it be better to create a class with three fields to put into your Vector
? With three items, that's not too much work, and you get the added bonus that you can give each of the three elements a helpful name, which should make your code a lot clearer.
但是,更重要的是,为什么数组可以只有 3 个项目?创建一个包含三个字段的类放入您的Vector
. 使用三个项目,这不会有太多工作,而且您还可以获得额外的好处,即您可以为这三个元素中的每一个提供一个有用的名称,这将使您的代码更加清晰。
Also, since Java 6, there exist a number of useful new synchronized List
implementations, which might perform better than Vector
, such as CopyOnWriteArrayList
, or wrap a regular List
in a Collections.synchronizedList
.
此外,自 Java 6 以来,存在许多有用的新同步List
实现,它们的性能可能比 更好Vector
,例如CopyOnWriteArrayList
,或将常规包装List
在Collections.synchronizedList
.
回答by gubby
You cannot create an array like that, do this:
您不能创建这样的数组,请执行以下操作:
Vector<String> Result_Vector_Array[] = (Vector<String>[]) new Vector[3];
I would suggest a different approach - arrays of containers like that are often quite hard to use, and don't help in the understanding of your code.
我建议采用不同的方法 - 像这样的容器数组通常很难使用,并且无助于理解您的代码。
PS Also worth noting that the java naming convention would be
PS另外值得注意的是java命名约定是
Vector<String> resultVectorArray[] = (Vector<String>[]) new Vector[3];
and it's not usual to include the type in the name (I suspect this will be contentious!), why not just call it 'result' and let the type system worry about the type?
并且在名称中包含类型并不常见(我怀疑这会引起争议!),为什么不将其称为“结果”并让类型系统担心类型?
回答by dfa
using reflection it would be:
使用反射它将是:
Vector<String>[] arrayOfVectors =
(Vector<String>[]) java.lang.reflect.Array.newInstance(Vector.class, size);
java.util.Arrays.fill(arrayOfVectors, new Vector<String>());
for (Vector<String> v : arrayOfVectors) {
System.out.println(v);
}
alternatively you can use an ArrayList and then wrap it using Collections#synchronizedList(java.util.List)
或者,您可以使用 ArrayList,然后使用Collections#synchronizedList(java.util.List)包装它
回答by Artem Barger
You can also create:
您还可以创建:
Vector<Vector<String>> Result_Vector_Array=new Vector<Vector<String>>( );
Or you can replace Vector
with some other collection.
或者您可以替换Vector
为其他一些集合。
回答by Andreas Dolk
I'd suggest to keep with collections, do something like
我建议继续收藏,做类似的事情
Collection<Vector<String>> resultVectorArray = new ArrayList<Vector<String>>(3);
Then you can use generics with the Constructor and practically spoken the same effect
然后您可以将泛型与构造函数一起使用,并且实际上说出了相同的效果
回答by Tom
If you want to use an synchronized ArrayList, you could use the synchronizedListmethod in java.util.Collections.
如果要使用同步的 ArrayList,可以使用java.util.Collections 中的synchronizedList方法。
ArrayList<String> a1 = new ArrayList<String>();
ArrayList<String> a2= new ArrayList<String>();
ArrayList<String> a3 = new ArrayList<String>();
ArrayList<String> array[] = (ArrayList<String>[]) new ArrayList[3];
array[0]= Collections.synchronizedList(a1);
array[1]= Collections.synchronizedList(a2);
array[2]= Collections.synchronizedList(a3);
回答by OscarRyz
Should I understand you are going to use multithread on that array?...
我应该明白你打算在那个数组上使用多线程吗?...
If you are not, then you don't have to worry about the synchronization.
如果不是,那么您不必担心同步。
I would:
我会:
List<List<String>> listOfLists = new ArrayList<List<String>>();
List<String> firstVector = new ArrayList<String>();
firstVector.add( "one" );
firstVector.add( "two" );
listOfLists.add( firstVector );
System.out.println( listOfLists.get(0).get(0) == "one" );
System.out.println( listOfLists.get(0).get(1) == "two" );
Prints true, true
打印真实,真实