有关Java动态数组的一个小问题
前言
问题描述
今天遇到一个关于集合的问题,觉得比较有趣,记录一下,这个问题是:定义一个用户类,至少包含姓名,年龄,生日,qq邮箱,初始化10个用户,利用String操作,提取qq到List集合中,姓名为英文的首字母转大写,生日格式化为 年月日 x时表示(x:子丑...)。对对象进行排序,根据生日从小到大排序.
解决方案
解决思路
- 我们先初始化10个对象,并存入List集合中
- 再new一个List集合为list1,遍历list集合,截取每个对象中的邮箱地址,存入list1集合
- 用同样的方式截取list集合中的name、birthday、age、email拼接成一个新的字符串,new一个Map数组,birthday作为key,新的字符串作为value存入Map
- 遍历这个Map数组,对HashMap的value进行排序
在这里我给大家看一下我的代码:
点击查看详细代码
import java.util.*; import java.util.List; public class User { public String name; public int age; public String birthday; public String qqMailBox; public static void main(String[] args) { List list = new ArrayList(); list.add("name: annie age: 12 birthday: 2009/03/05/03 qqMailBox: 2133221865@qq.com"); list.add("name: 狗剩 age: 13 birthday: 2010:01:05:05 qqMailBox: 2133281865@qq.com"); list.add("name: 翠花 age: 14 birthday: 2011:02:05:06 qqMailBox: 2133271865@qq.com"); list.add("name: 二狗 age: 14 birthday: 2011/03/05/02 qqMailBox: 2133251865@qq.com"); list.add("name: 旺财 age: 15 birthday: 2012:02:05:23 qqMailBox: 2133251865@qq.com"); list.add("name: 富贵 age: 12 birthday: 2009年01月05日19时 qqMailBox: 2133251865@qq.com"); list.add("name: saber age: 16 birthday: 2013/02/05/12 qqMailBox: 2133251865@qq.com"); list.add("name: 大力 age: 17 birthday: 2014年01月05日08时 qqMailBox: 2133251365@qq.com"); list.add("name: alger age: 18 birthday: 2015年03月05日15时 qqMailBox: 2133251465@qq.com"); list.add("name: 狗蛋 age: 19 birthday: 2016年03月05日23时 qqMailBox: 2133251845@qq.com"); //该集合用来存邮箱地址 List list1 = new ArrayList(); /** *截取集合中的每个Object对象,存入List集合 */ for(Object c : list){ //System.out.println(c); String c1 = c.toString(); //获取最后一个:所在的位置 int index1 = c1.lastIndexOf(":"); String ss = c1.substring(index1 + 1); list1.add(ss); //System.out.println(ss); } System.out.println("提取qq到List集合中===>"); System.out.println(list1); /** *截取所有属性,并排序 */ System.out.println("排序后的Map数组===>"); for(Object o : list){ //截取name,若为英文首字母转大写 String s1 = o.toString(); int index1 = s1.indexOf("age"); int index2 = s1.indexOf("name:"); String s2 = s1.substring(index2 + 5, index1).trim(); //判断所有字符是否为英文 boolean result = s2.matches("[a-zA-Z]+"); String name = ""; //首字母转大写 if(result == true) { String str2 = s2.substring(0, 1).toUpperCase() + s2.substring(1); name = str2; //System.out.println(str2); }else { //System.out.println(s2); name = s2; } //截取生日,格式化为 年月日 x时表示(x:子丑...) int index3 = s1.indexOf("birthday"); int index4 = s1.indexOf("qqMailBox"); String s3 = s1.substring(index3 + 9, index4).trim(); //System.out.println(s3); //获取所有数字 String str4 = ""; if(s3 != null && !"".equals(s3)){ for(int i = 0; i < s3.length(); i++){ if(s3.charAt(i) >= 48 && s3.charAt(i) <= 57){ str4 += s3.charAt(i); } } } //截取年月日时 String year = str4.substring(0, 4); String month = str4.substring(4, 6); String day = str4.substring(6, 8); String time = str4.substring(8, 10); //将String转为int int time2 = Integer.parseInt(time); String time3 = ""; if(time2 >= 0 && time2 < 2){ time3 = "子"; }else if(time2 >= 2 && time2 < 4){ time3 = "丑"; }else if(time2 >= 4 && time2 < 6){ time3 = "寅"; }else if(time2 >= 6 && time2 < 8){ time3 = "卯"; }else if(time2 >= 8 && time2 < 10){ time3 = "辰"; }else if(time2 >= 10 && time2 < 12){ time3 = "巳"; }else if(time2 >= 12 && time2 < 14){ time3 = "午"; }else if(time2 >= 14 && time2 < 16){ time3 = "未"; }else if(time2 >= 16 && time2 < 18){ time3 = "申"; }else if(time2 >= 18 && time2 < 20){ time3 = "酉"; }else if(time2 >= 20 && time2 < 22){ time3 = "戌"; }else if(time2 >= 22 && time2 < 24){ time3 = "亥"; }; //拼接生日 String birthday2 = year + "年" + month + "月" + day + "日" + time3 + "时"; //System.out.println(birthday2); //截取年龄 int index5 = s1.indexOf("age"); int index6 = s1.indexOf("birthday"); String s4 = s1.substring(index5 + 5, index6).trim(); //System.out.println(s4); //截取邮箱 int index7 = s1.indexOf("qqMailBox"); String s5 = s1.substring(index7 + 10).trim(); //System.out.println(s5); //将数据重新拼接后插入Map数组 Map map = new HashMap(); String key = str4; String value = "name:" + " " + name + " " + "age:" + " " + s4 + " " + "birthday:" + " " + birthday2 + " " + "qqMailBox:" + " " + s5; map.put(key, value); //遍历时排序 map.forEach((key1, value1) -> { //对HashMap的value进行排序 Object[] key_arr = map.keySet().toArray(); Arrays.sort(key_arr); for (Object key2 : key_arr) { Object value2 = map.get(key2); } System.out.print(key1 + "=" + value1 + " ===> "); }); } //System.out.println("按生日从小到排序===>" + set); } }
运行结果:
提取qq到List集合中===> [ 2133221865@qq.com, 2133281865@qq.com, 2133271865@qq.com, 2133251865@qq.com, 2133251865@qq.com, 2133251865@qq.com, 2133251865@qq.com, 2133251365@qq.com, 2133251465@qq.com, 2133251845@qq.com] 排序后的Map数组===> 2009030503=name: Annie age: 12 birthday: 2009年03月05日丑时 qqMailBox: 2133221865@qq.com ===> 2010010505=name: 狗剩 age: 13 birthday: 2010年01月05日寅时 qqMailBox: 2133281865@qq.com ===> 2011020506=name: 翠花 age: 14 birthday: 2011年02月05日卯时 qqMailBox: 2133271865@qq.com ===> 2011030502=name: 二狗 age: 14 birthday: 2011年03月05日丑时 qqMailBox: 2133251865@qq.com ===> 2012020523=name: 旺财 age: 15 birthday: 2012年02月05日亥时 qqMailBox: 2133251865@qq.com ===> 2009010519=name: 富贵 age: 12 birthday: 2009年01月05日酉时 qqMailBox: 2133251865@qq.com ===> 2013020512=name: Saber age: 16 birthday: 2013年02月05日午时 qqMailBox: 2133251865@qq.com ===> 2014010508=name: 大力 age: 17 birthday: 2014年01月05日辰时 qqMailBox: 2133251365@qq.com ===> 2015030515=name: Alger age: 18 birthday: 2015年03月05日未时 qqMailBox: 2133251465@qq.com ===> 2016030523=name: 狗蛋 age: 19 birthday: 2016年03月05日亥时 qqMailBox: 2133251845@qq.com ===>
- 集合框架排序
上文中最后一步是对Map集合中的key进行排序然后遍历出来
【推荐】国内首个AI IDE,深度理解中文开发场景,立即下载体验Trae
【推荐】编程新体验,更懂你的AI,立即体验豆包MarsCode编程助手
【推荐】抖音旗下AI助手豆包,你的智能百科全书,全免费不限次数
【推荐】轻量又高性能的 SSH 工具 IShell:AI 加持,快人一步
· .NET Core 中如何实现缓存的预热?
· 从 HTTP 原因短语缺失研究 HTTP/2 和 HTTP/3 的设计差异
· AI与.NET技术实操系列:向量存储与相似性搜索在 .NET 中的实现
· 基于Microsoft.Extensions.AI核心库实现RAG应用
· Linux系列:如何用heaptrack跟踪.NET程序的非托管内存泄露
· TypeScript + Deepseek 打造卜卦网站:技术与玄学的结合
· 阿里巴巴 QwQ-32B真的超越了 DeepSeek R-1吗?
· 如何调用 DeepSeek 的自然语言处理 API 接口并集成到在线客服系统
· 【译】Visual Studio 中新的强大生产力特性
· 2025年我用 Compose 写了一个 Todo App