1月21日 - ArrayList 练习
import java.util.ArrayList; public class LXArrayList { public static void main(String[] args) { // TODO 自动生成的方法存根 ArrayList<Integer> l = new ArrayList<Integer>(); l.add(123); l.add(456); l.add(789); for(int i = 0; i < 11; i++) { l.add(i); } System.out.println("列表中的元素有:" + l); System.out.print("列表中的元素有:"); for(int t : l) { System.out.print(" " + t); } System.out.print("\r" +"列表中的元素有:"); for(int i = 0; i < l.size();i++) { System.out.print(l.get(i) + " "); } ArrayList<Shu> sz = new ArrayList<Shu>(); sz.add(new Shu("樟树",15,20.5)); Shu ys = new Shu(); ys.setShuMIng("杨树"); ys.setGaoDu(13); ys.setKuanDu(15.3); sz.add(ys); System.out.println(); for (int i = 0; i < sz.size();i++) { System.out.println(sz.get(i)); } /* 输出结果如下: 列表中的元素有:[123, 456, 789, 0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10] 列表中的元素有: 123 456 789 0 1 2 3 4 5 6 7 8 9 10 列表中的元素有:123 456 789 0 1 2 3 4 5 6 7 8 9 10 Shu [shuMIng=樟树, gaoDu=15, kuanDu=20.5] Shu [shuMIng=杨树, gaoDu=13, kuanDu=15.3] */ } }
public class Shu { public Shu() { super(); } public Shu(String shuMIng, int gaoDu, double kuanDu) { super(); this.shuMIng = shuMIng; this.gaoDu = gaoDu; this.kuanDu = kuanDu; } private String shuMIng; private int gaoDu; private double kuanDu; public String getShuMIng() { return shuMIng; } public void setShuMIng(String shuMIng) { this.shuMIng = shuMIng; } public int getGaoDu() { return gaoDu; } public void setGaoDu(int gaoDu) { this.gaoDu = gaoDu; } public double getKuanDu() { return kuanDu; } public void setKuanDu(double kuanDu) { this.kuanDu = kuanDu; } @Override public String toString() { return "Shu [shuMIng=" + shuMIng + ", gaoDu=" + gaoDu + ", kuanDu=" + kuanDu + "]"; } }