集合

集合

概念:

​ 面向对象语言对事物的体现都是以对象的形式,所以为了方便对多个对象的操作,Java就提供了集合类。

数组和集合类同是容器,有何不同?

​ 数组虽然也可以存储对象,但长度是固定的;集合长度是可变的。数组中可以存储基本数据类型,集合只能存储对象。

集合类的特点

​ 集合只用于存储对象,集合长度是可变的,集合可以存储不同类型的对象。

1.Collection接口概述

​ Collection 层次结构中的根节点。Collection 表示一组对象,这些对象也称为 collection 的元素。一些 collection 允许有重复的元素,而另一些则不允许。一些 collection 是有序的,而另一些则是无序的。

**Collection****接口成员方法

1.boolean add(Object obj):添加指定元素

2.boolean remove(Object obj):移除指定元素

3.void clear():清除所有元素

4.boolean contains(Object o):判断集合中是否包含指定的元素

5.boolean isEmpty():判断集合是否为空

6.int size():返回集合中元素的个数

7.boolean addAll(Collection c):添加指定集合中的元素

8.boolean removeAll(Collection c):移除指定集合中的元素

9.boolean containsAll(Collection c):判断是否包含指定元素

10.boolean retainAll(Collection c):判断两个集合共有的元素

package org.wdit.unit01.Collection;

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

/**
 * Collection类:是集合继承体系结构中的根节点
 *          添加功能:1.添加元素  boolean add(Object o)
 *                  2.添加集合元素 boolean addAll(Collection c)
 *          删除功能:1.删除元素  boolean remove(Object o)
 *                  2.删除集合元素 boolean removeAll(Collection c)
 *                  3.删除所有元素 void clear()
 *          判断功能:1.判断是否包含指定元素 boolean contains(Object o)
 *                  2.判断集合中是否包含指定集合中的所有元素 boolean containsAll(Collection c)
 *                  3.判断集合是否为空 boolean isEmpty
 *          长度功能:1.获取集合中的元素个数 int size()
 *
 *          求交集: 1.boolean retainAll(Collection c)
 */
public class CollectionDemo {
   
    public static void main(String[] args) {
   
        Collection collection = new ArrayList();
        Collection c = new ArrayList();
        //添加元素  boolean add(Object o)
        collection.add("hello");
        collection.add("world");
        System.out.println(collection);

        //添加集合元素 boolean addAll(Collection c)
        collection.addAll(collection);
        System.out.println(collection);
        c.add("world");
        System.out.println("-------------------------------------------------------------------");

        //删除元素  boolean remove(Object o)
        collection.remove("hello");
        System.out.println(collection);


        //删除集合元素 boolean removeAll(Collection c)
        collection.removeAll(c);
        System.out.println(collection);

        //3.删除所有元素 void clear()
        collection.clear();
        System.out.println(collection);
        System.out.println("------------------------------------------------------------------");

        //1.判断是否包含指定元素 boolean contains(Object o)
        collection.add("hello");
        collection.add("world");
        System.out.println(collection);
        boolean b = collection.contains("hello");
        System.out.println(b);
        //2.判断集合中是否包含指定集合中的所有元素 boolean containsAll(Collection c)
        boolean b2 = collection.containsAll(c);
        System.out.println(b2);

        //3.判断集合是否为空 boolean isEmpty
        c.clear();
        boolean a = c.isEmpty();
        boolean b4 = collection.isEmpty();
        System.out.println(a);
        System.out.println(b4);

        //长度功能:1.获取集合中的元素个数 int size()
        int i = collection.size();
        System.out.println(i);

        //求交集: 1.boolean retainAll(Collection c)
        c.add("hello");
        c.add("Java");
        collection.retainAll(c);
        System.out.println(collection);

    }
}

11.Object[] toArray() 把集合转成数组,可以实现集合的遍历

12.Iterator iterator() 迭代器,集合的专用遍历方式

package org.wdit.unit01.Iterator;

import java.util.ArrayList;
import java.util.Iterator;
import java.util.List;

/**
 * 迭代器Iterator
 *      boolean hasNext()如果仍有元素可以迭代,则返回 true。
 *      E next()返回迭代的下一个元素。
 *
 */
public class IteratorDemo {
   
    public static void main(String[] args) {
   
        //声明集合
        List list = new ArrayList();
        //添加元素
        list.add("hello");
        list.add("Java");
        list.add("world");
        //获取该集合对应的迭代器
        Iterator iterator = list.iterator();
        //boolean hasNext()
//        boolean b = iterator.hasNext();
//        E next()
//        Object o = iterator.next();
//        System.out.println(b);
//        System.out.println(o);
//
//        b=iterator.hasNext();
//        o=iterator.next();
//        System.out.println(b);
//        System.out.println(o);
//
//        b=iterator.hasNext();
//        o=iterator.next();
//        System.out.println(b);
//        System.out.println(o);
//
//        b=iterator.hasNext();
//        o=iterator.next();
//        System.out.println(b);
        while (iterator.hasNext()){
   
            String s=(String) iterator.next();
            System.out.println(s);
        }



    }
}
package org.wdit.unit01.Iterator;

import java.util.ArrayList;
import java.util.Iterator;
import java.util.List;
import java.util.ListIterator;

/**
 *
 */
public class IteratorDemo2 {
   
    public static void main(String[] args) {
   
        List list = new ArrayList();
        list.add("ZhaoTong");
        list.add("QuBo");
        list.add("TangKang");
        ListIterator iterator = list.listIterator();
        while(iterator.hasNext()){
   
            String s =(String)iterator.next();
            if (s.equals("QuBo")){
   
                iterator.add("C");
            }
        }
        System.out.println(list);
    }
}
用两种方法遍历数组**

1.创建学生类,并创建5个学生对象

2.将学生对象存储到集合中

3.遍历集合

package org.wdit.unit01.Iterator;

import java.util.ArrayList;
import java.util.Iterator;
import java.util.List;
import java.util.ListIterator;

/**
 * 迭代器练习:使用俩种方法遍历学生集合
 */
public class IteratorTest {
   
    public static void main(String[] args) {
   
        //创建学生对象
        Student student = new Student("赵童",18);
        Student student2 =new Student("屈波",18);
        //创建集合对象
        List list = new ArrayList();
        //学生对象添加到集合中
        list.add(student);
        list.add(student2);

        for ( Object o : list ){
   
            Student s =(Student) o;
            System.out.println(s.getName()+"--"+s.getAge());
        }
        Iterator iterator = list.iterator();


        while(iterator.hasNext()){
   
            Student s = (Student)iterator.next();
            System.out.println(s.getName()+"----"+s.getAge());
        }


    }
}

2. List接口
List接口概述

​ •有序的 collection(也称为序列)。此接口的用户可以对列表中每个元素的插入位置进行精确地控制。用户可以根据元素的整数索引(在列表中的位置)访问元素,并搜索列表中的元素。

​ •与 set 不同,列表通常允许重复的元素。

List案例

​ •存储字符串并遍历

​ •存储自定义对象并遍历

List成员方法

1.添加的方法:void add(int index,Object element) : index的值不能大于size, 添加的索引处若有元素,则元素向后移动

2.删除的方法:Object remove(int index): 返回移除的对象

​ boolean remove(Object o):返回是否移除:包含true ,不包含 false

3.替换的方法:Object set(int index, Object element):index 必须是大于0,小size-1

4.返回迭代器的方法:ListIterator listIterator():list特有的迭代器

package org.wdit.unit01.Collection;

import java.util.ArrayList;
import java.util.List;

/**
 * List接口:是一个元素有序的并且可以允许元素重复的集合
 *      List特有方法:
 *      1.添加功能
 *          void add(int index, E element):添加元素在指定位置,原索引位置的元素向后移动
 *          boolean addAll(int index, Collection c)将集合添加到指定索引位置
 *      2.获取功能
 *          Object get(int index):返回指定索引位置的元素
 *      3.修改功能
 *          E set(int index,E element)用指定元素替换列表中指定位置的元素(可选操作)。将被替换的元素返回
 *      4.截取功能
 *          List subList(int fromIndex,int toIndex):返回列表中指定的 fromIndex(包括 )和 toIndex(不包括)之间的部分视图。
 *          将截取内容作为新的集合返回,原集合不变
 *      5.删除功能
 *          E remove (int index)删除指定索引位置的元素 返回删除的元素
 */
public class ListDemo {
   
    public static void main(String[] args) {
   
        //创建集合对象
        List list = new ArrayList();
        list.add("hello");
        list.add("World");
        list.add("Java");
        System.out.println(list);
        System.out.println("------------------------------------------------------");

        //void add(int index, E element):添加元素在指定位置,原索引位置的元素向后移动
        list.add(1,"你好");
        System.out.println(list);
        System.out.println("------------------------------------------------------");

        //boolean addAll(int index, Collection c)将集合添加到指定索引位置
        List list2 = new ArrayList();
        list2.add("Super");
        list2.add("Abcd");
        list.addAll(1,list2);
        System.out.println(list);
        System.out.println("------------------------------------------------------");

        //Object get(int index):返回指定索引位置的元素
        Object o = list.get(2);
        String s = (String) o;
        System.out.println(s);
        System.out.println("------------------------------------------------------");

        //Object set(int index,Object element)用指定元素替换列表中指定位置的元素(可选操作)。
        Object o1 = list.set(2, "fuck");
        System.out.println(o1);
        System.out.println(list);
        System.out.println("------------------------------------------------------");

        //List subList(int fromIndex,int toIndex):返回列表中指定的 fromIndex(包括 )和 toIndex(不包括)之间的部分视图。
        List a = list.subList(1, 3);
        System.out.println("原List:"+list);
        System.out.println("新List:"+a);
        System.out.println("------------------------------------------------------");

        //E remove (int index)删除指定索引位置的元素
        Object remove = list.remove(3);
        System.out.println("返回的E:"+remove);
        System.out.println(list);


    }
}

3.Vector的概述与使用
Vector类概述

​ •底层数据结构是数组,查询快,增删慢

​ •线程安全,效率低

Vector类特有功能

​ •public void addElement(E obj)

​ •public E elementAt(int index)

​ •public Enumeration elements()

Vector案例

​ •存储字符串并遍历

​ •存储自定义对象并遍历

package org.wdit.unit01.Vector;

import java.util.Enumeration
  • 1
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值