将二维数组传输到二维ArrayList吗?

编程入门 行业动态 更新时间:2024-10-24 12:33:01
本文介绍了将二维数组传输到二维ArrayList吗?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧! 问题描述

我有这段代码:

int[][] pattern = new int[][]{ { 1, 1, 1, 1, 1, 1, 1 }, { 1, 2, 0, 0, 0, 2, 1 }, { 1, 0, 3, 0, 3, 0, 1 }, { 1, 0, 0, 4, 0, 0, 1 }, { 1, 0, 3, 0, 3, 0, 1 }, { 1, 2, 0, 0, 0, 2, 1 }, { 1, 1, 1, 1, 1, 1, 1 }, };

我需要将此2d数组放入2d ArrayList中,以便我可以通过添加行和列来移动模式来对其进行操作.例如,当我的方法要求移动2行和2列时,我将能够将模式移动到如下所示:

I need to get this 2d array into a 2d ArrayList so i can manipulate it by adding rows and columns to move the pattern around. For example when my method calls for a shift of 2 rows and 2 columns i will be able to move the pattern to something like this:

{ 0, 0, 0, 0, 0, 0, 0, 0, 0 } { 0, 0, 0, 0, 0, 0, 0, 0, 0 } { 0, 0, 1, 1, 1, 1, 1, 1, 1 }, { 0, 0, 1, 2, 0, 0, 0, 2, 1 }, { 0, 0, 1, 0, 3, 0, 3, 0, 1 }, { 0, 0, 1, 0, 0, 4, 0, 0, 1 }, { 0, 0, 1, 0, 3, 0, 3, 0, 1 }, { 0, 0, 1, 2, 0, 0, 0, 2, 1 }, { 0, 0, 1, 1, 1, 1, 1, 1, 1 },

我只是想将2d数组放入2d Arraylist中,因此不胜感激!

I'm just looking to get the 2d array into a 2d Arraylist any help will be greatly appreciated!

推荐答案

案例1 简短,但需要将基本类型隐式引用为引用类型(int到Integer),例如Arrays.asList();

Case 1 It is short, but need to covert the primitive type to reference type (int to Integer) as needed for Arrays.asList();

Integer[][] pattern = new Integer[][]{ { 1, 1, 1, 1, 1, 1, 1 }, { 1, 2, 0, 0, 0, 2, 1 }, { 1, 0, 3, 0, 3, 0, 1 }, { 1, 0, 0, 4, 0, 0, 1 }, { 1, 0, 3, 0, 3, 0, 1 }, { 1, 2, 0, 0, 0, 2, 1 }, { 1, 1, 1, 1, 1, 1, 1 }, }; List<List<Integer>> lists = new ArrayList<>(); for (Integer[] ints : pattern) { lists.add(Arrays.asList(ints)); }

案例2 :如果您不想将原始类型转换为引用类型:(int[][] pattern = new int[][]至Integer[][] pattern = new Integer[][])

Case 2 If you don't want to covert the primitive type to reference type: (int[][] pattern = new int[][] to Integer[][] pattern = new Integer[][])

List<List<Integer>> lists = new ArrayList<>(); for (int[] ints : pattern) { List<Integer> list = new ArrayList<>(); for (int i : ints) { list.add(i); } lists.add(list); }

更多推荐

将二维数组传输到二维ArrayList吗?

本文发布于:2023-07-29 00:36:47,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1235588.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:数组   ArrayList

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!