简体   繁体   中英

How do I pass 2 arrays from one class to another

I am trying to pass 2 arrays I've created in this class, xArray[] and yArray[], to another class:

public class RungeCalculation {
    private double x;
    private double F1;
    private double F2;
    private double F3;
    private double F4;

    double[] xArray;
    double[] yArray;   

    public void solve(double y, double h, int j, double i) {      
        xArray = new double[j];
        yArray = new double[j];

        // code left out

        xArray[dex] = x;
        yArray[dex] = y;

        x = x + h;  
    }  

    private double f(double x, double y, double i){
         return i; 
    } 
}

How can I pass the arrays from the RungeCalculation class into the RungeResult class shown below and run them through the loops: (x and y are JTexArea)?

public class RungeResult extends JFrame {
    RungeResult() {
        // code left out

        for(int i = 0; i < xArray.length; i++) {
            x.append(" " + Double.toString(xArray[i]) + "\n");
        }
        for(int i = 0; i < yArray.length; i++) {
            y.append(" " + Double.toString(yArray[i]) + "\n");
        }
    }
}

I tried to make to function that called the arrays, and making them global and neither seemed to work.

double[] xArr() {
    return xArray;
}
double[] yArr() {
    return yArray;
}

and calling them in the other class:

double[] xArray = Arrays.xArr();
double[] yArray = Arrays.yArr();

Which did not seem to work out.

you can use list of arrays

list<Array[]> mylist = new list<Array[]>();
mylist.Add(xArray);
mylist.Add(yarray);

You should declare the xArr() and yArr() methods public ie:

public double[] xArr(){
    return xArr();
}

Then you can use them in the following way:

RungeCalculation rungeCalculation = new RungeCalculation();
//calculate the arrays here somehow
double[] xArray = rungeCalculation.xArr();
double[] yArray = rungeCalculation.yArr();

You can use a container class to hold two arrays

like below

public class Pair<F, S> {
    public F first;
    public S second;

    /**
     * Constructor for a Pair.
     *
     * @param first the first object in the Pair
     * @param second the second object in the pair
     */
    public Pair(F first, S second) {
        this.first = first;
        this.second = second;
    }
}

The usage:

class A{

   public Pair<int[], int[]> getTwoArrays() {
     //according your logic init xArray, yArray
     int[] xArray = new int[] {1, 2, 3, 4};
     int[] yArray = new int[] {1, 2, 3, 4};
     return new Pair(xArray, yArray);
   }

}


class B {


   public void useTwoArrays(Pair<int[], int[]> pairs) {
      int[] xArray = pairs.first;
      int[] yArray = pairs.second;
      // then do what you want to do

   }

}

about the Pair if you are in Android environment click here , if you are in java environment just create a class like above Pairs, this is Java generic click here

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