Java — set 和 list 集合练习题
                                                            生活随笔
收集整理的這篇文章主要介紹了
                                Java — set 和 list 集合练习题
小編覺得挺不錯的,現在分享給大家,幫大家做個參考.                        
                                1、使用Set集合,生成1-25之內不重復的7個隨機整數。
//代碼 package com.practice1111;import java.util.HashSet; import java.util.Set;public class createRandomBySet {public static void main(String[] args) {Set<Integer> randomSet = new HashSet<Integer>();while (randomSet.size() < 7) {//土方法,方法一,先生成0-25內的隨機整數,然后判斷去“0”int myRandom = (int) (Math.random() * 100) % 25;if(myRandom==0) {continue;}else {randomSet.add(myRandom);}/*生成隨機數,方法二,先生成一個0-1的隨機數,然后乘以24,再加1,最后取整*int myRandom = (int) (Math.random() * 24 + 1);*randomSet.add(myRandom);*/ }//打印集合for (Object result : randomSet) {System.out.print(result + " ");}} }運行結果:
2、如何判斷兩個集合是否有交集,并打印出他們的交集。
//代碼 package com.practice1111;import java.util.List; import java.util.ArrayList; import java.util.Collections;public class findTheIntersectionOfSets {public static void main(String[] args) {Integer[] a = { 1, 2, 3, 4, 5 };Integer[] b = { 3, 4, 5, 6, 7, 8, 9 };List<Integer> list1 = new ArrayList<Integer>(a.length);List<Integer> list2 = new ArrayList<Integer>(b.length);//方法一:增強for循環,將數組元素添加到集合中 /*for(int c: a) { list1.add(c); } for(int d: b) { list2.add(d);}*/ // 方法二:將數組元素添加到集合中Collections.addAll(list1, a);Collections.addAll(list2, b);// 打印list1集合System.out.println("集合二的元素為:");for (int e : list1) {System.out.print(e + " ");}System.out.println();// 打印list2集合System.out.println("集合一的元素為:");for (int f : list2) {System.out.print(f + " ");}System.out.println();// 判斷兩個集合有無交集,有則打印交集if (list1.retainAll(list2)) {System.out.println("有交集:");for (int e : list1) {System.out.print(e + " ");}} else {System.out.println("沒有交集!");}} }運行結果:
 
3、給你兩個集合,要求合并{A} + {B}={C}。 要求集合C中不會有相同的元素。
//代碼 package com.practice1111; import java.util.Set; import java.util.HashSet; public class MergeCollection {public static void main(String[] args) {Integer[] a = { 1, 2, 3, 4, 5 };Integer[] b = { 3, 4, 5, 6, 7, 8, 9 };Set<Integer> A = new HashSet<Integer>(a.length);Set<Integer> B = new HashSet<Integer>(a.length);//添加數組元素到A集合for(int c: a) {A.add(c);}//打印A集合System.out.println("集合A中的元素為:");for(int re: A) {System.out.print(re + " ");}//添加數組元素到B集合for(int d: b) {B.add(d);}System.out.println();//打印B集合System.out.println("集合B中的元素為:");for(int re: b) {System.out.print(re + " ");}//合并A集合和B集合for(int e: A) {B.add(e);}System.out.println();System.out.println("合并后的集合為:");for(int f : B) {System.out.print(f + " ");}} }運行結果為:
4、(list集合)輸入工人信息:
| zhang3 | 18 | 3000 | 
| li4 | 25 | 3500 | 
| wang5 | 22 | 3200 | 
在li4 之前插入一個工人,信息為:姓名:zhao6,年齡:24,工資3300 。
刪除wang5 的信息 。
遍歷,打印List 中所有工人的信息 。
利用迭代遍歷。
運行結果為:
 
5、某班級有若干學生(學生對象放在一個List中),每個學生有一個姓名屬性、班級名稱屬性(String)和考試成績屬性(int)。某次考試結束后,每個學生都獲得了一個考試成績,請打印出這個班級的總分和平均分。
//代碼 package com.practice1111; import java.util.List; import java.util.ArrayList;public class StudentTest {public static void main(String[] args) {// TODO Auto-generated method stubStudent s1 = new Student("zhangsan", "一班", 85);Student s2 = new Student("lisi", "一班", 97);Student s3 = new Student("wangwu", "一班", 76);Student s4 = new Student("zhaoliu", "二班", 86);Student s5 = new Student("cuiqi", "二班", 98);Student s6 = new Student("sunba", "二班", 92);Student s7 = new Student("lijiu", "二班", 100);int totleScore1 = 0; int totleScore2 = 0;List list1 = new ArrayList();list1.add(s1);list1.add(s2);list1.add(s3);List list2 = new ArrayList();list2.add(s4);list2.add(s5);list2.add(s6);list2.add(s7);for(Object s : list1) {Student student = (Student) s;totleScore1 += student.getscore();}for(Object s : list2) {Student student = (Student) s;totleScore2 += student.getscore();}System.out.println("一班的總分為:" + totleScore1 + "分" + " 平均分為:" + (double)totleScore1/list1.size()+ "分");System.out.println("二班的總分為:" + totleScore2 + "分" + " 平均分為:" + (double)totleScore2/list2.size()+ "分");}} class Student{String name;String className;int score;public Student(String name, String className, int score) {super();this.name = name;this.className = className;this.score = score;}public String getName() {return name;}public void setName(String name) {this.name = name;}public String getClassName() {return className;}public void setClassName(String className) {this.className = className;}public int getscore() {return score;}public void setscore(int score) {this.score = score;}}運行結果為:
總結
以上是生活随笔為你收集整理的Java — set 和 list 集合练习题的全部內容,希望文章能夠幫你解決所遇到的問題。
 
                            
                        - 上一篇: 系统分析师——论文篇(三)
- 下一篇: 组态王与松下PLC FP系列连接
