如何使用List、ArrayList和/和数组在Java中创建一个二维数组?

时间:2022-02-18 21:40:49

I'm fairly new to Java and having problems finding a structure to do the following.

我对Java相当陌生,在找到实现以下目标的结构时遇到了问题。

I want a fixed length array. Each item to be a variable length array (or list) holding strings. So I've tried...

我想要一个固定长度的数组。每个项目都是包含字符串的可变长度数组(或列表)。所以我试着…

ArrayList<String>[] wordList = new ArrayList[maxWordLength];

I get a slapped wrist from Java for "Main.java uses unchecked or unsafe operations." and when I try to add an item I get "java.lang.NullPointerException"

我被Java打了个耳光。当我尝试添加一个项目时,我得到了“java.lang. nullpointerexception”

wordList[oneWord.length()-1].add(oneWord);

How should I create my structure to keep Java happy?

我应该如何创建我的结构来保持Java的快乐?

3 个解决方案

#1


4  

Java doesn't like arrays of generic types. (See Restrictions on Generics in the Java tutorials.) Instead, use a list of lists:

Java不喜欢泛型类型的数组。(请参阅Java教程中对泛型的限制。)相反,使用列表:

List<List<String>> wordList = new ArrayList<>(maxWordLength);

This creates a list with an initial capacity of maxWordLength that can contain lists of String. The initial size (as opposed to capacity) will be 0. Then you can add individual lists of strings to wordList. To avoid a NullPointerException later, you should fill wordList with empty lists to start with:

这将创建一个初始容量为maxWordLength的列表,该列表可以包含字符串列表。初始大小(与容量相对)将为0。然后,您可以向wordList添加单独的字符串列表。为了避免稍后的NullPointerException,您应该先用空列表填充wordList:

for (int i = 0; i < maxWordLength; i++) {
    wordList.add(new ArrayList<>());
}

Finally, you can add a word to a particular list in wordList with:

最后,您可以在wordList中添加一个单词:

wordList.get(oneWord.length() - 1).add(oneWord);

This doesn't force wordList to be of fixed length, but otherwise should meet your requirements.

这并不要求单词列表的长度是固定的,但是应该满足您的要求。

#2


0  

Keep it simple: Array with all elements as array of the Strings:

保持简单:所有元素都作为字符串数组的数组:

List<List<String>> listOfStringsArray = new ArrayList<>();

List<String> stringArray = Arrays.asList("String1", "String2");

listOfStringsArray.add(stringArray);

#3


0  

List is the interface that concrete classes such as ArrayList and LinkedList must implement.

List是具体类(如ArrayList和LinkedList)必须实现的接口。

ArrayList is a List that is optimized for sequential reading, and adding, and is backed by an internal array.

ArrayList是一个为顺序读取和添加而优化的列表,它由一个内部数组支持。

ArrayList<blah> or List<blah> is a List that contains objects that inherits blah (or if it is an interface, objects that implement it.)

ArrayList 或List 是一个包含继承blah的对象的列表(如果是一个接口,则是实现它的对象)。

ArrayList[] is actually an array of ArrayLists when you new ArrayList[x] that really means create an array of x length that contains ArrayLists. Each of the ArrayLists are not assigned, and so it's an uninitialized object, by default, it is null in many compilers but you can't depend on that.

ArrayList[]实际上是一个ArrayList数组,当您使用新的ArrayList[x]时,它实际上意味着创建一个包含ArrayList的x长度数组。每个arraylist都没有被赋值,所以它是一个未初始化的对象,默认情况下,在许多编译器中它都是空的,但是你不能依赖它。

So, let's say you create this fixed length array that contains a variable length list. you either have to do a loop over the array and say wordList[i] = new ArrayList<String>() or you have to do a nullcheck before you assign it and create a new ArrayList on each assignment

假设你创建了一个包含可变长度列表的固定长度数组。要么对数组执行一个循环并写入wordList[i] = new ArrayList (),要么在分配数组并在每个赋值上创建一个新的ArrayList之前执行nullcheck

#1


4  

Java doesn't like arrays of generic types. (See Restrictions on Generics in the Java tutorials.) Instead, use a list of lists:

Java不喜欢泛型类型的数组。(请参阅Java教程中对泛型的限制。)相反,使用列表:

List<List<String>> wordList = new ArrayList<>(maxWordLength);

This creates a list with an initial capacity of maxWordLength that can contain lists of String. The initial size (as opposed to capacity) will be 0. Then you can add individual lists of strings to wordList. To avoid a NullPointerException later, you should fill wordList with empty lists to start with:

这将创建一个初始容量为maxWordLength的列表,该列表可以包含字符串列表。初始大小(与容量相对)将为0。然后,您可以向wordList添加单独的字符串列表。为了避免稍后的NullPointerException,您应该先用空列表填充wordList:

for (int i = 0; i < maxWordLength; i++) {
    wordList.add(new ArrayList<>());
}

Finally, you can add a word to a particular list in wordList with:

最后,您可以在wordList中添加一个单词:

wordList.get(oneWord.length() - 1).add(oneWord);

This doesn't force wordList to be of fixed length, but otherwise should meet your requirements.

这并不要求单词列表的长度是固定的,但是应该满足您的要求。

#2


0  

Keep it simple: Array with all elements as array of the Strings:

保持简单:所有元素都作为字符串数组的数组:

List<List<String>> listOfStringsArray = new ArrayList<>();

List<String> stringArray = Arrays.asList("String1", "String2");

listOfStringsArray.add(stringArray);

#3


0  

List is the interface that concrete classes such as ArrayList and LinkedList must implement.

List是具体类(如ArrayList和LinkedList)必须实现的接口。

ArrayList is a List that is optimized for sequential reading, and adding, and is backed by an internal array.

ArrayList是一个为顺序读取和添加而优化的列表,它由一个内部数组支持。

ArrayList<blah> or List<blah> is a List that contains objects that inherits blah (or if it is an interface, objects that implement it.)

ArrayList 或List 是一个包含继承blah的对象的列表(如果是一个接口,则是实现它的对象)。

ArrayList[] is actually an array of ArrayLists when you new ArrayList[x] that really means create an array of x length that contains ArrayLists. Each of the ArrayLists are not assigned, and so it's an uninitialized object, by default, it is null in many compilers but you can't depend on that.

ArrayList[]实际上是一个ArrayList数组,当您使用新的ArrayList[x]时,它实际上意味着创建一个包含ArrayList的x长度数组。每个arraylist都没有被赋值,所以它是一个未初始化的对象,默认情况下,在许多编译器中它都是空的,但是你不能依赖它。

So, let's say you create this fixed length array that contains a variable length list. you either have to do a loop over the array and say wordList[i] = new ArrayList<String>() or you have to do a nullcheck before you assign it and create a new ArrayList on each assignment

假设你创建了一个包含可变长度列表的固定长度数组。要么对数组执行一个循环并写入wordList[i] = new ArrayList (),要么在分配数组并在每个赋值上创建一个新的ArrayList之前执行nullcheck