[英]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).我正在尝试从 class 中的一个数组获取数据到另一个 class (以便我可以复制结果)。 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.
我知道通常您会使用 get-set 方法来实现这一点,但是,我不确定如何使用我拥有的类来实现这一点。
This method (in my back-end class) randomizes an array of labels and I want the result in 2 different classes.这个方法(在我的后端类中)随机化了一个标签数组,我希望结果在 2 个不同的类中。 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)
一个 class 我可以得到结果,因为我有一个标签数组,另一个 class 也是如此(但我希望第一个 class 的结果相同,而不是另一个不同的随机数组)
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):这是我的第一个 class (我得到结果的地方,标签已在上面创建 - 我只是没有包括那些):
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:这是 class 我想要随机化方法的完全相同的结果:
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.在您的
backend
class 中,我建议您有一个可以保存随机数组的字段。 This way, you can access the array between multiple classes through a getter.这样,您可以通过 getter 访问多个类之间的数组。
For example, you could have a field such as this at the top of your backend
class:例如,您可以在
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
:现在我们可以修改您的
randomise
方法,使其返回类型为void
,并将结果存储在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.这样做将允许您调用
randomise
一次,并使用getRandomisedLabels()
多次调用的结果,而无需操作数据。
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.编辑:正如下面提到的 Cliabhach,此方法仅在您使用
backend
class 的相同实例时才有效,因为在 class 的每个实例中存储了不同的数组。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.