JAVA 集合的嵌套
■TestNest.java -> 测试嵌套.java
import java.util.ArrayList;
import java.util.List;
class Cat {
public String name;
}
public class TestNest {
public static void main(String[] args) {
Cat c = new Cat();
Cat c2 = new Cat();
Cat c3 = new Cat();
c.name = "Ashley";
List<Cat> cat = new ArrayList<Cat>();
cat.add(c);
cat.add(c2);
cat.add(c3);
c.name = "Sharrey";
c2.name = "Monique";
c3.name = "Choo";
System.out.println(cat.get(0).name);
System.out.println(cat.get(1).name);
System.out.println(cat.get(2).name);
}
}
■TestNest.java
莎瑞
莫尼克
周
■Main.java的主要内容
import java.util.ArrayList;
import java.util.List;
class Hero {
public String name;
}
public class Main {
public static void main(String[] args) {
Hero h = new Hero();
Hero h2 = new Hero();
h.name = "ミナト";
List<Hero> a = new ArrayList<Hero>();
a.add(h);
a.add(h2);
h.name = "スガワラ";
h2.name = "ネコ";
System.out.println(a.get(0).name);
System.out.println(a.get(1).name);
}
}
Main.java的执行结果是:スガワラ\nネコ。