猿问

使用 Java 流将 ArrayList<Integer> 转换为 Integer[][]

我有一个 ArrayList,需要将其转换为二维数组。我需要使用 Java 流来实现这一点。


private static ArrayList<Integer> GLOBALLIST;

Integer[][] TwoDArray = new Integer[2][8];

GLOBALLIST = Lists.newArrayList(36,40,44,48,52,56,60,64,100,104,108, 112,116,120,124,128);

AtomicInteger counter = new AtomicInteger(0);

        TwoDArray = (Integer[][]) ((GLOBALLIST.stream()

                .collect(Collectors.groupingByConcurrent(it -> counter.getAndIncrement() / 8))

                .values()).toArray(new Integer[2][8]));

这是错误说明 ObjectList 无法转换为 Integer[][]


慕沐林林
浏览 312回答 3
3回答

冉冉说

当您的起点是ArrayList,即List支持随机访问时,例如ArrayList<Integer> list = new ArrayList<>(Arrays.asList(36,40,44,48,52,56,60,64,100,104,108,112,116,120,124,128,132,136,140,144,149,153,157,161,165,169,173,177));你可以简单地使用Integer[][] array = IntStream.range(0, (list.size()+7)/8)&nbsp; &nbsp; .mapToObj(ix -> list.subList(ix*=8, Math.min(ix+8,list.size())).toArray(new Integer[0]))&nbsp; &nbsp; .toArray(Integer[][]::new);System.out.println(Arrays.deepToString(array));[[36, 40, 44, 48, 52, 56, 60, 64], [100, 104, 108, 112, 116, 120, 124, 128], [132, 136, 140, 144, 149, 153, 157, 161], [165, 169, 173, 177]]

拉莫斯之舞

如果你使用番石榴,那么这里有一个解决方案:ArrayList<Integer> GLOBALLIST;&nbsp; &nbsp; GLOBALLIST = Lists.newArrayList(36,40,44,48,52,56,60,64,100,104,108, 112,116,120,124,128,132,136,140,144,149,153,157,161,165,169,173,177);&nbsp; &nbsp; int[][] twoDList = Lists.partition(GLOBALLIST,8)&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; .stream()&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; .map(Ints::toArray)&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; .map(a -> Arrays.copyOf(a, 8))&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; .toArray(int[][]::new);我在gradle 中使用了以下 guava 依赖项:compile 'com.google.guava:guava:22.0'

侃侃尔雅

随着轻微的修改到blockCollector在这个答案,你可以做这样的:public static Integer[][] toArray2D(Collection<Integer> list, int blockSize) {&nbsp; &nbsp; return list.stream()&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;.collect(blockCollector(blockSize))&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;.stream()&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;.map(sublist -> sublist.toArray(new Integer[sublist.size()]))&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;.toArray(length -> new Integer[length][]);}public static <T> Collector<T, List<List<T>>, List<List<T>>> blockCollector(int blockSize) {&nbsp; &nbsp; return Collector.of(&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; ArrayList<List<T>>::new,&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; (list, value) -> {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; List<T> block = (list.isEmpty() ? null : list.get(list.size() - 1));&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; if (block == null || block.size() == blockSize)&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; list.add(block = new ArrayList<>(blockSize));&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; block.add(value);&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; },&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; (r1, r2) -> { throw new UnsupportedOperationException("Parallel processing not supported"); }&nbsp; &nbsp; );}测试List<Integer> list = Arrays.asList(36,40,44,48,52,56,60,64,100,104,108, 112,116,120,124,128,132,136,140,144,149,153,157,161,165,169,173,177);Integer[][] r = toArray2D(list, 8);System.out.println(Arrays.deepToString(r));输出[[36, 40, 44, 48, 52, 56, 60, 64], [100, 104, 108, 112, 116, 120, 124, 128], [132, 136, 140, 144, 149, 153, 157, 161], [165, 169, 173, 177]]
随时随地看视频慕课网APP

相关分类

Java
我要回答