1. 知识点:ArrayList、Iterator package collections; import java.util.ArrayList; import java.util.Iterator; /** * @author Eightn0 * @create 2021-03-14 19:43 * 1)封装一个新闻类,包含标题和内容属性,提供get、set方法,重写toString方法,打印对象时只打印标题;(10分) 2)只提供一个带参数的构造器,实例化对象时,只初始化标题;并且实例化两个对象: 新闻一:中国多地遭雾霾笼罩空气质量再成热议话题 新闻二:春节临近北京“卖房热” 3)将新闻对象添加到ArrayList集合中,并且使用ListIterator倒序遍历; 4)在遍历集合过程中,对新闻标题进行处理,超过15字的只保留前14个,然后在后边加“…” 5)在控制台打印遍历出经过处理的新闻标题; */ class News{ private String title; private String body; public String getTitle() { return title; } public void setTitle(String title) { this.title = title; } public String getBody() { return body; } public void setBody(String body) { this.body = body; } @Override public String toString() { return "News{" + "title='" + title + '\'' + '}'; } public News(String title) { this.title = title; } } public class NewsList { public static void main(String[] args) { News news1 = new News("中国多地遭雾霾笼罩空气质量再成热议话题"); News news2 = new News("春节临近北京“卖房热”"); ArrayList arrayList = new ArrayList(); arrayList.add(news1); arrayList.add(news2); Iterator iterator = arrayList.iterator(); while (iterator.hasNext()){ News news = (News) iterator.next(); if (news.getTitle().length() >= 15){ System.out.println(news.getTitle().substring(1,14)+"..."); }else { System.out.println(news.getTitle()); } } } }
2.map的增删改查与遍历
/** * @author Eightn0 * @create 2021-03-14 20:28 * 定义一个Map接口类型的变量,引用一个实现类,添加键值对,判断集合中是否包含某一key值, * 通过某一key值得到value值,通过某一key删除键值对,把另一个map集合添加到此map集合, * 判断是否为空,清除集合,返回集合里元素的个数等常用操作。 * 通过两种方法遍历上题中的map集合 */ public class MapExerciseOne { @Test public void test(){ //定义一个Map接口类型的变量 Map map = new HashMap(); //添加键值对 map.put("AA",1); map.put("BB",12); map.put("CC",123); map.put("DD",1234); map.put("EE",12345); //判断集合中是否包含某一key值 System.out.println(map.containsKey("AA")); //通过某一key值得到value值 System.out.println(map.get("AA")); //通过某一key删除键值对 map.remove("BB"); //把另一个map集合添加到此map集合 Map map1 = new HashMap(); map1.put(123,"AA"); map1.put(1234,"BA"); map1.put(12345,"BB"); map.putAll(map1); //判断是否为空,清除集合,返回集合里元素的个数 System.out.println(map.isEmpty()); map1.clear(); System.out.println(map.size()); //通过两种方法遍历上题中的map集合 //map本身没有遍历方法,但是key本质是set,而value本质是collection,且与key对应 Set set = map.keySet(); Iterator iterator = set.iterator(); while (iterator.hasNext()){ Object key = iterator.next(); Object value = map.get(key); System.out.println(key+"=="+value); } System.out.println("****************************"); Set entryset = map.entrySet(); Iterator iterator1 = entryset.iterator(); while (iterator1.hasNext()){ Map.Entry entry = (Map.Entry)iterator1.next(); System.out.println(entry.getKey() + "==" + entry.getValue()); } } }
3. 随机数生成、hashset、arraylist、foreach
** * @author Eightn0 * @create 2021-03-14 21:00 * 1. 生成10个随机数,值在100到200之间; 2. 将这十个数存入HashSet集合中(有可能集合的长度小于10)。 3. 将这个HashSet集合转换成ArrayList集合 4. 重新为ArrayList集合排序,按照从小到大的顺序; 5. 使用foreach遍历集合; */ public class NumbersExer { @Test public void test(){ //将这十个数存入HashSet集合中( HashSet set = new HashSet(); //生成10个随机数,值在100到200之间 for (int i = 0; i < 10; i++) { int num = (int)(Math.random() * (200 - 100) + 100); set.add(num); } //将这个HashSet集合转换成ArrayList集合 ArrayList list = new ArrayList(set); //重新为ArrayList集合排序,按照从小到大的顺序 Collections.sort(list); //使用foreach遍历集合; for (Object o: list ) { System.out.println(o); } } }