我有一堂课
Class A{
  string a;
  int b;
  getters and setters of a and b;
}
我想列出 A 类的对象。在 Java 中怎么可能
我还想设置 a 和 b 的值。
我有一堂课
Class A{
  string a;
  int b;
  getters and setters of a and b;
}
我想列出 A 类的对象。在 Java 中怎么可能
我还想设置 a 和 b 的值。
你可以使用数组...
 A[] listOfA = new A[]{new A(), new A(), new A(), new A(), new A()};
 A[] anotherListOfA = new A[5];
 for (int index = 0; index < anotherListOfA.length; index++) {
      anotherListOfA[index] = new A();
 }
查看数组以获取更多详细信息
你可以使用一个实现List
 List<A> listOfA = new ArrayList<>(5);
 for (int index = 0; index < 5; index++) {
      listOfA.add(new A());
 }
查看收藏以获取更多详细信息
List<A> aList = new ArrayList<A>();
A anObj = new A();
anObj.setA(1);
anObj.setB(1);
aList.add(anObj);
List<A> aList = new ArrayList<A>();
设定值
A a = new A();
a.setMethod("Hello World");
aList.add(a);
列出价值
for(A aa : aList){
System.out.println(aa.getMethod());
}
A a new A();
a.setA("aaa");
a.setB(4);
List<A> aList = new ArrayList<A>();
aList.add(a);
 ///    .... 
String a;
int b;
for(A a: aList){
   a = a.getA();
   b = a.getB();
 }
A a = new A();
a.setA("bbb");
a.setB(88);
List<A> list = new ArrayList<A>();
list.add(a);
/////.....
您可以声明 A 类型的数组
List<A> myList = new ArrayList<A>();
然后创建一个 A 对象
A a = new A();
a.setA = stringA; // or whatever you want to set on the object
然后将该对象添加到数组中
myList.add(a);
列表只是一个接口。所以你不能实例化一个List. 您必须使用实现List接口的类。例如ArrayList.
你可以试试这个
      ArrayList <A> listOfA = new ArrayList<A>(intitalCapacity);