集合基础框架

本文详细介绍了Java集合框架的基础知识,包括集合、Collection接口、List接口及其实现类ArrayList的使用方法。通过示例代码展示了添加、删除、遍历元素的操作,并对比了集合与数组的区别。同时,讲解了List接口的特点和常用方法,如添加、删除元素及获取子列表。文章还涉及了迭代器和ListIterator的区别,并提供了示例演示。
摘要由CSDN通过智能技术生成

集合基础详细框架

一、大致包括六个部分
1.集合的概念
2.Collection接口
3.List接口与实现类
4.泛型和工具类
5.Set接口与实现类
6.Map接口与实现类

一、集合是什么?

  • 概念:对象的容器,定义了对多个对象进行操作的常用方法。可实现数组的功能。
  • 和数组的区别:
    (1)数组长度可以固定,集合长度不固定
    (2)数组可以存储基本类型和引用类型,集合只能存储引用类型

二、Collection接口

1.Collection体系集合

在这里插入图片描述

代码如下(示例1):

public class Demo01 {
    public static void main(String[] args) {
        //创建集合
        Collection co = new ArrayList();
        //1.添加元素
        co.add("春天");
        co.add("夏天");
        co.add("秋天");
        co.add("冬天");
        System.out.println("元素个数: "+co.size());
        System.out.println(co);
        //2.删除元素
//        co.remove("冬天");
//        System.out.println("元素个数: "+co.size());
//        System.out.println(co);
//        co.clear();
//        System.out.println("元素个数: "+co.size());
        //3.遍历元素
        //3.1使用增强for循环
        for (Object object:co){
            System.out.println(object);
        }
        //3.2使用迭代器
        //hasNext();有没有下一个元素
        //next();读取下一个元素
        //remove();删除当前元素
        System.out.println("*---------------*");
        Iterator it= co.iterator();
        while (it.hasNext()){
            String s=(String)it.next();
            System.out.println(s);
           // co.remove("冬天"); 不能使用
             // it.remove();
        }
      //  System.out.println(co.size());
        System.out.println(co.contains("冬天"));
        System.out.println(co.isEmpty());
    }

}

2.保存学生信息

代码如下(示例2):

public class Demo02 {
    public static void main(String[] args) {
        //新建collection对象
        Collection collection = new ArrayList();
        Student s1=new Student("锐雯",5);
        Student s2=new Student("亚瑟",6);
        Student s3=new Student("盲僧",9);
        //添加数据
        collection.add(s1);
        collection.add(s2);
        collection.add(s3);
        System.out.println("元素个数: "+collection.size());
        System.out.println(collection.toString());
        //删除
        collection.remove(s1);
        System.out.println("删除之后:"+collection.size());
        System.out.println(collection.toString());
//        collection.clear();
//        System.out.println(collection.size());
        //遍历
        System.out.println("------增强for遍历--------");
        for (Object object:collection){
            Student s=(Student) object;
            System.out.println(s.toString());
        }
        System.out.println("*--------------------*");
        //迭代性
        Iterator it=collection.iterator();
         while (it.hasNext()){
             Student s=(Student)it.next();
             System.out.println(s.toString());
         }
         //判断
        System.out.println(collection.contains(s2));
        System.out.println(collection.contains(s1));
        System.out.println(collection.isEmpty());
    }
}

public class Student {
    private String name;
    private int age;
     public Student(){

     }
    public Student(String name, int age) {
         super();
         this.name =name;
        this.age = age;
    }

    public String getName() {
        return name;
    }

    public void setName(String name) {
        this.name = name;
    }

    public int getAge() {
        return age;
    }

    public void setAge(int age) {
        this.age = age;
    }

    @Override
    public String toString() {
        return "Student [name=" + name +",age="+age+"]";
    }
}


List子接口

  • 特点:有序、有下标、元素可以重复
  • 方法:
  • void add(int index, Object o) //在index位置插入对象o
  • bollean addAll(int index,Collection c) //将一个集合中的元素添加到此集合中的index位置
  • Object get(int index) /返回集合中指定位置的元素
  • List subList(int fromIndex,int toIndex) //返回fromIndex和toIndex之间的集合元素
public class Demo03 {
    public static void main(String[] args) {
        //先创建集合对象
        List list=new ArrayList<>();
        //添加元素
        list.add("苹果");
        list.add("小米");
        list.add(0,"华为");
        list.add(2,"OPPO");
        System.out.println("元素个数: "+list.size());
        System.out.println(list.toString());
        //删除元素
        System.out.println("*-------------*");
        list.remove(0);
        System.out.println("删除之后元素个数: "+list.size());
        System.out.println(list.toString());
        //遍历元素
        System.out.println("*-------------------*");
        for (int i = 0; i < list.size(); i++) {
            System.out.println(list.get(i));
        }
        System.out.println("*-----------------*");
        for (Object object :list){
            System.out.println(object);
        }
        System.out.println("*--------------------*");
        Iterator it=list.iterator();
        while (it.hasNext()){
            System.out.println(it.next());
        }
        //和Iterator的区别,ListIterator可以向前或向后遍历,添加,删除,修改元素
        System.out.println("*-------列表迭代器-----------*");
       ListIterator lit = list.listIterator();
       //从前往后
        while (lit.hasNext()){
            System.out.println(lit.nextIndex()+":"+lit.next());
        }
        System.out.println("****************");
        //从后往前
         while (lit.hasPrevious()){
             System.out.println(lit.previousIndex()+":"+lit.previous());
         }
         //判断
        System.out.println(list.contains("OPPO"));
        System.out.println(list.isEmpty());
        //获取位置
        System.out.println(list.indexOf("苹果"));
    }
}
package com.Collection;

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

public class Demo04 {
    public static void main(String[] args) {
        //创建集合
        List list = new ArrayList();
        //添加数字数据(自动装箱)
        list.add(10);
        list.add(20);
        list.add(30);
        list.add(40);
        list.add(50);
        list.add(99);
        System.out.println("元素个数: "+list.size());
        System.out.println(list.toString());
         //删除操作
        list.remove(2);//指的是数组下标
        System.out.println(list.toString());
        list.remove((Object)99);
       // list.remove(new Integer(20));
        System.out.println(list.toString());
        //补充方法subList:返回子集合 含头不含尾
       List subList= list.subList(1,3);
        System.out.println(subList.toString());
    }
}
  • 下节讲述List实现类
  • ArrayList的使用
  • vector的使用
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

锐雯.

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值