我有点困惑,希望有人能对此事有所了解。我创建了一个从图像数组中删除水平线并返回没有任何水平线的图像数组的函数。但是当我用我的图像数组调用这个函数时,它也会直接修改我传递给它的图像数组。为什么会这样?
例子
int[][] imageArray = ImageToArray();
int[][] _NoLines = RemovedLines(imageArray);
imageArray
应该还有它的线条吧?
如果有人好奇的话,这是实际的功能:
public int[][] RemovedLines(int[][] _baseImage)
{
int width = _baseImage[0].length, height = _baseImage.length;
int white = 0;
int black = 0;
for (int y = 0; y < height; y++) {
white = 0;
black = 0;
for (int x = 0; x < width; x++) {
if(_baseImage[y][x] == 0xFFFFFFFF){
white++;
}else{
black++;
}
}
if(black > white)
{
// line detected fist time
// now get height of line
int _starts = y;
_starts++;
int _end = 0;
for(; _starts < height; _starts++){
white = 0;
black = 0;
for(int _x = 0; _x < width; _x++){
if(_baseImage[_starts][_x] == 0xFFFFFFFF){
white++;
}else{
black++;
}
}
if(white > black){
// end of line height
_end = _starts;
_starts = y;
break;
}
}
white = 0;
black = 0;
for(;_starts < _end; _starts++){
for(int line = 0; line < width; line++){
if(_baseImage[_starts-1][line] != 0xFF000000
&& _baseImage[_starts-2][line] != 0xFF000000
|| _baseImage[_starts+1][line] != 0xFF000000
&& _baseImage[_starts+2][line] != 0xFF000000){
_baseImage[_starts][line] =0xFFFFFFFF;
}
}
}
y = _end;
}
}
return _baseImage;
}
最佳答案
在 Java 中数组是对象。将对象传递给方法时,传递给方法的值是其引用的副本。这会导致对对象的修改,作为参数传递给方法,对对象的引用进行修改,从而改变对象。
我遇到过关于 Java 如何按与原语和对象相关的值传递的最好描述之一是 story在 Java Ranch 上。
关于java - android按值传递?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/14742152/