2

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};
}
Huzaifah
  • 43
  • 3
  • 1
    So you just want the array that results from `back.randomise(lbl)` to be accessible in multiple classes? – parthlr Jul 14 '20 at 01:03
  • I'm having some difficulty understanding the second paragraph, specifically as to which Class is doing what. Can you describe them using different names? – Cliabhach Jul 14 '20 at 01:03
  • @parthlr, yes, that is exactly what I would like to do :) – Huzaifah Jul 14 '20 at 01:09
  • @Cliabhach, What I meant (apologies for not explaining it clearly) is that I would like the result of the method in my back-end class (which is the "randomise" method) to be accessible in both classes (called GameController and KeyController). However, the result of the method is already available to me in the KeyController class but is not accessible to me in the GameController class – Huzaifah Jul 14 '20 at 01:11
  • Thanks, that helps. This is JavaFX, right? I don't have enough experience with it to make a proper answer, but you should add the `javafx` tag if it belongs. – Cliabhach Jul 14 '20 at 01:22
  • 2
    You really need to post a [mre] to make it clear what the issue here is. It doesn't really make sense to provide the same set of UI components to two different controllers, because you can only use a given `Label` (for example) in one scene (and only once in that scene). You should really use a MVC approach store the *data* (not the labels) in a backend model, and share that model with both controllers. See, e.g. https://stackoverflow.com/questions/32342864/applying-mvc-with-javafx. Also see https://stackoverflow.com/questions/14187963. – James_D Jul 14 '20 at 13:07
  • 2
    OT: don't reinvent the wheel. Use [`Collections.shuffle`](https://docs.oracle.com/en/java/javase/14/docs/api/java.base/java/util/Collections.html#shuffle(java.util.List)) to randomly permute a list. – James_D Jul 14 '20 at 13:08

1 Answers1

2

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.

parthlr
  • 386
  • 3
  • 13
  • Do you think it's worth mentioning that the instances of `backend` will need to be the same for this to work? – Cliabhach Jul 14 '20 at 01:24
  • @Cliabhach I suppose it wouldn't hurt to mention it – parthlr Jul 14 '20 at 01:33
  • @Huzaifah "instance of a class" just means "object created by that class". I.e. they must be the same actual object (not just objects from the same class). – James_D Jul 14 '20 at 14:11
  • _what do you mean by "instance" of backend_ if you have to ask, it's time to work through a tutorial on language basics ;) – kleopatra Jul 14 '20 at 14:30