简体   繁体   中英

how to reassign array variable stored in an object in java

I'm new to this site so let me know if this sort of question is welcome here. I'm currently coding a class in java to store a set of integers in an array stored in an object and i'm having trouble reassigning the array variable store in the objects created.its not compiling as is(I'm a new programmer so i'm pretty sure i'm missing something simple here).

    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("}");
    }
    
}

edit - error message returned is:

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

First of all, you forgot to put the array inside the class, you can handle it privately like this before the constructor in this way:

private int [] array;

Constructor is used to initialize objects. If you create an empty constructor, you won't be able to pass it any parameters to initialize the array. You can create the constructor this way:

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

At line you indicated the compiler tells you that you cannot handle the method statically because you are working on an instance method. The "this" keyword is used as a reference to an instance. Since the static methods doesn't have (belong to) any instance you cannot use the "this" reference within a static method. So, you have to remove the static handling from the method. Also, since you want to return the array with the entered value, your method cannot be of type void. So, your method will be:

//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;
}

Since the length of the array was already available ( array.length ), it was not necessary to create the variable n, so I took the liberty of making some improvements to your code, to make it less redundant. Similarly, the method you created to print the array would be:

//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("} ");
}

However, once you've made these small changes your code should work fine. You can try to create a testing class like this, to check that everything works right:

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));       
    
    }
       
}

It looks like you are trying to access array however it cannot be seen by the addValue method.

You need to declare the array outside of the Set constructor:

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

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

Secondly, the reason for the error is that you cannot use this inside a static method. Static methods are not tied to an object (The Set class) so you need removed static from the method:

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

Then to use the method you would create the Set object and use addValue something like this:

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

The other option is to make the array a static value (it will no longer be specific to the object but shared with everything), but this is proberbly not the behaviour you want:

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

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM