简体   繁体   English

如何将多维整数数组复制到另一个数组

[英]How to copy a multidimensional integer array into another one

So I have this block of code 所以我有这段代码

public class Filter {
   int[][] sourceImage;

public Filter(int[][] image) {

    // TODO store values to sourceImage
   }
}

All I want to do here is store the values passed into image to sourceImage. 我在这里要做的就是将传入图像的值存储到sourceImage中。 Can someone please help me with how to do that? 有人可以帮我怎么做吗? Thank You. 谢谢。

The easiest way would be to just do 最简单的方法是

sourceimage = image;

Note that this copies the reference to the array, so both sourceimage and the reference passed into the filter() method will point to the same array. 请注意,这sourceimage引用复制到数组,因此sourceimage和传递给filter()方法的引用都将指向同一数组。 Any changes made to the array will be visible from both references. 从两个引用中都可以看到对该数组所做的任何更改。

If sourceImage must be a distinct array, you can loop through both dimensions and copy each item: 如果sourceImage必须是一个不同的数组,则可以遍历两个维度并复制每个项目:

sourceImage = new int[image.length][]; // Initialize the first dimension.
for (int i=0; i<sourceImage.length; i++) {
    sourceImage[i] = new int[image[i].length]; // Initialize the 2nd dimension.
    for (int j=0; j<sourceImage[i].length; j++) {
        sourceImage[i][j] = image[i][j]; // Copy each value.
    }
}

You can do this a little quicker by making use of System.arraycopy , but explicit loops are better for learning :-) 您可以通过使用System.arraycopy更快地完成此操作,但是显式循环更适合于学习:-)

If it's okay that the object's sourceImage is the same array as the one passed to the constructor, then you can simply assign it. 如果对象的sourceImage 传递给构造函数的数组相同 ,则可以简单地分配它。 Doing it this way means that any changes to one of the arrays ( image or sourceImage ) will affect them both , because they are just two references to the same array object. 这样做意味着对数组之一( imagesourceImage )的任何更改都会影响它们两个 ,因为它们只是对同一数组对象的两个引用。

sourceImage = image;
sourceImage = new int[image.length][];
for (int i=0; i<image.length; i++) {
    sourceImage[i] = Arrays.copyOf(image[i],image[i].length);
}

All you have to do is use Arrays.copyOf() method so that the values of image[][] will get copied to sourceImage[][] 您所要做的就是使用Arrays.copyOf()方法,以便将image [] []的值复制到sourceImage [] []

public class Filter {
   int[][] sourceImage;

public Filter(int[][] image) {

        sourceImage = new int[image.length][];

    for (int i = 0; i < image.length; ++i) {
         sourceImage[i] = Arrays.copyOf(image[i], image[i].length);
     }
   } 

}

You have to do this because if you DO THIS 您必须这样做,因为如果这样做

sourceImage=image;//(WHICH SHOULD NEVER BE DONE UNLESS YOU ACTUALLY WANT BOTH TO REFER TO THE SAME LOCATION)

then if in the program you try to change the value of image then the value of sourceImage will change as they refer to the same location 然后,如果您在程序中尝试更改image的值,则sourceImage的值将更改,因为它们引用相同的位置

You cannot pass the array. 您不能传递数组。 Technically only address is passed between methods. 从技术上讲,仅地址在方法之间传递。 Instead you can keep the array in a class and send that class as an argument. 相反,您可以将数组保留在一个类中,然后将该类作为参数发送。

class A{ A级{
int arr[] = {1,2,3,4,5}; int arr [] = {1,2,3,4,5};
} }

class B{ B级{
A a = new A(); A a = new A();
public A process(A y) { 公共A进程(y){
} }
} }

Hope this clarifies your question. 希望这可以澄清您的问题。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM