Java的集合与迭代器

一、集合
1.数据与集合
   数组弊端:
1)只能插入相同类型的元素(基本数据类型和引用数据类型都能保存)
2)长度一旦确定就不能改变,要添加超出数组长度个数的元素,比较复杂
2.创建集合类的原因
 数组操作数据的弊端
 用来代替数组使用
3. 集合特点:
  1)能添加不同类型的元素
  2)长度可变
 注意:集合中只能添加引用数据类型(对象类型)
二、集合中的方法
package com.lanou3g.collection;

import java.util.ArrayList;
import java.util.Collection;

import com.lanou3g.bean.Student;

/*
 * 集合Collection接口中的方法
 * 集合可以保存不同数据类型的数据
 * 保存基本数据类型是以自动装箱的形式,进行存储
 * 
 */
//注解 rawtypes 保持原有类型
//unchecked 不检查插入数据类型
//放到类上 整个类 不写泛型,都不会报黄
@SuppressWarnings({ "rawtypes", "unchecked" })

public class Demo02 {
public static void main(String[] args) {
     //fun1();

     //fun2(collection);
     //fun3();
     //fun4();
    //addAll
    //fun5();
    //fun7();
    //fun8();
}

/**
 * 
 */
public static void fun8() {
    //把两个集合交集取出来,保存在调用的集合里
    //(谁调用的保存在谁那)
    Collection c1 = new ArrayList();
    c1.add("a");
    c1.add("b");
    c1.add("c");
    //如果c1集合 和c2集合 求出交集 放到c1中,
    //如果c1和原来对比,
    //发生变化,返回true
    //如果不发生变化 返回 false
    Collection c2 = new ArrayList();
    c2.add("x");
    c2.add("y");
    c2.add("z");
    boolean isRetainAll = c1.retainAll(c2);
    System.out.println(c1);
    System.out.println(c2);
    System.out.println(isRetainAll);
}

/**
 * 
 */
public static void fun7() {
    Collection c1 = new ArrayList();
    c1.add("a");
    c1.add("b");
    c1.add("c");
    Collection c2 = new ArrayList();
    c2.add("x");
    c2.add("y");
    c2.add("z");

    //删除的是:经两个集合的交集的元素删除
    //谁调用,删除谁的集合元素,另外的集合里的元素不变
    //只删除交集,重复也可以删除
    boolean isRemoveAll = c1.removeAll(c2);
    System.out.println(c1+"--c1");
    System.out.println(c2+"--c2");
    System.out.println(isRemoveAll);
}

/**
 * 
 */
public static void fun5() {
    Collection c1 = new ArrayList();
    c1.add("a");
    c1.add("b");
    c1.add("c");
    Collection c2 = new ArrayList();
    c2.add("t");
    c2.add("r");
    c2.add("w");
    //给集合1添加一个元素,这个元素是c2
    //c1[a,b,c,[t,r,w]]; c2[t,r,w];
    //c1.add(c2);
    //直接调用要测试的方法
    //把c2集合 中每一个元素取出来,添加到c1集合末尾

  boolean isAddAll =    c1.addAll(c2);
  System.out.println(c1+"---c1");
  System.out.println(c2+"---c2");
  System.out.println(isAddAll);
}

/**
 * 错误的强转方式
 */
public static void fun4() {
    Collection collection = new ArrayList();
     collection.add(new Student("冯", 14));
     collection.add(new Student("小小",31));
     collection.add(new Student("菜菜", 12));
     //集合转数组
     //相当于有一个向上转型
      //  Object[] array = collection.toArray();
        Student [] array = (Student[])collection.toArray();
        for (int i = 0; i < array.length; i++) {
            System.out.println(array[i].getName());
        }
}

/**
 * 
 */
public static void fun3() {
    /*
      * 创建一个集合
      * 保存3个学生
      * 遍历打印 学生姓名 (只打印姓名)
      * 
      */
     Collection collection = new ArrayList();
     collection.add(new Student("冯", 14));
     collection.add(new Student("小小",31));
     collection.add(new Student("菜菜", 12));
     //集合转数组
     //相当于有一个向上转型
     //对象调用方法 强转注意:把数组中每个对象进行强转
     //而不是把保存对象的容器(数组)强转
        Object[] array = collection.toArray();
        //遍历数组
        for (int i = 0; i < array.length; i++) {
            //array[i]直接从数组中取出来,是Object类型
            //要想使用Student类中的方法,需要强转
            //必须是这个类型,才能强转
            Student student = (Student)array[i];
            System.out.println(student.getName());
        }
}

/**
 * @param collection
 */
public static void fun2(Collection collection) {
    //添加 a b c d
     collection.add("a");
     collection.add("b");
     collection.add("c");
     collection.add("d");
     //遍历集合中的每一个元素
    Object [] array = collection.toArray();
     for (int i = 0; i < array.length; i++) {
        System.out.println(array[i]);
    }
}

/**
 * 集合的基本方法
 */
public static void fun1() {
    //创建一个集合 ,多态的声明方法
     //添加一个注解:保持原有类型的注解
    Collection collection = new ArrayList();
    //添加方法
    //什么时候 会添加失败?
    //ArrayList中的add 不可能返回失败
    //不能返回失败为什么还要设计返回值呢---思想
    //方法要适应所有的子类 子接口
boolean b = collection.add("a");
boolean b1 =collection.add("b");
boolean b2 =    collection.add("c");

    //当你向集合中添加基本数据类型的时候
    //系统会帮你进行 自动装箱 把基本数据类型变成它的包装类
boolean b3 =    collection.add(1);
boolean b4 =    collection.add(true);
    System.out.println(b);
    System.out.println(b1);
    System.out.println(b2);
    System.out.println(b3);
    System.out.println(b4);
    //打印集合
    //System.out.println(collection.toString());

    //获取集合的长度
    System.out.println(collection.size());

    //判断是否包含某个元素
     boolean b7 = collection.contains("aa");
     System.out.println(b7);

     //从集合中删除一个元素
     //操作的是原集合--操作的是集合本身
    boolean b8 = collection.remove("b");
     System.out.println(b8);
     System.out.println(collection);

     //判断集合是否为空
     boolean b9 = collection.isEmpty();
     System.out.println(b9);

     //清空集合
     collection.clear();
     System.out.println(collection);
}
}
三、迭代器
package com.lanou3g.collection;

import java.util.ArrayList;
import java.util.Collection;
import java.util.Iterator;

import com.lanou3g.bean.Student;

/*
 * 迭代器(遍历)
 */
@SuppressWarnings({ "rawtypes","unchecked" ,"unused" })
public class Demo03 {
public static void main(String[] args) {
    //fun1();
    //fun2();
    /*
     * 创建一个集合
     * 保存三个学生
     * 使用迭代器遍历,打印学生姓名
     */
    Collection collection = new ArrayList();
    collection.add(new Student("小小", 11));
    collection.add(new Student("芳芳", 11));
    collection.add(new Student("海海", 11));
    //遍历集合
    Iterator iterator = collection.iterator();
    while(iterator.hasNext()) {
        Object next = iterator.next();
        Student student = (Student)next;
        System.out.println(student.getName());
    }
}

/**
 * 遍历集合
 */
public static void fun2() {
    Collection collection = new ArrayList();
    //迭代时 有一个指针,指向集合的首位置
    //每调用一次 next方法 这个指针向下移动一格
    collection.add("a"); //  
    collection.add("b"); //
    collection.add("c"); //
    collection.add("d"); //
    collection.add("e"); //
                         //⬅
    //遍历集合
    Iterator iterator = collection.iterator();
    //如果有元素,就继续获取,没元素,就停止循环

    while(iterator.hasNext()) {
        //注意:迭代时,循环中只能使用一次next()方法
        //
        Object next = iterator.next();
        System.out.println(iterator.next());
    }
}

/**
 * 
 */
public static void fun1() {
    Collection collection = new ArrayList();
    collection.add("a");
    collection.add("b");
    collection.add("c");
    collection.add("d");
    //获取集合中的迭代器
    Iterator iterator = collection.iterator();
    //从集合中取出元素
    //先判断集合中是否有元素,有在取,
  boolean isHasNext =  iterator.hasNext();
  System.out.println(isHasNext);
  //取出元素
  Object next = iterator.next();
  System.out.println(next);
}
}

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值