简体   繁体   English

在Java中从int数组转换为Integer arraylist

[英]Converting from int array to Integer arraylist in java

I have a problem in converting from Array to ArrayList 我从Array转换为ArrayList时遇到问题

public class one 
{
public static void main(String args[])
{
int y[]={12,25,38,46};
two p=new two();
p.setLocations(y);
}
}


import java.io.*;
import java.util.*;   

public class two 
{
    ArrayList<Integer> data_array=new ArrayList<Integer>();


void setLocations(int locations[])
{
        ArrayList<Integer> locations_arraylist=new ArrayList(Arrays.asList(locations));
        data_array=locations_arraylist;
    for(int i=0;i<data_array.size();i++)
        System.out.println("data_array["+i+"]="+data_array.get(i));
}
}

In the below line 在下面的行

ArrayList<Integer> locations_arraylist=new  ArrayList(Arrays.asList(locations));
//Copying from array to ArrayList-Its converting,Please suggest

An int[] is quite different from a List<Integer> . int[]List<Integer>完全不同。 For example, an Integer has an identity as well as a value. 例如,一个Integer具有标识又具有值。 There is no very simple way to do the conversion. 没有非常简单的方法来进行转换。

The following way works with Java 8. 以下方法适用于Java 8。

int[] array = {1, 2, 3, 4, 5};
List<Integer> list = IntStream.of(array).boxed().collect(Collectors.toCollection(ArrayList::new));

The following way works in earlier versions. 以下方法适用于早期版本。

int[] array = {1, 2, 3, 4, 5};
List<Integer> list = new ArrayList<Integer>();
for (int a : array)
    list.add(a);

If you pass an int[] to Arrays.asList you get a List<int[]> , not a List<Integer> . 如果将int[]传递给Arrays.asList则会得到List<int[]> ,而不是List<Integer>

It is recommended to use the interface List for members/variables and use the ArrayList constructor. 建议对成员/变量使用接口List ,并使用ArrayList构造函数。 Also ArrayList without the brackets indicates the raw type instead of instead of the generic. 另外,不带方括号的ArrayList表示原始类型,而不是常规类型。

If you would like to avoid a for loop that will copy the values from array to List , there are 2 solutions : 如果您要避免for循环将值从数组复制到List ,则有两种解决方案:

  1. Guava (put on top import com.google.common.primitives.Ints; ) 番石榴(放在顶部import com.google.common.primitives.Ints;

     List<Integer> locations_arraylist = Ints.asList(locations); 
  2. pass the values to Arrays.asList() directly 将值直接传递给Arrays.asList()

     List<Integer> locations_arraylist = Arrays.asList(12, 25, 38, 46); 

You can try this: 您可以尝试以下方法:

replace int y[]={12,25,38,46}; 替换int y[]={12,25,38,46}; to Integer y[] = {12, 25, 38, 46}; Integer y[] = {12, 25, 38, 46};
No need of this line as well 也不需要这条线
ArrayList<Integer> data_array = new ArrayList<Integer>();
You can use for-each loop for print the array : 您可以使用for-each循环来打印数组:

int i=0;
        for (Integer locations_arraylist1 : locations_arraylist) {
             System.out.println("data_array[" + i + "]=" + locations_arraylist1);
             i++;
        }

Try this: 尝试这个:

int[] arrayInt = new int[]{1, 2, 3};
List<Integer> list = Arrays.asList(ArrayUtils.toObject(arrayInt));

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM