Java List 练习(3) (2024.7.24)
创始人
2024-12-13 23:05:44
0

        ListExericse3

package ListExercise20240724; import java.util.*; import java.util.function.Consumer;  public class ListExercise3 {     public static void main(String[] args) {         // List集合五种遍历方式         // 1.迭代器 2.列表迭代器 3.增强for 4.Lambda表达式 5.普通for(这是List集合特有的,原理是List集合有索引)         Scanner sc = new Scanner(System.in);         // 先准备一个集合,然后进行遍历         List arrayList = new ArrayList<>();         System.out.println("请输入集合中有几个元素");         int n = Integer.parseInt(sc.nextLine());         for (int i = 0; i < n; i++) {             System.out.printf("请输入第%d个元素", i + 1);             String element = sc.nextLine();             arrayList.add(element);         }         iteratorPrint(arrayList);         System.out.println("-----------------------------");         listIteratorPrint(arrayList);         System.out.println(arrayList);         System.out.println("-----------------------------");         forPrint(arrayList);         System.out.println("-----------------------------");         LambdaPrint(arrayList);         System.out.println("-----------------------------");         printArray(arrayList);      }      // 1.迭代器遍历     public static void iteratorPrint(List arrayList) {         Iterator it = arrayList.iterator();         while (it.hasNext()) {             E str = it.next();             System.out.println(str);         }     }      // 2.列表迭代器遍历 在使用列表迭代器的时候,可以添加元素     public static void listIteratorPrint(List arrayList) {         ListIterator it = arrayList.listIterator();         while (it.hasNext()) {             E str = it.next();             System.out.println(str);             if (str.equals("b")) {                 it.add((E)"zxl");             }         }     }      // 3.增强for     public static void forPrint(List arrayList) {         for (E e : arrayList) {             System.out.println(e);         }     }      // 4.Lambda表达式     public static  void LambdaPrint(List arrayList) {         arrayList.forEach(s -> System.out.println(s)); //        arrayList.forEach(new Consumer() { //            @Override //            public void accept(E e) { //                System.out.println(e); //            } //        });     }      // 5.普通for     public static  void printArray(List arrayList) {         for (int i = 0; i < arrayList.size(); i++) {             System.out.println(arrayList.get(i));         }     } } 

        ListAndSet

package ListExercise20240724; import java.util.ArrayList; import java.util.List; import java.util.Scanner;  public class ListAndSet {     public static void main(String[] args) {         Scanner sc = new Scanner(System.in);         // Set和List都是List的实现类,但有所差异;一般而言,List集合是有序的(怎么存的怎么取),Set是无序的(存取无序)         // List集合可以存在重复的元素,而Set集合不可以;List集合是有索引的,可以通过索引来获得元素,而Set是没有索引的          // 因为List集合存在索引,所以说围绕其索引,产生了许多特殊的List特有的方法          List arrayList = new ArrayList<>();         // 这里写多态必须写List而不能写Collection,因为Collection是父接口,必须包含List和Set中的共同方法,所以说就没有List独有的方法         System.out.println("需要添加几个元素?");         String n = sc.nextLine();         for (int i = 0; i < Integer.parseInt(n); i++) {             System.out.printf("请输入第%d个元素", i + 1);             String element = sc.nextLine();             arrayList.add(element); // 这里是普通的添加,就是按照顺序添加元素         }         insertElement(arrayList);         System.out.println(arrayList);         insertElement(arrayList);         System.out.println(arrayList);         removeElement(arrayList);         System.out.println(arrayList);         removeElement(arrayList);         System.out.println(arrayList);         System.out.println(arrayList);     }      // 1. void add(int index, E element) 在集合的指定索引处插入元素     public static void insertElement(List arrayList) { // 方法参数写多态         Scanner sc = new Scanner(System.in);         System.out.println("请输入想要添加元素的索引");         String index = sc.nextLine();         System.out.println("请输入想要添加的元素");         String element = sc.nextLine();         E e = (E)element;         arrayList.add(Integer.parseInt(index), e);     }      // 2. E remove(int index) 删除指定索引的元素,返回被删除的元素     public static  E removeElement(List arrayList) {         Scanner sc = new Scanner(System.in);         System.out.println("请输入想要删除元素的索引");         int index = Integer.parseInt(sc.nextLine());         if (index < 0 || index >= arrayList.size()) {             return null;         }         return (E)arrayList.remove(index);     }      // 3. E set(int index, E element) 修改指定索引的元素,返回被修改的元素     public static  E setElement(List arrayList) {         Scanner sc = new Scanner(System.in);         System.out.println("请输入想要修改的元素的索引");         int index = Integer.parseInt(sc.nextLine());         if (index < 0 || index >= arrayList.size()){             System.out.println("索引无效");             return null;         }         System.out.println("请输入想要修改的内容");         String element = sc.nextLine();         E e = (E)element;         return (E)arrayList.set(index, e);     } } 

 

相关内容

热门资讯

山水投影仪安卓系统,沉浸式自然... 你有没有想过,在夜幕降临的时候,把大自然的美景搬进家里?想象当你坐在沙发上,一杯热茶在手,眼前却是一...
ia攻略/牛牛房卡批发平台新道... 新道游/新皇豪是一款非常受欢迎的棋牌游戏,咨询房/卡添加微信:【3329006910】或QQ:332...
两秒就懂!炸金花从哪里买房卡,... 微信游戏中心:炸金花房卡,添加微信【66336574】,进入游戏中心或相关小程序,搜索“微信炸金花房...
科技实测!牛牛房卡批发平台黑鹰... 您好!微信黑鹰众玩大厅链接获取房卡可以通过以下几种方式购买: 1.微信渠道:(黑鹰众玩)大厅介绍:...
玩家攻略,金花房间怎么创建神牛... 今 日消息,神牛大厅房卡添加微信33549083 苹果今日发布了 iOS 16.1 正式版更新,简单...
微信链接金花房卡怎么弄/金花链... 金花是一款非常受欢迎的棋牌游戏,咨询房/卡添加微信:44346008许多玩家在游戏中会购买房卡来享受...
重大通报,金花房卡批发价九神联... 九神联盟房卡更多详情添加微:33549083、 2、在商城页面中选择房卡选项。 3、根...
分享!微信拼三张房间卡在哪买的... 拼三张是一款非常受欢迎的棋牌游戏,咨询房/卡添加微信:33699510许多玩家在游戏中会购买房卡来享...
科技实测!牛牛房卡批发平台海贝... 科技实测!牛牛房卡批发平台海贝之城/微信链接房卡购买渠道海贝之城是一款非常受欢迎的游戏,咨询房/卡添...
正版授权!牛牛房卡出售玉兔大厅... 微信游戏中心:玉兔大厅房卡在哪里买打开微信,添加客服微信【88355042】,进入游戏中心或相关小程...
一分钟了解!金花房卡是正规的玄... 玄武大厅是一款非常受欢迎的棋牌游戏,咨询房/卡添加微信:【3329006910】或QQ:332900...
两秒就懂!拼三张正规房卡总代理... 微信游戏中心:拼三张房卡,添加微信【71319951】,进入游戏中心或相关小程序,搜索“微信拼三张房...
头条推荐!金花房卡出售新星游/... 头条推荐!金花房卡出售新星游/凤凰大厅/微信链接房卡购买渠道Sa9Ix苹果iPhone 17手机即将...
在哪里能买金花房卡/牛牛链接房... 牛牛是一款非常受欢迎的棋牌游戏,咨询房/卡添加微信:15984933许多玩家在游戏中会购买房卡来享受...
玩家攻略,斗牛房间怎么创建的战... 微信游戏中心:战神联盟房卡在哪里买打开微信,添加客服微信【88355042】,进入游戏中心或相关小程...
两秒就懂!微信买链接拼三张房卡... 微信游戏中心:拼三张房卡,添加微信【56001354】,进入游戏中心或相关小程序,搜索“微信拼三张房...
IA解析/金花房卡制作链接新众... 新众亿/皇豪互娱是一款非常受欢迎的棋牌游戏,咨询房/卡添加微信:【3329006910】或QQ:33...
重大通报,斗牛房间怎么创建的悟... 重大通报,斗牛房间怎么创建的悟空系列/随意玩/微信链接房卡购买联系方式Sa9Ix苹果iPhone 1...
分享!微信群打炸金花房卡怎么搞... 炸金花是一款非常受欢迎的棋牌游戏,咨询房/卡添加微信:33699510许多玩家在游戏中会购买房卡来享...
重大通报,金花房卡代理零售黄帝... 今 日消息,黄帝大厅房卡添加微信33549083 苹果今日发布了 iOS 16.1 正式版更新,简单...