在java中将二维数组转换为列表?

在java中将二维数组转换为列表?,java,java-8,Java,Java 8,我有一个mxn对象的二维数组,比如Foo。所以我有Foo[][]foosArray。在Java中,将其转换为列表的最佳方法是什么?for(int i=0;i将其转换为列表的唯一方法是遍历数组并在运行时构建列表,如下所示: for(int i=0;i<m;i++) for(int j=0;j<n;j++) yourList.add(foosArray[i][j]); ArrayList<Foo[]> list = new ArrayList<

我有一个
mxn
对象的二维数组,比如
Foo
。所以我有
Foo[][]foosArray
。在Java中,将其转换为
列表的最佳方法是什么?

for(int i=0;i将其转换为列表的唯一方法是遍历数组并在运行时构建列表,如下所示:

for(int i=0;i<m;i++)
    for(int j=0;j<n;j++)
        yourList.add(foosArray[i][j]);
ArrayList<Foo[]> list = new ArrayList<Foo[]>(foosArray.length);
for(Foo[] foo: foosArray){
    list.add(foo);
}
ArrayList list=新的ArrayList(foosArray.length);
for(Foo[]Foo:foosArray){
添加(foo);
}

假设您希望二维数组按以下顺序排列,这是一种很好的处理二维数组的方法:

[[阵列[0]-元素],[阵列[1]元素]…]

public List twoDArray列表(T[]twoDArray){
列表=新的ArrayList();
for(T[]数组:twoDArray){
addAll(Arrays.asList(array));
}
退货清单;
}

另一种技术

//converting 2D array to string
String temp = Arrays.deepToString(fooArr).replaceAll("\\[", "").replaceAll("\\]", "");
List<String> fooList = new ArrayList<>(Arrays.asList(","));
//将二维数组转换为字符串
字符串temp=Arrays.deepToString(fooArr.replaceAll(“\\[”,”).replaceAll(“\\]”,”);
列表列表=新的ArrayList(Arrays.asList(“,”);
请注意:在将数组转换为列表时,基本数组和对象数组之间存在差异。即int[]和Integer[]

e、 (g)

在Keppil Response上工作;必须使用以下命令将基元数组转换为对象数组

否则,在正常for循环中逐个添加int值

int iLength = twoDArray.length;
List<List<Integer>> listOfLists = new ArrayList<>(iLength);
for (int i = 0; i < iLength; ++i) {
    int jLength = twoDArray[0].length;
    listOfLists.add(new ArrayList(jLength));
    for (int j = 0; j < jLength; ++j) {
      listOfLists.get(i).add(twoDArray[i][j]);
    }
}
int-ilelength=twoDArray.length;
List listOfLists=新阵列列表(长度);
对于(int i=0;i
还要注意,array.asList(array)将给出。

,因为

列表集合=数组。流(数组)/“数组”是二维的
.flatMap(数组::流)
.collect(Collectors.toList());

这可以通过以下方式使用Java 8流API完成:

String[][] dataSet = new String[][] {{...}, {...}, ...};
List<List<String> list = Arrays.stream(dataSet)
                               .map(Arrays::asList)
                               .collect(Collectors.toList());
String[][]dataSet=新字符串[][{{…},{…},{…},};
List使用java8“flatMap”进行游戏。一种方法可能是跟随

List<Foo> collection = Arrays.stream(array).flatMap(Arrays::stream).collect(Collectors.toList());
List collection=Arrays.stream(array).flatMap(Arrays::stream).collect(Collectors.toList());
ArrayList AlleglementList=新的ArrayList(Alleglements.length);
addAll(Arrays.asList(allelements));

等位元素是二维的

这里有一个逐步解决方案,可以将int(即基本数据类型)的二维数组转换为列表。解释见代码注释

import java.util.Arrays;
import java.util.List;
import java.util.stream.Collectors;

public class Temp {
    public static void main(String... args) {
        System.out.println("Demo...");
        int [] [] twoDimArray = new int[][]{
                {1,2}, {3,5}, {8,15}
        };

        List< List<Integer> > nestedLists =
                Arrays.
                //Convert the 2d array into a stream.
                stream(twoDimArray).
                //Map each 1d array (internalArray) in 2d array to a List.
                map(
                        //Stream all the elements of each 1d array and put them into a list of Integer.
                        internalArray -> Arrays.stream(internalArray).boxed().collect(Collectors.toList()
                    )
        //Put all the lists from the previous step into one big list.
        ).collect(Collectors.toList());

        nestedLists.forEach(System.out::println);
        
    }
}

让我们添加一些基本类型的数组。但您可以将参数传递给任何对象。方法正确地将int[]]作为对象[]处理:

 public <T, E> List<E> twoDArrayToList(T twoDArray) {

   if (twoDArray instanceof int[][])
     return Arrays.stream((int[][])twoDArray)
                  .flatMapToInt(a->Arrays.stream(a))
                  .mapToObj(i->(E)Integer.valueOf(i))
                  .collect(Collectors.toList());

   //… processing arrays of other primitive types 

   if (twoDArray instanceof Object[][])
        return Arrays.stream((E[][])twoDArray)
               .flatMap(Arrays::stream)
               .collect(Collectors.toList());

   return null; // better to throw appropriate exception 
                // if parameter is not two dimensional array
}
public List twoDArray列表(T twoDArray){
if(int[]的两个Darray实例)
返回Arrays.stream((int[][])twoDArray)
.flatMapToInt(a->array.stream(a))
.mapToObj(i->(E)Integer.valueOf(i))
.collect(Collectors.toList());
//…正在处理其他基元类型的数组
if(对象[][]的两个Darray实例)
返回Arrays.stream((E[][])twoDArray)
.flatMap(数组::流)
.collect(Collectors.toList());
返回null;//最好抛出适当的异常
//如果参数不是二维数组
}
例如

Integer[]a={{1,2,3},{4,5,6},{9,8,9};
列表arr=Arrays.stream(a)
.map(数组::asList)
.collect(Collectors.toList());

你的意思是一个列表,还是一个“2D”列表?你希望元素以什么顺序出现,还是一个列表列表?你想要一个数组列表还是一个列表列表?@nhahdh我编辑了这个问题。请检查一下——这与Tomasz Mularczyk的答案有什么不同?我想将int[]]数组转换为List。我得到一个错误-必需类型:List提供:List。如何解决此问题?无法转换数组类型int。必须具有整型数组。要将其流式传输到列表,请检查我的解决方案我要将int[][]数组转换为List。我在方法引用中得到一个错误-错误的返回类型:无法将java.util.stream.IntStream转换为java.util.stream.stream。我该如何解决这个问题?
String[][] dataSet = new String[][] {{...}, {...}, ...};
List<List<String> list = Arrays.stream(dataSet)
                               .map(Arrays::asList)
                               .collect(Collectors.toList());
List<Foo> collection = Arrays.stream(array).flatMap(Arrays::stream).collect(Collectors.toList());
ArrayList<Elelement[]> allElelementList = new ArrayList<>(allElelements.length);
    allElelementList.addAll(Arrays.asList(allElelements));
import java.util.Arrays;
import java.util.List;
import java.util.stream.Collectors;

public class Temp {
    public static void main(String... args) {
        System.out.println("Demo...");
        int [] [] twoDimArray = new int[][]{
                {1,2}, {3,5}, {8,15}
        };

        List< List<Integer> > nestedLists =
                Arrays.
                //Convert the 2d array into a stream.
                stream(twoDimArray).
                //Map each 1d array (internalArray) in 2d array to a List.
                map(
                        //Stream all the elements of each 1d array and put them into a list of Integer.
                        internalArray -> Arrays.stream(internalArray).boxed().collect(Collectors.toList()
                    )
        //Put all the lists from the previous step into one big list.
        ).collect(Collectors.toList());

        nestedLists.forEach(System.out::println);
        
    }
}
Demo...
[1, 2]
[3, 5]
[8, 15]
 public <T, E> List<E> twoDArrayToList(T twoDArray) {

   if (twoDArray instanceof int[][])
     return Arrays.stream((int[][])twoDArray)
                  .flatMapToInt(a->Arrays.stream(a))
                  .mapToObj(i->(E)Integer.valueOf(i))
                  .collect(Collectors.toList());

   //… processing arrays of other primitive types 

   if (twoDArray instanceof Object[][])
        return Arrays.stream((E[][])twoDArray)
               .flatMap(Arrays::stream)
               .collect(Collectors.toList());

   return null; // better to throw appropriate exception 
                // if parameter is not two dimensional array
}
Integer[][] a = { { 1, 2, 3 }, { 4, 5, 6 }, { 9, 8, 9 }, };
        
List<List<Integer>> arr = Arrays.stream(a)
                .map(Arrays::asList)
                .collect(Collectors.toList());