我知道有很多方法可以存储来自另一个类的数组,但是每个人(甚至初学者)都可以轻松理解的最佳方法是什么?
这里是示例代码:
第一类://这是获取要存储的数组的位置
public class Example{
private int[] Numbers = {3,2,1}//sample numbers
}
第二类://这是存储数组的位置
public class Example2{
public void Storage{
int[] NumberStorage = /* this is where the Numbers from the
1st class to be stored */
}
}
最佳答案
在您的 Example
类中创建一个 getter
:
public int[] getNumbers() { return Numbers; }
然后在您的 Example2
类中,您可以实例化 Example
的实例并调用 getter
,如下所示:
public void storage(){
Example example = new Example();
int[] NumberStorage = example.getNumbers();
}
关于java - 在java中存储另一个类的数组,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/35670803/