简体   繁体   中英

Passing and Getting array as parameter to a function in java

Can anyone help me with returning array from a function X() . I want to pass that array to a function Y() as parameter in same class.

What I have:

int[] create()throws IOException {
    System.out.println("Enter Size of Array");
    BufferedReader b=new BufferedReader(new InputStreamReader(System.in)); 
    n=Integer.parseInt(b.readLine());
    //A=new int[n];
    System.out.println("Enter Array"); for(i=0;i<n;i++) {
        int y=Integer.parseInt(b.readLine());
        A[i]=y;
    }
    return A;
}

void getarray() {
}

You can store the argument to X() in a member variable, and then access the array through the member variable in Y() .

class YourClass {

    private int[] someArray;

    public void X(int[] argArray) {
        someArray = argArray;         // save it like this
        ...
    }

    public void Y() {
        ...
        someArray[3] = 2;             // access it here
    }
}

Note that if you may want to consider storing such temporary variables in a ThreadLocal<int[]> if you want to be thread safe.


Regarding the update: If A is a member variable, you can access A in getarray() , either by just referring to A or, this.A if it is shadowed by some local variable.

You pass an array into a method by declaring the method

public void myMethod(int[] ary);

and then, if you have an instance of an object, you do

int[] myAry = new int[9];
obj.myMethod(myArray);

now if your myMethod is calling your second method, it can just pass the reference to the array to the second method, which needs to accept an array argument. If not, then you need to store the array in a member field in your class.

class MyClass {
    int[] tmp;

    public void myMethod(int[] ary) {
        this.tmp = ary;
    }

    public void myMethod2(){
        // can use this.tmp, which will be null if it isn't set.
    }


}

To pass an array as parameter to a method you can use:

public void X(int[] array) {
}

If you want to access an array from two different methods of the same class, you can make the array a member of the class:

public class MyClass {
    int[] array = new int[10];

    public void X() {
    }

    public void Y() {
    }
}

Now both X and Y can access the array.

class Z
{
  private int a[];

  public void x(int input [])
  {
     a=input.clone();//store input values in 'a' safely
  }

  public void y()
  {
   //work with a [] here
   System.out.println(a[0]);
   }
}

You can pass anything to a method, arrays included. All you have to do is add the square brackets [] after the object or simple variable type (eg int[] input , char[] input , JTree[] trees , etc. etc.)

public void X(int[] input) {
    //do stuff with 'input'
    Y(input);
}

public void Y(int[] input) {
    //do stuff with 'input'
}

Note that the method Y accepts an array of the simple variable type int . If it didn't, you would have to store the array in a member field in your class in order to use your array in the Y method.

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