|NO.Z.00048|——————————|BigDataEnd|——|Java&集合类库.V05|——|Java.v05|List集合.二.v02|

一、编程代码
### --- 编程代码

——>         [List集合]——[List集合中增加和查找方法的使用]
——>         [List集合中修改和删除以及自己和获取的使用]
package com.yanqi.task14;

import java.util.LinkedList;
import java.util.List;

public class ListMethodTest {

    public static void main(String[] args) {

        // 1.准备一个List集合并打印
        List lt1 = new LinkedList();
        System.out.println("lt1 = " + lt1); // [啥也没有]

        System.out.println("------------------------------------------");
        // 2.向集合中添加元素并打印
        // 向集合中的开头位置添加元素
        lt1.add(0, "one");
        System.out.println("lt1 = " + lt1); // [one]
        // 向集合中的末尾位置添加元素
        lt1.add(1, 3);
        System.out.println("lt1 = " + lt1); // [one, 3]
        // 向集合中的中间位置添加元素
        lt1.add(1, "two");
        System.out.println("lt1 = " + lt1); // [one, two, 3]

        System.out.println("------------------------------------------");
        // 3.根据参数指定的下标来获取元素
        String str1 = (String) lt1.get(0);
        System.out.println("获取到的元素是:" + str1); // one

        // 注意:获取元素并进行强制类型转换时一定要慎重,因为容易发生类型转换异常
        //String str2 = (String)lt1.get(2); // 编译ok,运行发生ClassCastException类型转换异常
        //System.out.println("获取到的元素是:" + str2); // 3

        System.out.println("------------------------------------------");
        // 4.使用get方法获取集合中的所有元素并打印
        StringBuilder sb1 = new StringBuilder();
        sb1.append("[");
        for (int i = 0; i < lt1.size(); i++) {
            //Object obj = lt1.get(i);
            //System.out.println("获取到的元素是:" + obj);
            Object obj = lt1.get(i);
            // 若取出的元素是最后一个元素,则拼接元素值和]
            if (lt1.size()-1 == i) {
                sb1.append(obj).append("]");
            }
            // 否则拼接元素和逗号以及空格
            else {
                sb1.append(obj).append(",").append(" ");
            }
        }
        System.out.println("lt1 = " + sb1); // [one, two, 3]

        System.out.println("------------------------------------------");
        // 5.查找指定元素出现的索引位置
        System.out.println("one第一次出现的索引位置为:" + lt1.indexOf("one")); // 0
        lt1.add("one");
        System.out.println("lt1 = " + lt1); // [one, two, 3, one]
        System.out.println("one反向查找第一次出现的索引位置是:" + lt1.lastIndexOf("one")); // 3

        System.out.println("------------------------------------------");
        System.out.println("lt1 = " + lt1); // [one, two, 3, one]
        // 6.实现集合中元素的修改
        Integer it1 = (Integer) lt1.set(2, "three");
        System.out.println("被修改的元素是:" + it1); // 3
        System.out.println("修改后集合中的元素有:" + lt1); // [one, two, three, one]

        String str2 = (String) lt1.set(3, "four");
        System.out.println("被修改的元素是:" + str2); // one
        System.out.println("修改后集合中的元素有:" + lt1); // [one, two, three, four]

        System.out.println("------------------------------------------");
        // 7.使用remove方法将集合中的所有元素删除
        //for (int i = 0; i < lt1.size(); /*i++*/) {
       /* for (int i = lt1.size()-1; i >= 0; i--) {
            //System.out.println("被删除的元素是:" + lt1.remove(i)); // one  two  three  four 删除元素后,后面的元素补位
            //System.out.println("被删除的元素是:" + lt1.remove(0));
            System.out.println("被删除的元素是:" + lt1.remove(i));
        }
        System.out.println("最终集合中的元素有:" + lt1); // [啥也没有]*/

        System.out.println("------------------------------------------");
        // 8.获取当前集合中的子集合,也就是将集合中的一部分内容获取出来,子集合和当前集合共用同一块内存空间
        // 表示获取当前集合lt1中下标从1开始到3之间的元素,包含1但不包含3
        List lt2 = lt1.subList(1, 3);
        System.out.println("lt2 = " + lt2); // [two, three]
        // 删除lt2中元素的数值
        str2 = (String) lt2.remove(0);
        System.out.println("被删除的元素是:" + str2); // two
        System.out.println("删除后lt2 = " + lt2); // [three]
        System.out.println("删除后lt1 = " + lt1); // [one, three, four]
    }
}
二、编译打印
D:\JAVA\jdk-11.0.2\bin\java.exe "-javaagent:D:\IntelliJIDEA\IntelliJ IDEA 2019.3.3\lib\idea_rt.jar=63990:D:\IntelliJIDEA\IntelliJ IDEA 2019.3.3\bin" -Dfile.encoding=UTF-8 -classpath E:\NO.Z.10000——javaproject\NO.H.00001.javase\javase\out\production\javase com.yanqi.task14.ListMethodTest
lt1 = []
------------------------------------------
lt1 = [one]
lt1 = [one, 3]
lt1 = [one, two, 3]
------------------------------------------
获取到的元素是:one
------------------------------------------
lt1 = [one, two, 3]
------------------------------------------
one第一次出现的索引位置为:0
lt1 = [one, two, 3, one]
one反向查找第一次出现的索引位置是:3
------------------------------------------
lt1 = [one, two, 3, one]
被修改的元素是:3
修改后集合中的元素有:[one, two, three, one]
被修改的元素是:one
修改后集合中的元素有:[one, two, three, four]
------------------------------------------
------------------------------------------
lt2 = [two, three]
被删除的元素是:two
删除后lt2 = [three]
删除后lt1 = [one, three, four]

Process finished with exit code 0

 
 
 
 
 
 
 
 
 

Walter Savage Landor:strove with none,for none was worth my strife.Nature I loved and, next to Nature, Art:I warm'd both hands before the fire of life.It sinks, and I am ready to depart
                                                                                                                                                   ——W.S.Landor

 

 

posted on   yanqi_vip  阅读(22)  评论(0编辑  收藏  举报

相关博文:
阅读排行:
· 全程不用写代码,我用AI程序员写了一个飞机大战
· MongoDB 8.0这个新功能碉堡了,比商业数据库还牛
· 记一次.NET内存居高不下排查解决与启示
· 白话解读 Dapr 1.15:你的「微服务管家」又秀新绝活了
· DeepSeek 开源周回顾「GitHub 热点速览」
< 2025年3月 >
23 24 25 26 27 28 1
2 3 4 5 6 7 8
9 10 11 12 13 14 15
16 17 18 19 20 21 22
23 24 25 26 27 28 29
30 31 1 2 3 4 5

导航

统计

点击右上角即可分享
微信分享提示