赞
踩
- package collection;
-
- import java.util.ArrayList;
-
- import charactor.Hero;
-
- public class TestCollection {
- @SuppressWarnings("rawtypes")
- public static void main(String[] args) {
- //容器类ArrayList,用于存放对象
- ArrayList heros = new ArrayList();
- heros.add( new Hero("盖伦"));
- System.out.println(heros.size());
-
- //容器的容量"capacity"会随着对象的增加,自动增长
- //只需要不断往容器里增加英雄即可,不用担心会出现数组的边界问题。
- heros.add( new Hero("提莫"));
- System.out.println(heros.size());
-
- }
-
- }

- package collection;
-
- import java.util.ArrayList;
-
- import charactor.Hero;
-
- public class TestCollection {
- public static void main(String[] args) {
- ArrayList heros = new ArrayList();
-
- // 把5个对象加入到ArrayList中
- for (int i = 0; i < 5; i++) {
- heros.add(new Hero("hero " + i));
- }
- System.out.println(heros);
-
- // 在指定位置增加对象
- Hero specialHero = new Hero("special hero");
- heros.add(3, specialHero);
-
- System.out.println(heros.toString());
-
- }
-
- }

- package collection;
-
- import java.util.ArrayList;
-
- import charactor.Hero;
-
- public class TestCollection {
- public static void main(String[] args) {
- ArrayList heros = new ArrayList();
-
- // 初始化5个对象
- for (int i = 0; i < 5; i++) {
- heros.add(new Hero("hero " + i));
- }
- Hero specialHero = new Hero("special hero");
- heros.add(specialHero);
-
- System.out.println(heros);
- // 判断一个对象是否在容器中
- // 判断标准: 是否是同一个对象,而不是name是否相同
- System.out.print("虽然一个新的对象名字也叫 hero 1,但是contains的返回是:");
- System.out.println(heros.contains(new Hero("hero 1")));
- System.out.print("而对specialHero的判断,contains的返回是:");
- System.out.println(heros.contains(specialHero));
- }
-
- }

- package collection;
-
- import java.util.ArrayList;
-
- import charactor.Hero;
-
- public class TestCollection {
- public static void main(String[] args) {
- ArrayList heros = new ArrayList();
-
- // 初始化5个对象
- for (int i = 0; i < 5; i++) {
- heros.add(new Hero("hero " + i));
- }
- Hero specialHero = new Hero("special hero");
- heros.add(specialHero);
-
- //获取指定位置的对象
- System.out.println(heros.get(5));
- //如果超出了范围,依然会报错
- System.out.println(heros.get(6));
-
- }
-
- }

- package collection;
-
- import java.util.ArrayList;
-
- import charactor.Hero;
-
- public class TestCollection {
- public static void main(String[] args) {
- ArrayList heros = new ArrayList();
-
- // 初始化5个对象
- for (int i = 0; i < 5; i++) {
- heros.add(new Hero("hero " + i));
- }
- Hero specialHero = new Hero("special hero");
- heros.add(specialHero);
-
- System.out.println(heros);
- System.out.println("specialHero所处的位置:"+heros.indexOf(specialHero));
- System.out.println("新的英雄,但是名字是\"hero 1\"所处的位置:"+heros.indexOf(new Hero("hero 1")));
-
- }
- }

- package collection;
-
- import java.util.ArrayList;
-
- import charactor.Hero;
-
- public class TestCollection {
- public static void main(String[] args) {
- ArrayList heros = new ArrayList();
-
- // 初始化5个对象
- for (int i = 0; i < 5; i++) {
- heros.add(new Hero("hero " + i));
- }
- Hero specialHero = new Hero("special hero");
- heros.add(specialHero);
-
- System.out.println(heros);
- heros.remove(2);
- System.out.println("删除下标是2的对象");
- System.out.println(heros);
- System.out.println("删除special hero");
- heros.remove(specialHero);
- System.out.println(heros);
-
- }
- }

- package collection;
-
- import java.util.ArrayList;
-
- import charactor.Hero;
-
- public class TestCollection {
- public static void main(String[] args) {
- ArrayList heros = new ArrayList();
-
- // 初始化5个对象
- for (int i = 0; i < 5; i++) {
- heros.add(new Hero("hero " + i));
- }
- Hero specialHero = new Hero("special hero");
- heros.add(specialHero);
-
- System.out.println(heros);
- System.out.println("把下标是5的元素,替换为\"hero 5\"");
- heros.set(5, new Hero("hero 5"));
- System.out.println(heros);
- }
- }

- package collection;
-
- import java.util.ArrayList;
-
- import charactor.Hero;
-
- public class TestCollection {
- public static void main(String[] args) {
- ArrayList heros = new ArrayList();
-
- // 初始化5个对象
- for (int i = 0; i < 5; i++) {
- heros.add(new Hero("hero " + i));
- }
- Hero specialHero = new Hero("special hero");
- heros.add(specialHero);
- System.out.println(heros);
- System.out.println("获取ArrayList的大小:");
- System.out.println(heros.size());
- }
- }

- package collection;
-
- import java.util.ArrayList;
-
- import charactor.Hero;
-
- public class TestCollection {
- public static void main(String[] args) {
- ArrayList heros = new ArrayList();
-
- // 初始化5个对象
- for (int i = 0; i < 5; i++) {
- heros.add(new Hero("hero " + i));
- }
- Hero specialHero = new Hero("special hero");
- heros.add(specialHero);
- System.out.println(heros);
- Hero hs[] = (Hero[])heros.toArray(new Hero[]{});
- System.out.println("数组:" +hs);
-
- }
- }

- package collection;
-
- import java.util.ArrayList;
-
- import charactor.Hero;
-
- public class TestCollection {
- public static void main(String[] args) {
- ArrayList heros = new ArrayList();
-
- // 初始化5个对象
- for (int i = 0; i < 5; i++) {
- heros.add(new Hero("hero " + i));
- }
-
- System.out.println("ArrayList heros:\t" + heros);
-
- //把另一个容器里所有的元素,都加入到该容器里来
- ArrayList anotherHeros = new ArrayList();
- anotherHeros.add(new Hero("hero a"));
- anotherHeros.add(new Hero("hero b"));
- anotherHeros.add(new Hero("hero c"));
- System.out.println("anotherHeros heros:\t" + anotherHeros);
- heros.addAll(anotherHeros);
- System.out.println("把另一个ArrayList的元素都加入到当前ArrayList:");
- System.out.println("ArrayList heros:\t" + heros);
-
- }
- }

- package collection;
-
- import java.util.ArrayList;
-
- import charactor.Hero;
-
- public class TestCollection {
- public static void main(String[] args) {
- ArrayList heros = new ArrayList();
-
- // 初始化5个对象
- for (int i = 0; i < 5; i++) {
- heros.add(new Hero("hero " + i));
- }
-
- System.out.println("ArrayList heros:\t" + heros);
- System.out.println("使用clear清空");
- heros.clear();
- System.out.println("ArrayList heros:\t" + heros);
-
- }
- }

- package collection;
-
- import java.util.ArrayList;
- import java.util.List;
-
- import charactor.Hero;
-
- public class TestCollection {
-
- public static void main(String[] args) {
- //ArrayList实现了接口List
-
- //常见的写法会把引用声明为接口List类型
- //注意:是java.util.List,而不是java.awt.List
- //接口引用指向子类对象(多态)
-
- List heros = new ArrayList();
- heros.add( new Hero("盖伦"));
- System.out.println(heros.size());
-
- }
-
- }

- package collection;
-
- import java.util.ArrayList;
- import java.util.List;
-
- import property.Item;
- import charactor.APHero;
- import charactor.Hero;
-
- public class TestCollection {
-
- public static void main(String[] args) {
-
- //对于不使用泛型的容器,可以往里面放英雄,也可以往里面放物品
- List heros = new ArrayList();
-
- heros.add(new Hero("盖伦"));
-
- //本来用于存放英雄的容器,现在也可以存放物品了
- heros.add(new Item("冰杖"));
-
- //对象转型会出现问题
- Hero h1= (Hero) heros.get(0);
- //尤其是在容器里放的对象太多的时候,就记不清楚哪个位置放的是哪种类型的对象了
- Hero h2= (Hero) heros.get(1);
-
- //引入泛型Generic
- //声明容器的时候,就指定了这种容器,只能放Hero,放其他的就会出错
- List<Hero> genericheros = new ArrayList<Hero>();
- genericheros.add(new Hero("盖伦"));
- //如果不是Hero类型,根本就放不进去
- //genericheros.add(new Item("冰杖"));
-
- //除此之外,还能存放Hero的子类
- genericheros.add(new APHero());
-
- //并且在取出数据的时候,不需要再进行转型了,因为里面肯定是放的Hero或者其子类
- Hero h = genericheros.get(0);
-
- }
-
- }

- package collection;
-
- import java.util.ArrayList;
- import java.util.List;
-
- import charactor.Hero;
-
- public class TestCollection {
-
- public static void main(String[] args) {
- List<Hero> genericheros = new ArrayList<Hero>();
- List<Hero> genericheros2 = new ArrayList<>();
-
- }
-
- }

- package collection;
-
- import java.util.ArrayList;
- import java.util.Iterator;
- import java.util.List;
-
- import charactor.Hero;
-
- public class TestCollection {
-
- public static void main(String[] args) {
- List<Hero> heros = new ArrayList<Hero>();
-
- // 放5个Hero进入容器
- for (int i = 0; i < 5; i++) {
- heros.add(new Hero("hero name " + i));
- }
-
- // 第一种遍历 for循环
- System.out.println("--------for 循环-------");
- for (int i = 0; i < heros.size(); i++) {
- Hero h = heros.get(i);
- System.out.println(h);
- }
-
- }
-
- }

- package collection;
-
- import java.util.ArrayList;
- import java.util.Iterator;
- import java.util.List;
-
- import charactor.Hero;
-
- public class TestCollection {
-
- public static void main(String[] args) {
- List<Hero> heros = new ArrayList<Hero>();
-
- //放5个Hero进入容器
- for (int i = 0; i < 5; i++) {
- heros.add(new Hero("hero name " +i));
- }
-
- //第二种遍历,使用迭代器
- System.out.println("--------使用while的iterator-------");
- Iterator<Hero> it= heros.iterator();
- //从最开始的位置判断"下一个"位置是否有数据
- //如果有就通过next取出来,并且把指针向下移动
- //直达"下一个"位置没有数据
- while(it.hasNext()){
- Hero h = it.next();
- System.out.println(h);
- }
- //迭代器的for写法
- System.out.println("--------使用for的iterator-------");
- for (Iterator<Hero> iterator = heros.iterator(); iterator.hasNext();) {
- Hero hero = (Hero) iterator.next();
- System.out.println(hero);
- }
-
- }
-
- }

- package collection;
-
- import java.util.ArrayList;
- import java.util.Iterator;
- import java.util.List;
-
- import charactor.Hero;
-
- public class TestCollection {
-
- public static void main(String[] args) {
- List<Hero> heros = new ArrayList<Hero>();
-
- // 放5个Hero进入容器
- for (int i = 0; i < 5; i++) {
- heros.add(new Hero("hero name " + i));
- }
-
- // 第三种,增强型for循环
- System.out.println("--------增强型for循环-------");
- for (Hero h : heros) {
- System.out.println(h);
- }
-
- }
-
- }

Copyright © 2003-2013 www.wpsshop.cn 版权所有,并保留所有权利。