如何深度复制2维数组(不同的行大小) [英] How to deep copy 2 dimensional array (different row sizes)

查看:252
本文介绍了如何深度复制2维数组(不同的行大小)的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

这是我在这样一个社区的第一个问题,所以我的问题格式可能不会太好遗憾的是摆在首位。

This is my first question in a community like this, so my format in question may not be very good sorry for that in the first place.

现在,我的问题是,我想深复制Java中的2维数组。在1维杜安它甚至2行和列的固定大小的二维数组,当它是pretty容易。我的主要问题是我不能让一个初始化的第二个数组我试图复制,如:

Now that my problem is I want to deep copy a 2 dimension array in Java. It is pretty easy when doin it in 1 dimension or even 2 dimension array with fixed size of rows and columns. My main problem is I cannot make an initialization for the second array I try to copy such as:

int[][] copyArray = new int[row][column]

由于行大小是不固定的和变化中的每一行索引诸如我尝试复制此数组:

Because the row size is not fixed and changes in each row index such as I try to copy this array:

int[][] envoriment = {{1, 1, 1, 1}, {0, 1, 6}, {1}};

所以你看,如果我说新INT [3] [4] 将有多余的空格,我不想要的。是否有深层复制此类二维阵列的方法?

So you see, if I say new int[3][4] there will be extra spaces which I don't want. Is there a method to deep copy such kind of 2 dimensional array?

推荐答案

我觉得你的意思是,列大小是不固定的。反正一个简单直接的方法是:

I think what you mean is that the column size isn't fixed. Anyway a simple straightforward way would be:

public int[][] copy(int[][] input) {
      int[][] target = new int[input.length][];
      for (int i=0; i <input.length; i++) {
        target[i] = Arrays.copyOf(input[i], input[i].length);
      }
      return target;
}

这篇关于如何深度复制2维数组(不同的行大小)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆