[英]two-dimensional array between classes | java
i have one problem with inheritance. 我有一个继承问题。 I have 2 classes, (CreateAnArray,TheGame) and now - i have two-dimensional array which is created in 'CreateAnArray' class and i want to use it in 'TheGame' class, but every time i try to do it, it shows me 'null'. 我有2个类,(CreateAnArray,TheGame),现在-我有一个二维数组,它是在'CreateAnArray'类中创建的,我想在'TheGame'类中使用它,但是每次我尝试这样做时,它都会显示我“空”。
public class TheGame extends CreateAnArray{
public void insert(String k){
for(int i=0;i<arrayOX.length;i++){
for(int j=0;j<arrayOX.length;j++){
if(arrayOX[i][j] == k && (arrayOX[i][j] != "x" && arrayOX[i][j] != "y"))
arrayOX[i][j]=k;
System.out.println(arrayOX[i][j]);
}
}
}
And the second class: 第二类:
public class CreateAnArray {
public String[][] arrayOX = new String[3][3];
public void create(){
int k=1;
for(int i=0;i<arrayOX.length;i++){
for(int j=0;j<arrayOX.length;j++){
arrayOX[i][j]=Integer.toString(k++);
}
}
}
public void show(){
for(int i=0;i<arrayOX.length;i++){
for(int j=0;j<arrayOX.length;j++){
System.out.print(arrayOX[i][j]);
}
System.out.println();
}
}
I've tried a returning method, creating objects but nothing helps, what should I do? 我尝试了一种返回方法,创建了对象,但没有帮助,该怎么办?
Don't do this. 不要这样 Have your first class operate normally. 让您的头等舱正常运行。 When it needs arrayOX
, have a function return that array to the first class. 当需要arrayOX
,可以使用一个函数将该数组返回到第一类。 Don't extend the class here. 不要在这里扩展课程。
public String[] create(){
String[] arrayOX = new String[3][3];
int k=1;
for(int i=0;i<arrayOX.length;i++){
for(int j=0;j<arrayOX.length;j++){
arrayOX[i][j]=Integer.toString(k++);
}
}
return arrayOX;
}
Then, call this function to get arrayOX
in the other class. 然后,调用此函数以在另一个类中获取arrayOX
。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.