Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/sharepoint/4.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
java中int数组到Integer数组列表的转换_Java_Arrays_Arraylist - Fatal编程技术网

java中int数组到Integer数组列表的转换

java中int数组到Integer数组列表的转换,java,arrays,arraylist,Java,Arrays,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_ar

我在从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));
}
}
公共一级
{
公共静态void main(字符串参数[])
{
int y[]={12,25,38,46};
两个p=新的两个();
p、 设置位置(y);
}
}
导入java.io.*;
导入java.util.*;
公共二班
{
ArrayList data_array=新的ArrayList();
无效设置位置(整数位置[])
{
ArrayList locations_ArrayList=新的ArrayList(Arrays.asList(locations));
数据\数组=位置\数组列表;

对于(int i=0;i建议对成员/变量使用接口
List
,并使用
ArrayList
构造函数。此外,不带括号的
ArrayList
表示原始类型,而不是泛型

如果要避免将值从数组复制到
列表的for循环,有两种解决方案:

  • 番石榴(放在顶部
    import com.google.common.primitives.Ints;


  • int[]
    列表
    有很大不同。例如,
    整数既有一个标识,也有一个值。没有非常简单的转换方法

    以下方法适用于Java8

    int[] array = {1, 2, 3, 4, 5};
    List<Integer> list = IntStream.of(array).boxed().collect(Collectors.toCollection(ArrayList::new));
    
    int[]数组={1,2,3,4,5};
    List List=IntStream.of(array.boxed().collect(Collectors.toCollection(ArrayList::new));
    
    以下方法适用于早期版本

    int[] array = {1, 2, 3, 4, 5};
    List<Integer> list = new ArrayList<Integer>();
    for (int a : array)
        list.add(a);
    
    int[]数组={1,2,3,4,5};
    列表=新的ArrayList();
    for(int a:array)
    列表.添加(a);
    
    如果将
    int[]
    传递给
    Arrays.asList
    ,则得到的是
    列表
    ,而不是
    列表

    您可以尝试以下方法:

    整数y[]={12,25,38,46};
    替换为
    整数y[]={12,25,38,46};

    也不需要此行
    ArrayList data\u array=new ArrayList();

    您可以使用每个循环打印阵列:

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

    int[] arrayInt = new int[]{1, 2, 3};
    List<Integer> list = Arrays.asList(ArrayUtils.toObject(arrayInt));
    
    int[]arrayInt=newint[]{1,2,3};
    List List=Arrays.asList(ArrayUtils.toObject(arrayInt));
    
    什么是
    位置
    :数组
    int
    ArrayList
    ?请决定。@Eng.Fouad“locations”是一个int数组,我需要将位置从int转换为ArrayList
    setLocations(整数位置[])
    也应该是一个整数数组参数。我需要扩展我的类,并需要在将来执行添加和删除选项。然后最好按照Paul Boddington的答案进行操作。他在那里使用了一个列表作为他的解决方案。我只是尝试进行最小的修复,并使代码正常工作。:)
    int[] array = {1, 2, 3, 4, 5};
    List<Integer> list = new ArrayList<Integer>();
    for (int a : array)
        list.add(a);
    
    int i=0;
            for (Integer locations_arraylist1 : locations_arraylist) {
                 System.out.println("data_array[" + i + "]=" + locations_arraylist1);
                 i++;
            }
    
    int[] arrayInt = new int[]{1, 2, 3};
    List<Integer> list = Arrays.asList(ArrayUtils.toObject(arrayInt));