0

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.

sawprogramming
  • 737
  • 1
  • 9
  • 15
Aagaard
  • 1
  • 1
  • this might help you: https://stackoverflow.com/questions/8738017/passing-an-array-from-class-a-to-class-b – SPUlx Apr 26 '19 at 14:55

3 Answers3

0

you can use list of arrays

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

You should declare the xArr() and yArr() methods public i.e.:

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();
sbke
  • 43
  • 1
  • 10
0

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

Jeffery Ma
  • 3,051
  • 1
  • 23
  • 26