【问题标题】:Transfer a Two-Dimensional array to Two-Dimensional ArrayList?将二维数组传输到二维 ArrayList?
【发布时间】:2016-03-03 02:34:15
【问题描述】:

我有这段代码:

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 },
};

我需要把这个二维数组放入一个二维数组列表中,这样我就可以通过添加行和列来操纵它来移动模式。例如,当我的方法要求移动 2 行和 2 列时,我将能够将模式移动到这样的位置:

        { 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 },

我只是想将二维数组放入二维数组列表中,任何帮助将不胜感激!

【问题讨论】:

  • 请参阅已接受答案中的评论,它如何将数组移动到您想要的位置?

标签: java arrays arraylist


【解决方案1】:

案例1很短,但需要根据Arrays.asList();的需要将原始类型转换为引用类型(intInteger

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[][]

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);
}

【讨论】:

  • 在情况 2 中,原始 int 也隐式转换为 Integer,而 list.add() 方法。
  • 回答更新了,我是说数组类型,我知道你说的,万一1如果你不把原始类型转换成引用类型Arrays.asList()就不行了。
  • @HexxNine 我想知道如何将您的数组移动 2 行 2 列?这不是你的要求吗?
【解决方案2】:

如果将原始类型 int 转换为引用类型 Integer,则可以使用流:

public static void Two_Array_to_list_1() {
Integer[][] dataSet = new Integer[][] {{1, 2}, {3, 4}, {5, 6}};

List<List<Integer>> list =
    Arrays.stream(dataSet).map(Arrays::asList).collect(Collectors.toList());

System.out.println(list);

}

【讨论】:

    【解决方案3】:

    你可以这样做:

    public static List<Integer[]> twoDArrayList(int shift, int[][] input)
    {
    
        List<Integer[]> output = new ArrayList<Integer[]>();
        if( input.length == 0 ) return null;
        int columnlength = input.length;
        int rowlength = input[0].length;
        if (columnlength != rowlength) return null;
    
        int padsize = shift;
        for( int i = 0; i < padsize; i++ )
        {
            Integer[] zeroes = new Integer[shift+columnlength];
    
            for( int j = 0; j < shift+columnlength; j++)
            {
                zeroes[j] = 0;
            }
            output.add( zeroes );
        }
    
        for( int i = 0; i < columnlength; i++ )
        {
            int[] row = input[i];
            int[] zeroes = new int[shift];
            List<Integer> temp = new ArrayList<Integer>();
            for( int j = 0; j < shift; j++)
            {
                temp.add(0);
            }
            for( int k = 0; k < row.length; k++)
            {
                temp.add(row[k]);
            }
            output.add(temp.toArray(new Integer[]{}));
        }
    
        return output;
    }
    

    查看演示here

    当您将 shift 提供为 2 时:输出将如下所示:

    Running Shifting array...
    Array no. 0 in the list is : 0 0 0 0 0 0 0 0 0 
    Array no. 1 in the list is : 0 0 0 0 0 0 0 0 0 
    Array no. 2 in the list is : 0 0 1 1 1 1 1 1 1 
    Array no. 3 in the list is : 0 0 1 2 0 0 0 2 1 
    Array no. 4 in the list is : 0 0 1 0 3 0 3 0 1 
    Array no. 5 in the list is : 0 0 1 0 0 4 0 0 1 
    Array no. 6 in the list is : 0 0 1 0 3 0 3 0 1 
    Array no. 7 in the list is : 0 0 1 2 0 0 0 2 1 
    Array no. 8 in the list is : 0 0 1 1 1 1 1 1 1 
    

    当你提供 3 时,你的输出看起来像:

    Running Shifting array...
    Array no. 0 in the list is : 0 0 0 0 0 0 0 0 0 0 
    Array no. 1 in the list is : 0 0 0 0 0 0 0 0 0 0 
    Array no. 2 in the list is : 0 0 0 0 0 0 0 0 0 0 
    Array no. 3 in the list is : 0 0 0 1 1 1 1 1 1 1 
    Array no. 4 in the list is : 0 0 0 1 2 0 0 0 2 1 
    Array no. 5 in the list is : 0 0 0 1 0 3 0 3 0 1 
    Array no. 6 in the list is : 0 0 0 1 0 0 4 0 0 1 
    Array no. 7 in the list is : 0 0 0 1 0 3 0 3 0 1 
    Array no. 8 in the list is : 0 0 0 1 2 0 0 0 2 1 
    Array no. 9 in the list is : 0 0 0 1 1 1 1 1 1 1 
    

    【讨论】:

      【解决方案4】:

      简而言之,对于一维原始 int 数组,在这种情况下必须使用 IntStream.of() 而不是 stream()

      int[][] input = new int[][]{{1, 3, 10}, {2, 4, 55}};
      
      System.out.println(
                  Arrays.stream(input)
                          .map(e -> IntStream.of(e).boxed().collect(Collectors.toList()))
                          .collect(Collectors.toList())
      );
      // [[1, 3, 10], [2, 4, 55]]
      

      对于盒装整数数组,请参阅 Daria Yu 的回答

      【讨论】:

        猜你喜欢
        • 2020-09-10
        • 2023-04-03
        • 1970-01-01
        • 1970-01-01
        • 2020-10-04
        • 1970-01-01
        • 2017-01-18
        • 2019-01-23
        • 2021-06-14
        相关资源
        最近更新 更多