英文:
What is the difference between List<Integer[]> and List<List<Integer>> in java
问题
什么是 List<Integer[]>
和 List<List<Integer>>
在 Java 中的区别。这两种类型都是 ArrayList 吗?如果它们不同,如何进行相互转换。谢谢。
public static List<Integer[]> fourNumberSum(int[] candidates, int target) {
List<List<Integer>> res = new ArrayList<>();
dfs(candidates, target, res, new ArrayList<Integer>(), 0);
return res;
}
./Program.java:11: error: incompatible types: List<List<Integer>> cannot be converted to List<Integer[]>
英文:
What is the difference between List<Integer[]> and List<List<Integer>> in java.
Are both types of ArrayList? If they are different how to convert to each other.
Thanks.
public static List<Integer[]> fourNumberSum(int[] candidates, int target) {
List<List<Integer>> res = new ArrayList<>()
dfs(candidates, target, res, new ArrayList<Integer>(), 0);
return res;
}
./Program.java:11: error: incompatible types: List<List<Integer>> cannot be converted to List<Integer[]>
return res;
^
答案1
得分: 1
List<Integer[]>
这将在您的List内部创建一个类型为Integer的数组。这意味着您将拥有一个List,其中包含类型为Integer[]的元素。但是数组的大小将是固定的。因此,您始终需要知道放入数组内的数据项的长度。因此,您所有的List元素(这些元素是数组)应该具有恒定的大小。
List<Integer[]> sampleList = new ArrayList<>();
//将数组元素添加到列表中
Integer[] element1 = {15, 20, 40};//在这里您需要知道大小
sampleList.add(element1);
//从列表中检索元素
Integer[] listElement1 = sampleList.get(0);
List<List<Integer>>
这将在列表内部创建一个列表。与之前的数组不同,您可以将任意数量的数据放入该特定列表中。因此,在这种情况下,您不需要像之前一样了解要存储的数据元素的大小。
List<List<Integer>> sampleList = new ArrayList<>();
//将列表元素添加到列表中
List<Integer> element1 = new ArrayList<>();
element1.add(1); //您可以设置任意数量的元素
sampleList.add(element1);
//从列表中检索元素
List<Integer> listElement1 = sampleList.get(0);
英文:
List<Integer[]>
This will create an array of type Integer inside your List.Which means you will have a List which contains it's elements of type Integer[]. But again the array size will be constant.So always you need to know the length of the data items which you are placing inside the Array.So all your List elements(Which are arrays)should have constant size.
List<Integer[]> sampleList = new ArrayList<>();
//Add array elements to list
Integer[] element1 = {15, 20, 40};//Here you need to know the size
sampleList.add(element1);
//Retrieving the elements from the list
Integer[] listElement1 = sampleList.get(0);
List<List<Integer>>
This will create a List inside a List.Not an array like before so you can place any amount of data to that particular list.So in this case you don't need to know about the size of data elements which you are going to store like before.
List<List<Integer>> sampleList = new ArrayList<>();
//Add list elements to list
List<Integer> element1 = new ArrayList<>();
element1.add(1);//You can set any number of elements
sampleList.add(element1);
//Retrieving the elements from the list
List<Integer> listElement1 = sampleList.get(0);
答案2
得分: 1
他们不是Integer[]
表示整数数组,而List<Integer>
表示列表,如果你想将数组转换为列表,可以使用Arrays.asList(array)
。
英文:
They aren't Integer[]
indicates an Array of integers and List<Integer>
indicates a list, if you want to convert array to a list you can do Arrays.asList(array)
答案3
得分: -1
希望这个示例能帮助您理解:
List<Integer[]> x = new ArrayList<Integer[]>();
Integer[] arr1 = new Integer[3];
arr1[0] = 34;
arr1[1] = 55;
arr1[2] = 2;
Integer[] arr2 = new Integer[3];
arr2[0] = 34;
arr2[1] = 55;
arr2[2] = 2;
x.add(arr1);
x.add(arr2);
List<List<Integer>> a = new ArrayList<List<Integer>>();
List<Integer> y = new ArrayList<Integer>();
y.add(1);
y.add(2);
List<Integer> z = new ArrayList<Integer>();
z.add(1);
z.add(2);
a.add(y);
a.add(z);
英文:
I hope this example will help you understand
List<Integer[]> x = new ArrayList<Integer[]>();
Integer[] arr1 = new Integer[3];
arr1[0] = 34;
arr1[1] = 55;
arr1[2] = 2;
Integer[] arr2 = new Integer[3];
arr2[0] = 34;
arr2[1] = 55;
arr2[2] = 2;
x.add(arr1);
x.add(arr2);
List<List<Integer>> a = new ArrayList<List<Integer>>();
List<Integer> y = new ArrayList<Integer>();
y.add(1);
y.add(2);
List<Integer> z = new ArrayList<Integer>();
z.add(1);
z.add(2);
a.add(y);
a.add(z);
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论