什么是在Java中List<Integer[]>和List<List<Integer>>之间的区别?

huangapple go评论98阅读模式
英文:

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&lt;Integer[]&gt; fourNumberSum(int[] candidates, int target)  {
    List&lt;List&lt;Integer&gt;&gt; res = new ArrayList&lt;&gt;()
	dfs(candidates, target, res, new ArrayList&lt;Integer&gt;(), 0);
	return res;
}

./Program.java:11: error: incompatible types: List&lt;List&lt;Integer&gt;&gt; cannot be converted to List&lt;Integer[]&gt;
	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&lt;Integer[]&gt;

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&lt;Integer[]&gt; sampleList = new ArrayList&lt;&gt;();

//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&lt;List&lt;Integer&gt;&gt;

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&lt;List&lt;Integer&gt;&gt; sampleList = new ArrayList&lt;&gt;();

//Add list elements to list

List&lt;Integer&gt; element1 = new ArrayList&lt;&gt;();

element1.add(1);//You can set any number of elements

sampleList.add(element1);

//Retrieving the elements from the list 

List&lt;Integer&gt; listElement1 = sampleList.get(0);

答案2

得分: 1

他们不是Integer[]表示整数数组,而List<Integer>表示列表,如果你想将数组转换为列表,可以使用Arrays.asList(array)

英文:

They aren't Integer[] indicates an Array of integers and List&lt;Integer&gt; 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&lt;Integer[]&gt; x = new ArrayList&lt;Integer[]&gt;();
    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&lt;List&lt;Integer&gt;&gt; a = new ArrayList&lt;List&lt;Integer&gt;&gt;();        
    List&lt;Integer&gt; y = new ArrayList&lt;Integer&gt;();
    y.add(1);
    y.add(2);
    List&lt;Integer&gt; z = new ArrayList&lt;Integer&gt;();
    z.add(1);
    z.add(2);
    a.add(y);
    a.add(z);

huangapple
  • 本文由 发表于 2020年10月19日 03:49:49
  • 转载请务必保留本文链接:https://go.coder-hub.com/64417659.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定