标签:集合实例 集合求随机数 集合录入自定义类排序 自然排序选择器排序 集合嵌套
1、键盘录入5个学生信息(姓名,语文成绩,数学成绩,英语成绩),按照总分从高到低输出到控制台。import java.util.Comparator;
import java.util.Scanner;
import java.util.TreeSet;
public class ZuoyeScannerSelect {
public static void main(String[] args) {
TreeSet<StudentSelect> ts=new TreeSet<StudentSelect>(new Comparator<StudentSelect>() {
@Override
public int compare(StudentSelect s1, StudentSelect s2) {
int num1=s2.sum(s2)-s1.sum(s1);
int num2=(num1==0)?s2.getName().compareTo(s1.getName()):num1;
return num2;
}
}) ;
System.out.println("请输入学生信息:");
//录入学生信息
for(int x=1;x<6;x++) {
Scanner sc=new Scanner(System.in);
System.out.print("请输入第"+x+"个学生的姓名:");
//录入学生姓名
String name=sc.nextLine();
System.out.print("请输入第"+x+"个学生的语文成绩:");
//录入学生语文成绩
int chineseScore=sc.nextInt();
System.out.print("请输入第"+x+"个学生的数学成绩:");
//录入学生数学成绩
int mathScore=sc.nextInt();
System.out.print("请输入第"+x+"个学生的英语成绩:");
//录入学生英语成绩
int englishScore=sc.nextInt();
//将学生信息封装到学生对象里
StudentSelect s=new StudentSelect();
s.setName(name);
s.setChineseScore(chineseScore);
s.setMathScore(mathScore);
s.setEnglishScore(englishScore);
//把学生对象添加到集合中
ts.add(s);
}
//遍历
for(StudentSelect s:ts) {
System.out.println(s.getName()+","+ + s.getChineseScore()+","
+ s.getMathScore()+","+ s.getEnglishScore()+","+s.sum(s));
}
}
}`
实体类:
package treeSet集合;
public class StudentZiran implements Comparable<StudentZiran>{
//自然排序,自定义类中实现Comparable接口
private String name;
private int chineseScore;
private int mathScore;
private int englishScore;
public StudentZiran() {//无参构造
// TODO Auto-generated constructor stub
}
//有参构造
public StudentZiran(String name,int chineseScore,int mathScore,int englishScore) {
this.name=name;
this.chineseScore=chineseScore;
this.mathScore=mathScore;
this.englishScore=englishScore;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public int getChineseScore() {
return chineseScore;
}
public void setChineseScore(int chineseScore) {
this.chineseScore = chineseScore;
}
public int getMathScore() {
return mathScore;
}
public void setMathScore(int mathScore) {
this.mathScore = mathScore;
}
public int getEnglishScore() {
return englishScore;
}
public void setEnglishScore(int englishScore) {
this.englishScore = englishScore;
}
//求总成绩方法
public int sum(StudentZiran s) {
return s.getChineseScore()+s.getMathScore()+s.getEnglishScore();
}
//重写compearTo()方法
@Override
public int compareTo(StudentZiran s) {
//主要排序条件:总成绩,按总成绩从高到低
int num1=s.sum(s)-this.sum(this);
//次要排序条件,当总成绩相同时按学生姓名内容比较
int num2=(num1==0)?s.getName().compareTo(this.getName()):num1;
return num2;
}
}
b:选择器排序
测试类:
`package treeSet集合;
import java.util.Comparator;
import java.util.Scanner;
import java.util.TreeSet;
public class ZuoyeScannerSelect {
public static void main(String[] args) {
TreeSet<StudentSelect> ts=new TreeSet<StudentSelect>(new Comparator<StudentSelect>() {
@Override
public int compare(StudentSelect s1, StudentSelect s2) {
int num1=s2.sum(s2)-s1.sum(s1);
int num2=(num1==0)?s2.getName().compareTo(s1.getName()):num1;
return num2;
}
}) ;
System.out.println("请输入学生信息:");
//录入学生信息
for(int x=1;x<6;x++) {
Scanner sc=new Scanner(System.in);
System.out.print("请输入第"+x+"个学生的姓名:");
//录入学生姓名
String name=sc.nextLine();
System.out.print("请输入第"+x+"个学生的语文成绩:");
//录入学生语文成绩
int chineseScore=sc.nextInt();
System.out.print("请输入第"+x+"个学生的数学成绩:");
//录入学生数学成绩
int mathScore=sc.nextInt();
System.out.print("请输入第"+x+"个学生的英语成绩:");
//录入学生英语成绩
int englishScore=sc.nextInt();
//将学生信息封装到学生对象里
StudentSelect s=new StudentSelect();
s.setName(name);
s.setChineseScore(chineseScore);
s.setMathScore(mathScore);
s.setEnglishScore(englishScore);
//把学生对象添加到集合中
ts.add(s);
}
//遍历
for(StudentSelect s:ts) {
System.out.println(s.getName()+","+ + s.getChineseScore()+","
+ s.getMathScore()+","+ s.getEnglishScore()+","+s.sum(s));
}
}
}`
实体类:
package treeSet集合;
public class StudentSelect {
private String name;
private int chineseScore;
private int mathScore;
private int englishScore;
public StudentSelect() {//无参构造
// TODO Auto-generated constructor stub
}
//有参构造
public StudentSelect(String name,int chineseScore,int mathScore,int englishScore) {
this.name=name;
this.chineseScore=chineseScore;
this.mathScore=mathScore;
this.englishScore=englishScore;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public int getChineseScore() {
return chineseScore;
}
public void setChineseScore(int chineseScore) {
this.chineseScore = chineseScore;
}
public int getMathScore() {
return mathScore;
}
public void setMathScore(int mathScore) {
this.mathScore = mathScore;
}
public int getEnglishScore() {
return englishScore;
}
public void setEnglishScore(int englishScore) {
this.englishScore = englishScore;
}
//求总成绩方法
public static int sum(StudentSelect s) {
return s.getChineseScore()+s.getMathScore()+s.getEnglishScore();
}
}
2、获取10随机数,1-20之间,要求值不能重复!(ArrayList,HashSet)
ArrayList:
package 集合获取随机数;
import java.util.ArrayList;
import java.util.Iterator;
import java.util.Random;
public class ArrayListGetRandom {
public static void main(String[] args) {
//创建集合al
ArrayList<Integer> al=new ArrayList<Integer>();
//创建新集合newal
ArrayList<Integer> newal=new ArrayList<Integer>();
//判断新集合newal中的元素个数是否够10个,若不够,继续添加
while(newal.size()<10) {
//创建获取随机数对象
Random r=new Random();
//获取随机数
int n=r.nextInt(20);
//将获取到的随机数添加到集合al集合
al.add(n);
//遍历旧集合
Iterator<Integer> it=al.iterator();
while(it.hasNext()) {
int i=it.next();
//判断newal中是否包含al中的元素,若不包含,将al中的元素添加到newal中
if(!newal.contains(i)) {
newal.add(i);
}
}
//遍历newal集合
for(int i:newal) {
System.out.println(i);
}
}
}
}
HashSet:
package 集合获取随机数;
import java.util.HashSet;
import java.util.Random;
import java.util.Set;
public class HashSetGetRandom {
public static void main(String[] args) {
//创建Set集合对象
Set<Integer> set=new HashSet<Integer>();
//创建获取随机数类对象
Random r=new Random();
//判断已获取随机数个数,若不够10个,则继续添加
while(set.size()<10) {
//获取随机数
int n=r.nextInt(20);
//将获取的随机数添加到集合set中
set.add(n);
}
//增强for循环遍历集合set
for(int i:set) {
System.out.println(i);
}
}
}
3、ArrayList集合嵌套HashMap集合并遍历。
需求:
假设ArrayList集合的元素是HashMap。有3个。
每一个HashMap集合的键和值都是字符串。
元素如下,请遍历。
ArrayList<HashMap<String,String>>
结果:
周瑜---小乔
吕布---貂蝉
郭靖---黄蓉
杨过---小龙女
令狐冲---任盈盈
林平之---岳灵珊
`package 集合嵌套;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.Set;
public class AM {
public static void main(String[] args) {
//创建一个大的ArrayList集合am
ArrayList<HashMap<String,String>> am=new ArrayList<HashMap<String,String>>();
//创建第一个HashMap集合hm1
HashMap<String,String> hm1=new HashMap<String,String>();
//给集合hm1添加元素
hm1.put("周瑜","小乔");
hm1.put("吕布","貂蝉");
//将hm1的元素添加到大集合am中
am.add(hm1);
//创建第二个HashMap集合hm2
HashMap<String,String> hm2=new HashMap<String,String>();
//给集合hm2添加元素
hm2.put("郭靖","黄蓉");
hm2.put("杨过","小龙女");
//将hm2的元素添加到大集合am中
am.add(hm2);
//创建第三个HashMap集合hm3
HashMap<String,String> hm3=new HashMap<String,String>();
//给集合hm3添加元素
hm3.put("令狐冲","任盈盈");
hm3.put("林平之","岳灵珊");
//将hm3的元素添加到大集合am中
am.add(hm3);
//遍历大集合am
for(HashMap<String,String> ahm:am) {
Set<String> set=ahm.keySet();
for(String key:set) {
String value=ahm.get(key);
System.out.println(key+"---"+value);
}
System.out.println();
}
}
}`
标签:集合实例 集合求随机数 集合录入自定义类排序 自然排序选择器排序 集合嵌套
原文地址:http://blog.51cto.com/13678728/2115388