你好,我是Java的初学者,这是我的问题:
我有第一个类,有以下变量:
import java.util.ArrayList;
public class numbers {
private int number1 = 50;
private int number2 = 100;
}
我也有这门课:
import java.util.ArrayList;
public class test {
private numbers number;
}
我的问题是:我想存储number1&将number2变量放入ArrayList,然后从类test中访问此ArrayList.我怎样才能做到这一点?
解决方法:
import java.util.ArrayList;
public class numbers {
private int number1 = 50;
private int number2 = 100;
private List list;
public numbers() {
list = new ArrayList();
list.add(number1);
list.add(number2);
}
public List getList() {
return list;
}
}
和测试类:
import java.util.ArrayList;
public class test {
private numbers number;
//example
public test() {
number = new numbers();
List list = number.getList();
//hurray !
}
}
标签:java,arraylist
来源: https://codeday.me/bug/20191003/1851161.html