Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/java/309.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_Arrays_Object - Fatal编程技术网

在Java中清除多维对象

在Java中清除多维对象,java,arrays,object,Java,Arrays,Object,我想将4个数组从一个类中的方法传递到一个B类中的方法。 我在类B中的方法中创建了一个类A的实例来获取数组 类中的方法定义为Object[],我使用: return new Object[]{Array1,Array2,Array3,Array4}; 返回数组 在B类的方法中,我得到一个对象定义为的数组: private Object outGeoObj[] = new Object[4]; 我正在成功检索数组,但我想在再次使用之前清除该对象。我试过: public void ClearVal

我想将4个数组从一个类中的方法传递到一个B类中的方法。 我在类B中的方法中创建了一个类A的实例来获取数组

类中的方法定义为Object[],我使用:

return new Object[]{Array1,Array2,Array3,Array4};
返回数组

在B类的方法中,我得到一个对象定义为的数组:

private Object outGeoObj[] = new Object[4];
我正在成功检索数组,但我想在再次使用之前清除该对象。我试过:

public void ClearValues(){
    if (outGeoObj != null){
        outGeoObj = null;
    }
    else{
        System.out.println("Object is null");
    }  
}
但它不起作用。有什么建议吗

最简单的工作示例:

B类:

public class MainFem {

private OutGeoMesh outmesh;
private Object outGeoObj[] = new Object[4]; // [0: Xmpoint, 1: Ympoint, 2: Vec, 3: numpoints]


public MainFem() {
    outmesh = new OutGeoMesh();
}

public void ClearValues(){

    if (outGeoObj != null){
        for(int i = 0; i < outGeoObj.length; i++) {
             outGeoObj[i] = null;
        }
    }
    else{
        System.out.println("Object is null");
    }  

} // END Method ClearValues



public void MainStart(int Xpoint[][], int Ypoint[][], int nump[], int c2, int Line[][][], DrawPanel drawPanel){

    outGeoObj = outmesh.createOutGeomesh(Xpoint, Ypoint, nump, c2, Line, drawPanel);

     int temp = (int[][]) outGeoObj[3];
     System.out.println(temp[0][0]);

   }// End Method MainStart
} // END CLASS MainFem
公共类MainFem{
私人露台;
私有对象outGeoObj[]=新对象[4];//[0:Xmpoint,1:Ympoint,2:Vec,3:numpoints]
公共服务{
outmesh=新OutGeoMesh();
}
public void ClearValues(){
if(outGeoObj!=null){
for(int i=0;i
A类:

public class OutGeoMesh {

private double Xmpoint[][][] = new double[500][200][20];  
private double Ympoint[][][] = new double[500][200][20];
private double Vec[][][] = new double[500][2][20];  
private int numpoints[][] = new int[500][20];  

public OutGeoMesh() {
    // TODO Auto-generated constructor stub
}

public Object[] createOutGeomesh(int Xpoint[][], int Ypoint[][], int nump[], int c2, int Line[][][], DrawPanel drawPanel) {

  for (int j = 0; j <= c2; j++) {
        for (int i = 0; i < nump[j]; i++) {

            Vec[i][0][j] = i;
            Vec[i][1][j] = i+1;

            Xmpoint[i][0][j] = Xpoint[i][j];
            Ympoint[i][1][j] = Ypoint[i][j];

            numpoints[i][j] = numpoints[i][j] + 1;

      } // END FOR i

    } // END FOR j


 return new Object[]{Xmpoint,Ympoint,Vec,numpoints};

} // END METHOD createOutGeomesh
// ---------------------------------

} // END CLASS OutGeoMesh
public-class-OutGeoMesh{
私人双倍Xmpoint[][]=新双倍[500][200][20];
私家双精度点[][]]=新双精度点[500][200][20];
私家双张Vec[][]=新双张[500][2][20];
私有整数整数[][]=新整数[500][20];
公共厕所(){
//TODO自动生成的构造函数存根
}
公共对象[]createOutGeomesh(int-Xpoint[]]、int-Ypoint[]]、int-nump[]、int-c2、int-Line[][]]、DrawPanel DrawPanel){

对于(int j=0;j您需要执行以下操作:

Arrays.fill(outGeoObj, null);

代码不起作用的原因是,您只是在删除对数组的引用,但代码的其他部分仍在使用相同的数组。通过使用
数组。填充
可以删除数组的内容。

从这些问题中还不清楚类
A
B
是如何交互的,以及类
B
是如何交互的实际的问题是什么?如果你从相关的类中发布更多的代码会有所帮助。你说它不起作用是什么意思?从你的代码显示的情况来看,你没有清除值,你只是简单地使
outGeoObj==null
。我想你想重置所有的值,这样你就可以执行
outGeoObj=newobject>[outGeoObj.length];
。或者您可能希望清除数组中的数组,因此对
outGeoObj
使用
for循环
,然后对数组中的每个数组使用
嵌套for循环
,并相应地设置它们的值。在数组中循环,为值指定null?@CoderMusgrove我尝试过outGeoObj=new Object[outGeoObj.length];但它不起作用。