Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/java/375.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
在类之间传递创建的数组[Java]_Java_Class_Object_Scope - Fatal编程技术网

在类之间传递创建的数组[Java]

在类之间传递创建的数组[Java],java,class,object,scope,Java,Class,Object,Scope,我正在开发一个基本的图像处理程序,目前有3个类:顶点、图形和操作 public class Vertex{ //Vertex functions, including tracking neighbours } public class Graph{ Vertex[][] aVertices; public Graph(Color[][] image){ int xTop = 0; for (int i = 0; i < im

我正在开发一个基本的图像处理程序,目前有3个类:顶点、图形和操作

public class Vertex{
    //Vertex functions, including tracking neighbours
}

public class Graph{

    Vertex[][] aVertices;

    public Graph(Color[][] image){

        int xTop = 0;
        for (int i = 0; i < image.length; i++){
            if (i > xTop){
                xTop = i;
            }
        }    

        int yTop = image.length;

        Vertex[][] aVertices = new Vertex[xTop][yTop];

        for(int i = 0; i < xTop; i++){
            for(int j = 0; j < yTop; j++){
                Vertex newVertex = new Vertex(i, j, image[i][j]);
                aVertices[i][j] = newVertex;
            }
        }    

        for(int i = 0; i < xTop; i++){
            for(int j = 0; j < yTop; j++){
                if(aVertices[i][j] == aVertices[i-1][j]){
                    aVertices[i][j].neighbourize(aVertices[i-1][j]);
                }
                if(aVertices[i][j] == aVertices[i+1][j]){
                    aVertices[i][j].neighbourize(aVertices[i+1][j]);
                }
                if(aVertices[i][j] == aVertices[i][j-1]){
                    aVertices[i][j].neighbourize(aVertices[i][j-1]);    
                }
                if(aVertices[i][j] == aVertices[i][j+1]){
                    aVertices[i][j].neighbourize(aVertices[i][j+1]);
                }
            }
        }

    }

}

public class Manipulations{

    //Image manipulations that access aVertices    

}
公共类顶点{
//顶点函数,包括跟踪邻域
}
公共类图{
顶点[][]避免;
公共图形(彩色[]图像){
int-xTop=0;
对于(int i=0;ixTop){
xTop=i;
}
}    
int yTop=image.length;
顶点[][]避免=新顶点[xTop][yTop];
for(int i=0;i

如您所见,在创建图形时,将创建一个2D数组,其中包含顶点对象,然后使用共享颜色为顶点对象指定适当的相邻状态。我现在想把这整个避免,并在操作中处理它,但不确定如何在适当的范围内移动它。有谁能给我指出正确的方向吗?

我想你对这方面的知识还不够

对于您的问题,您可以在
Graph
中定义这种方法

public Vertex[][] getVertices() {
  return this.aVertices;
}
操作中
,您可以使用

Graph g = new Graph(image);
Vertex[][] vertices = g.getVertices();

是的,我对面向对象编程还是相当陌生的,感谢上帝的回答!如果可能的话,我会接受。这里有一个有用的方法。请注意,有两个变量名为
aVertices
。我建议你了解两者之间的区别以及它们如何给你带来问题。(注意一个是字段变量,另一个是局部变量。这两个术语可以帮助您找到更多信息。)如何创建每个类的对象?