Dog dog1=newDog("ahhua",12); Dog dog2=newDog("a",15); Dog dog3=newDog("ah",13); Dog dog4=newDog("ah",13); //迭代器遍历 List list=newArrayList(); list.add(dog1); list.add(dog2); list.add(dog3); list.add(dog4); //支持索引 System.out.println(list.get(2)); for (Object a:list){ System.out.println(a); }
Dog dog1=newDog("ahhua",12); Dog dog2=newDog("a",15); Dog dog3=newDog("ah",13); Dog dog4=newDog("ah",13); //迭代器遍历 List list=newArrayList(); list.add(dog2); list.add(dog1); list.add(dog2); list.add(dog3); //dog4插入到前面 list.add(0,dog4); //返回位置 System.out.println(list.indexOf(dog2)); System.out.println(list.lastIndexOf(dog2)); //移除指定的元素,并且返回 System.out.println(list.remove(2).toString()); //指定位置为指定元素 list.set(1,dog4); System.out.println(list.subList(0,2));