如何在java中重新分配存储在对象中的数组变量

sshcrbum  于 2021-06-30  发布在  Java
关注(0)|答案(2)|浏览(263)

我是新到这个网站,所以让我知道,如果这种问题是欢迎在这里。我目前正在用java编写一个类,将一组整数存储在存储在对象中的数组中,在重新分配所创建对象中的数组变量存储时遇到了问题。它不是按原样编译的(我是一个新程序员,所以我很确定我在这里遗漏了一些简单的东西)。

public class Set 
    { 
        // constructor  
        Set() 
        {
        int array[] = {};
        } 

    //adds a value to the set
    public static void addValue(int [] array, int element) 
    {    
         int i; 
         int n = array.length;
         int newArray[] = new int[n + 1]; 
        //copy original array into new array
        for (i = 0; i < n; i++) 
              newArray[i] = array[i]; 

        //add element to the new array
        newArray[n] = element;
        //the issue is this line here
        this.array = newArray;
    } 

    //print the set 
    public static void printSet(int [] array) 
    {    
         int i;
         int n = array.length;
         System.out.print("{");
         for (i = 0; i < n; i++) 
         {
            System.out.print(array[i]);
         }
         System.out.println("}");
    }

}

编辑-返回的错误消息是:

Set.java:23: error: non-static variable this cannot be referenced from a static context
        this.array = newArray;
        ^
Set.java:23: error: cannot find symbol
        this.array = newArray;
            ^
  symbol: variable array
2 errors
bzzcjhmw

bzzcjhmw1#

看起来你正在尝试访问 array 但是它不能被 addValue 方法。
你需要申报 array 外面的 Set 施工单位:

public class Set 
{ 
    //New location to declear the array
    int array[];

    // constructor  
    Set() 
    {
        //We can still initialize the array here
        array[] = {};
    }

其次,错误的原因是您不能使用 this 在静态方法中。静态方法不绑定到对象(set类),因此需要从方法中删除静态方法:

//static removed from this line
public void addValue(int [] array, int element) 
    {

然后使用方法创建 Set 对象和用途 addValue 像这样:

Set exampleSet = new Set();
exampleSet.addValue(yourArray, index);

另一个选项是使数组成为静态值(它将不再特定于对象,而是与所有对象共享),但这可能不是您想要的行为:

public class Set 
{ 
    //New location to declear the array
    static int array[];

//And to access the object you could use
Set.array = newArray;
jhdbpxl9

jhdbpxl92#

首先,你忘了把数组放在类里面,你可以像这样在构造函数之前私下处理它:

private int [] array;

构造函数用于初始化对象。如果创建一个空构造函数,则无法向其传递任何参数来初始化数组。可以通过以下方式创建构造函数:

Set (int [] array){
        this.array = array;
}

在您指示的第行,编译器告诉您不能静态地处理该方法,因为您正在处理一个示例方法。“this”关键字用作示例的引用。因为静态方法没有(属于)任何示例,所以不能在静态方法中使用“this”引用。因此,必须从方法中移除静态处理。另外,由于要返回具有输入值的数组,因此方法的类型不能为void。所以,你的方法是:

//adds a value to the set
public int [] addValue(int [] array, int element){    

    int newArray[] = new int[array.length + 1]; 

    for (int i = 0; i < array.length; i++) 
          newArray[i] = array[i]; 

    newArray[array.length] = element;
    return newArray;
}

由于数组的长度已经可用(array.length),因此不必创建变量n,因此我冒昧地对代码进行了一些改进,以减少冗余。类似地,您创建的打印数组的方法是:

//print the set 
public void printSet(int [] array){    

     System.out.print("{ ");
     for (int i = 0; i < array.length; i++){
        System.out.print(array[i] + " ");
     }
     System.out.println("} ");
}

但是,一旦您做了这些小的更改,您的代码就可以正常工作了。您可以尝试创建这样一个测试类,以检查一切是否正常:

public class TestingSet {

    public static void main (String [] args){

        //creating array
        int [] array = {1, 2, 3};

        //creating an instance of Set class
        Set s = new Set(array);

        //printing array
        s.printSet(array);

        //printing array with new value
        s.printSet(s.addValue(array,4));       

    }

}

相关问题