2014-03-05 71 views
3

我有一个主要的2D对象数组,我想裁剪它的一部分。要清楚,我试图引用主数组的一部分,并创建一个对应于指定区域的新对象。所以,如果我改变裁剪数组中的东西,它也会在主数组中改变。我被困在试图找出数组是否传递对该对象或值的引用。 如果我有一个数组如何裁剪数组的一部分?

1,2,3 
4,5,6 

然后我就能够抓住

2,3 
5,6 

如果我是将它更改为

1,1 
2,2 

它看起来像

1,1,1 
4,2,2 

这是t他是我想要做的简单版本。

+1

有你看这里:http://docs.oracle.com/javase/7/docs/api/java/util/Arrays.html – AntonH

+0

会你引用你想裁剪的数组的部分与其维度和索引或其值? – asaini007

+1

Java是**总是**通过价值。你也可以向我们展示你到目前为止所尝试过的吗? – m0skit0

回答

1

我被困在试图找出数组是否传递对该对象或值的引用。

我不太清楚我是否正确理解你的隐含问题,但是Java数组通过值来存储它们的元素。这意味着int数组将存储int元素,而Object数组将存储对对象的引用,因此如果更改数组元素,则将更改引用以将“指向”指向不同对象,同时更改正在引用的对象不会导致数组的直接更改。

至于你的例子:

您可以创建持有要覆盖区域到int[][]数组引用(BTW是一个对象以及)和偏移量和大小的物体。

然后提供setters和getters,它们进行必要的索引计算并访问共享数组,例如,像这样:

//note that for simplicity's sake I'll omit a lot of code like constructors etc. 
class Region { 
    int[][] sharedArray; 
    int xOffset; 
    int yOffset; 
    int width; 
    int height; 

    public int get(int x, int y) { 
    //Note: you should check the array length and offsets to prevent IndexOutOfBoundsExceptions 
    return sharedArray[x + xOffset][y + yOffset]; 
    } 

    public set set(int x, int y, int value) { 
    //Note: you should check the array length and offsets to prevent IndexOutOfBoundsExceptions 
    sharedArray[x + xOffset][y + yOffset] = value; 
    } 
} 

int[][] array = ...; 
//define a 10x10 region starting at indices x = 2 and y = 4, e.g. it spans x = [2,11] and y = [4,13] 
Region r = new Region(array, 2, 4, 10, 10); 

//retrieve the element at position 5/5 relative to the region 
//or (5+2)/(5+4) = 7/9 in the shared array 
int element = r.get(5, 5); 
+0

因此,因为我的数组是一个对象数组,它将传递该对象的引用而不是该对象内的信息? – user2876613

+0

@ user2876613是数组元素只是引用,对象本身不被复制。 – Thomas

0

在java中,所有东西都是按值传递的,但是原始类型存储为值,对象存储为引用。如果您尝试使用它,则会复制储值。

看看在每一行的评论:

static class MutableInteger { 
    public int value; 
} 

public static void main(String[] args) { 
    int[] arraya = new int[3]; 
    int[] arrayb = arraya; //reference is copied, they both referencing to the same array now 
    arrayb[0] = 5; 
    int value = arrayb[0]; //arrayb[i] is integer - primitive data type, hence the value is copied 
    arrayb[0] = 3; //now arraya[0] and arrayb[0] are equal 3, "int value" is still 5 

    MutableInteger[] arrayc = new MutableInteger[3]; 
    arrayc[0] = new MutableInteger(); 
    MutableInteger a = arrayc[0]; //MutableInteger is object, therefore is stored as reference in arrayc[0], reference is copied 
    a.value = 5; //now the both, arrayc[0].value and "a.value" are equal 5 
} 
1

你可以创建一个类,说CroppedArray,提供了间接。就像是;

public class CroppedArray { 
int[][] theArray; 
int x1, y1, x2, y2; 

public CroppedArray(int[][] realArray,int x1,int y1,int x2,int y2) { 
    theArray = realArray; 
    this.x1 = x1; 
    this.y1 = y1; 
    this.x2 = x2; 
    this.y2 = y2; 
} 
public int getCell(int i, int j) { 
    return theArray[x1+i][y1+j]; 
} 
public int setCell(int i, int j, int value) { 
    return (theArray[x1+i][y1+j] = value); 
} 
} 

你会使用它像

CroppedArray section = new CroppedArray(original,2,2,3,3); 
section.setValue(0,0,-1); 
if (original[2][2] != section.getValue(0,0)) { ... err.re }