java学习--集合(大写四.4)

发布于:2024-10-18 ⋅ 阅读:(130) ⋅ 点赞:(0)

4.collection子接口:List

在这里插入图片描述

4.1 List接口存储数据特点

List接口中存储数据的特点:用于存储有序\可以重复的数据.
可以使用List替代数组,动态数组

4.2List接口常用方法

4.2.1、第一波: Collection中声明的15个方法

4.2.2、第二波:因为List是有序的,进而就会有序号、索引,就会针对索引操作的一些方法)

1)插入元素
①add(int index,Object ele):在index位置插入ele元素
②boolean addAll(int index,Collection eles):从index位置开始将eles中的所有元素添加进来
2)获取元素
①Object get(int index):获取指定index位置的元素
②List subList(int fromIndex,int toIndex):返回从fromIndex到toIndex位置的子集合
3)获取元素索引
①int indexOf(Object obj):返回Obj在集合中首次出现的位置
②int LastIndexOf(Object obj):返回Obj在集合中末次出现的位置
4)删除和替换索引
①Object remove(int index):返回Obj在集合中首次出现的位置
②Object set(int index,Object ele):返回Obj在集合中末次出现的位置

4.2.3、小结

1)增
①add(Object obj):添加一个元素
②addAll(Collection coll):将另一个集合元素添加到当前集合中
2)删
①remove(Object obj):删指定的元素
②remove(int index):以索引方式进行删除
3)改
①set(int index,Object ele):将指定位置改成新元素
4)查
①get(int index)

5)插

①add(int index,Object ele)在指定位置插入一个元素
②addAll(int index,Collection else)在指定位置插入多个元素

6)遍历

①iterator():使用迭代器进行遍历
②iterator():增强for循环
③一般的for循环

7)长度

①size()

4.3List接口主要实现类及其特点

java.util.Collection:存储一个一个的数据
子接口:List:存储有序可重复的数据(动态的数组)
主要实现类:ArrayList、LinkList、Vector

4.3.1ArrayList(List主要实现类)线程不安全,效率高,底层使用Object[]类型数组,在添加、查找数据效率较高,在插入、删除时效率较低

4.3.2 LinkList:底层使用双向链表的方式进行存储,在插入、删除时效率较高,在添加、查找数据效率较低,在对集合的数据频繁的插入、删除建议使用此类

4.3.3 Vector(List古老实现类)线程安全,效率低,底层使用Object[]类型数组

4.4练习

4.4.1案例:键盘录入学生信息,保存到集合List中。

(1) 定义学生类,属性为姓名、年龄,提供必要的getter、setter方法,构造器,toString(),equals()方法。

(2) 使用ArrayList集合,保存录入的多个学生对象。

(3) 循环录入的方式,1:继续录入,0:结束录入。

(4) 录入结束后,用foreach遍历集合。

在这里插入图片描述


```java

```java
package com.zhou.list.exer1;


import java.util.ArrayList;
import java.util.Scanner;

public class StudentTest {
    public static void main(String[] args) {

        Scanner sc = new Scanner(System.in);

        ArrayList list = new ArrayList();
        System.out.println("请录入学生的信息");
        while (true) {

            System.out.println("1:继续录入,0:结束录入");
            int seletcion = sc.nextInt();
            if (seletcion == 0) {

                break;
            }

            System.out.println("请输入学生姓名:");
            String name = sc.next();
            System.out.println("请输入学生年龄:");
            int age = sc.nextInt();


            Student s = new Student(name, age);

            list.add(s);


        }


        for (Object s : list) {
            System.out.println(s);
        }

        sc.close();


    }


}

4.4.2案例:定义方法public static int listTest(Collection list,String s)统计集合中指定元素出现的次数

(1) 创建集合,集合存放随机生成的30个小写字母

(2) 用listTest统计,a、b、c、x元素的出现次数

(3) 效果如下

在这里插入图片描述

package com.zhou.list.exer2;

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

public class ListTest {


    public static void main(String[] args) {
        ArrayList list = new ArrayList();

        for (int i = 0; i <30 ; i++) {
            list.add((char)(Math.random()*(122-97+1)+97)+"");
        }


        int acount = listTest(list, "a");
        int bcount = listTest(list, "b");
        int ccount = listTest(list, "c");
        int xcount = listTest(list, "x");


        System.out.println("a:"+acount);
        System.out.println("b:"+bcount);
        System.out.println("c:"+ccount);
        System.out.println("x:"+xcount);



    }




    public static int listTest(Collection list, String s){
      int count=0;

        for (Object obj:list) {

            if (s.equals(obj)){

                count++;
            }
        }









      return  count;


    }

4.4.3案例:KTV点歌系统

【说明】
使用ArrayList集合,实现编写一个模拟KTV点歌系统的程序。在程序中,
指令1代表添加歌曲,
指令2代表将所选歌曲置顶,
指令3代表将所选歌曲提前一位,
指令4代表退出该系统。

要求根据用户输入的指令完成相关的操作。

【提示】
(1) 显式界面如下:
System.out.println(“-------------欢迎来到点歌系统------------”);
System.out.println(“1.添加歌曲至列表”);
System.out.println(“2.将歌曲置顶”);
System.out.println(“3.将歌曲前移一位”);
System.out.println(“4.退出”);

(2) 程序中需要创建一个集合作为歌曲列表,并向其添加一部分歌曲
(3) 通过ArrayList集合定义的方法操作歌曲列表
(4) 本题目使用LinkedList 如何?

package com.zhou.list.exer3.model;

import java.util.ArrayList;
import java.util.Scanner;
public class KTVByArrayList {


    private static ArrayList musicList = new ArrayList();// 创建歌曲列表
    private static Scanner sc = new Scanner(System.in);


    public static void main(String[] args) {

        addMusicList();// 添加一部分歌曲至歌曲列表
        boolean  flag=true;
        while (flag) {
            System.out.println("当前歌曲列表"+musicList);
            System.out.println("-------------欢迎来到点歌系统------------");
            System.out.println("1.添加歌曲至列表");
            System.out.println("2.将歌曲置顶");
            System.out.println("3.将歌曲前移一位");
            System.out.println("4.退出");
            System.out.print("请输入操作序号:");
            int key = sc.nextInt();
            switch (key){
                case 1:
                 addMusic();
                  break;
                case 2:// 将歌曲置顶
                  setTop();
                  break;
                case 3:
                    setBefore();
                    break;
                case 4:
                    System.out.println("====退出====");
                    System.out.println("您已推出系统");
                    flag=false;
                    break;
                default:
                    System.out.println("----------------------------------");
                    System.out.println("功能选择有误,请输入正确的功能序号!");
                    break;





            }




        }




    }



    private  static void addMusicList(){

        musicList.add("告白气球");
         musicList.add("稻香");
         musicList.add("听妈妈的话");
         musicList.add("晴天");
         musicList.add("一路向北");


     }

  private static void addMusic() {
        System.out.print("请输入要添加的歌曲名称:");
        String musicName = sc.next();// 获取键盘输入内容
        musicList.add(musicName);// 添加歌曲到列表的最后
        System.out.println("已添加歌曲:" + musicName);
    }



    private static void setTop() {
        System.out.print("请输入要置顶的歌曲名称:");
        String musicName = sc.next();
        int musicIndex = musicList.indexOf(musicName);
        if (musicIndex<0){

            System.out.println("当前列表中没有输入的歌曲!");
        }else if (musicIndex==0){
            System.out.println("当前歌曲已在最顶部!");


        }else {
            musicList.remove(musicName);
            musicList.add(0, musicName);// 将指定的歌曲放到第一位
            System.out.println("已将歌曲《" + musicName + "》置顶");



        }


    }





    private static void setBefore() {
        System.out.print("请输入要置前的歌曲名称:");
        String musicName = sc.next();

        int musicIndex = musicList.indexOf(musicName);
        if (musicIndex<0){
            System.out.println("当前列表中没有输入的歌曲!");

        }else if (musicIndex==0){

            System.out.println("当前歌曲已在最顶部!");

        }else{

            musicList.remove(musicName);
            musicList.add(musicIndex-1,musicName);

            System.out.println("已将歌曲《" + musicName + "》置前一位");

        }




    }



}

4.5、List实现类源码分析

4.5.1ArrayList

4.5.1.1ArrayList的特点

实现了List接口,存储有序的、可以重复的数据
底层使用Object[]数组存储
线程不安全
在添加、查找数据效率较高,在插入、删除时效率较低

4.5.1.2ArrayList源码解析
1)jdk7版本:以jdk1.7.0_07为例

new

2)jdk8版本:以jdk1.8.0_271为例

4.5.2Vector

4.5.2.1Vector的特点

实现了List接口,存储有序的、可以重复的数据
底层使用Object[]数组存储
线程安全,效率低

4.5.2.2Vector源码解析(以jdk1.8.0_271为例)

4.5.3LinkedList

4.5.3.1LinkedList的特点

实现List接口,存储有序可以重复的数据
底层采用双向链表存储
添加、查找效率低,插入,删除效率高
线程不安全

4.5.3.2LinkedList源码解析(以jdk1.8.0_271为例)

网站公告

今日签到

点亮在社区的每一天
去签到