简体   繁体   中英

How to transfer array from one class to another?

I am trying to get data from one array in a class to another class (so that I can copy the result). I know that in general you would use a get-set method to achieve this, however, I am not sure of how to accomplish that with the classes I have.

This method (in my back-end class) randomizes an array of labels and I want the result in 2 different classes. The one class I can get the result because I have an array of Labels, as is also the case in the other class (but I want the same result from the first class, not another different randomized array)

public Label[] randomise(Label[] lbl){
        Random randomiseArr = new Random();
        for (int i = 0; i < lbl.length; i++) {
            int randomIndexToSwap = randomiseArr.nextInt(lbl.length);
            Label temp = lbl[randomIndexToSwap];
            lbl[randomIndexToSwap] = lbl[i];
            lbl[i] = temp;
        }
        return lbl;
    }

This is my first class (the one where I get my result, the labels have been created above - i just didn't include those):

public void initialize(URL url, ResourceBundle rb) {
        
        backend back = new backend();
        
        Label[] lbl = {t1, t2, t3, t4, t5, t6, t7, t8, t9, t10, t11, t12, t13, t14,
            t15, t16, t17, t18, t19, t20, t21, t22, t23, t24, t25};

        int ranClr = (int) (Math.random() * (10 - 1)) + 1;

        if (ranClr <= 5) {
            back.redTeam(back.randomise(lbl), clr1, clr2, clr3, clr4);
        } else {
            back.blueTeam(back.randomise(lbl), clr1, clr2, clr3, clr4);
            
        }
        
        
    }

And this is the class in which I would like the exact same result of the randomizing method:

    public void initialize(URL url, ResourceBundle rb) {
        
        backend back = new backend();
        
        Button[] btn = {btn1, btn2, btn3, btn4, btn5, btn6, btn7, btn8, btn9,
            btn10, btn11, btn12, btn13, btn14, btn15, btn16, btn17, btn18, btn19,
            btn20, btn21, btn22, btn23, btn24, btn25};

        Label[] lbl = {lbl1, lbl2, lbl3, lbl4, lbl5, lbl6, lbl7, lbl8, lbl9,
            lbl10, lbl11, lbl12, lbl13, lbl14, lbl15, lbl16, lbl17, lbl18, lbl19,
            lbl20, lbl21, lbl22, lbl23, lbl24, lbl25};
}

In your backend class, I would suggest that you have a field that can hold the randomized array. This way, you can access the array between multiple classes through a getter.

For example, you could have a field such as this at the top of your backend class:

private Label[] randomisedLabels;

Then, you can have a getter for the the field:

public Label[] getRandomisedLabels() {
    return randomisedLabels;
}

Now we can modify your randomise method to have a return type of void and instead store the result in randomisedLabels :

public void randomise(Label[] lbl){
    Random randomiseArr = new Random();
    for (int i = 0; i < lbl.length; i++) {
        int randomIndexToSwap = randomiseArr.nextInt(lbl.length);
        Label temp = lbl[randomIndexToSwap];
        lbl[randomIndexToSwap] = lbl[i];
        lbl[i] = temp;
    }
    // Instead of returning the array, store it so it can be used later
    randomisedLabels = lbl;
}

Doing it this way will allow you to call randomise once and use the result of the call with getRandomisedLabels() multiple times without the data being manipulated.

EDIT: As Cliabhach mentioned below, this approach only works if you are using the same instance of the backend class since a different array is stored in each instance of the class.

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