1):不定参数的使用
public class VariArgs { public static void main(String[] args) { test("aaa"); test("aaa", "bbb"); test("aaa", "bbb", "ccc"); } public static void test(String arg, String... args) { System.out.println(arg); System.out.println("----------------"); for (String s : args) { System.out.println(s); } } }
2):ArrayList的定义和遍历
//初始化 Soldier infantry1=new Infantry("张三"); Soldier infantry2=new Infantry("狗蛋"); Soldier caverlyMan=new CaverlyMan("李四"); Soldier artillery=new Artillery("王二"); List<Soldier> soldiers=new ArrayList<Soldier>(); //遍历 //第一种 for (Soldier soldier:soldiers){ soldier.attack(); } //第二种 for (int i=0;i<soldiers.size();i++){ //这里是soldiers.get(i) 而不是soldiers[i] soldiers.get(i).attack(); } //第三种 Iterator iterator=soldiers.iterator(); while (iterator.hasNext()){ iterator.next(); }
时间: 2024-11-05 14:55:37