【java集合框架类】SDUT 实验四(1)

I 2714下沉的船

一艘船很不幸的撞上了暗礁,船长组织大家上救生艇,而且船长决定女人(woman)和小孩(child)先上船,然后其次是男人(man),最后是船长(captain)。给出原来的顺序,通过编程得到按船长要求排列后的顺序。

身份优先级: woman = child > man > captain.

注意:如果两者的身份优先级一样,请按原来的序列的顺序决定,原序列中在前面的人,依然在前面。

import java.math.BigInteger;
import java.util.*;

public class Main {
	public static void main(String[] args) {
		Scanner s = new Scanner(System.in);
		while(s.hasNext())
		{
			int t=s.nextInt();
			List<String>list1=new ArrayList<String>();
			List<String>list2=new ArrayList<String>();
			List<String>list3=new ArrayList<String>();
			while(t-->0)
			{
				String a=s.next();
				String b=s.next();
				if(b.equals("woman")||b.equals("child"))
					list1.add(a);
				else if(b.equals("man"))
					list2.add(a);
				else if(b.equals("captain"))
					list3.add(a);
			}
			Iterator<String> iter1=list1.iterator();
			while(iter1.hasNext())
			{
				String str=iter1.next();
				System.out.println(str);
			}
			Iterator<String> iter2=list2.iterator();
			while(iter2.hasNext())
			{
				String str=iter2.next();
				System.out.println(str);
			}
			Iterator<String> iter3=list3.iterator();
			while(iter3.hasNext())
			{
				String str=iter3.next();
				System.out.println(str);
			}
		}
		s.close();
	}
}

 J 2397 分类游戏

第一行给出m,n,第二行给出m个前缀(单个大写字母),后n行每行有一个单词。输出每个前缀所匹配的一个或多个单词,没有不输出。(忽略单词首字母大小写)

import java.math.BigInteger;
import java.util.*;

public class Main {
	public static void main(String[] args) {
		Scanner s = new Scanner(System.in);
		while(s.hasNext())
		{
			int m=s.nextInt();
			int n=s.nextInt();
			char t[]=new char[10];
			for(int i=0;i<m;i++)
			{
				String str=s.next();
				t[i]=str.charAt(0);
		    }
			List[] l=new List[120];
			for(int i=0;i<n;i++)
				l[i]=new ArrayList();
			for(int i=0;i<n;i++)
			{
				String s1=s.next();
				for(int j=0;j<m;j++)
				{
					if(s1.toUpperCase().charAt(0)==t[j])
						{
							l[j].add(s1);
							break;
						}
				}
			}
			for(int i=0;i<m;i++)
			{
				if(l[i].size()==0)
					continue;
				for(int j=0;j<l[i].size();j++)
				{
					System.out.print(l[i].get(j));
					if(j==l[i].size()-1)
						System.out.println();
					else
						System.out.print(" ");
				}
			}
			System.out.println();
		}
		s.close();
	}
}

ck找女朋友

import java.util.*;

 class God {
	String name;
	int high,age;
	String tel;
	public God(String name, int high, int age, String tel) {
		super();
		this.name = name;
		this.high = high;
		this.age = age;
		this.tel = tel;
	}
	@Override
	public int hashCode() {
		final int prime = 31;
		int result = 1;
		result = prime * result + age;
		result = prime * result + high;
		result = prime * result + ((name == null) ? 0 : name.hashCode());
		result = prime * result + ((tel == null) ? 0 : tel.hashCode());
		return result;
	}

	@Override
	public boolean equals(Object obj) {
		if (this == obj)
			return true;
		if (obj == null)
			return false;
		if (getClass() != obj.getClass())
			return false;
		God other = (God) obj;
		if (age != other.age)
			return false;
		if (high != other.high)
			return false;
		if (name == null) {
			if (other.name != null)
				return false;
		} else if (!name.equals(other.name))
			return false;
		if (tel == null) {
			if (other.tel != null)
				return false;
		} else if (!tel.equals(other.tel))
			return false;
		return true;
	}

	@Override
	public String toString() {
		return name + " " + high + " " + age + " " + tel ;
	}
	
}

public class Main {

	public static void main(String[] args) {
		Scanner s=new Scanner(System.in);
		List<God>list=new ArrayList<God>();
		while(s.hasNext())
		{
		int n=s.nextInt();
		int a=s.nextInt();
		int b=s.nextInt();
		int c=s.nextInt();
		int d=s.nextInt();
		while(n-->0)
		{
			String name=s.next();
			int high=s.nextInt();
			int age=s.nextInt();
			String tel=s.next();
			if(high>=a&&high<=b&&age>=c&&age<=d)
			{
				God x=new God(name,high,age,tel);
				if(!list.contains(x))
						list.add(x);
			}
			Collections.sort(list, new Comparator<God>() {

				@Override
				public int compare(God o1, God o2) {
					if(o1.high==o2.high)
						return o2.age-o1.age;
					else
						return o1.high-o2.high;
				}
			});
		}
		System.out.println(list.size());
		Iterator<God>it=list.iterator();
		while(it.hasNext())
		{
			System.out.println(it.next());
		}
		}
	}
}

SDUT 3360 学生信息的添加与查询

Problem Description

设计一个学生添加和查询的系统,从键盘读入学生的数据,然后再从屏幕显示出来。

Input

第一行有2个整数N和M,其中:N——学生数量,M——学生属性数量;
第二行有M个字符串,表示学生的属性名称,其中第1个属性id表示关键字;其中各字段属性的数据类型是确定的。
接下来有N行M列数据,分别表示学生各种属性的值,关键字相同的记录代表一个学生(后来读入的信息覆盖前面读入数据)

Output

输出所有学生的属性及数据。(每行的列数据之间用‘\t’进行分隔)

Sample Input

5 4
id name birthday score
0001 Mike 1990-05-20 98.5
0002 John 1992-05-20 67
0003 Hill 1994-05-02 36.5
0004 Christ 1996-05-20 86.5
0001 Jack 1998-05-20 96
Sample Output

id:0001    name:Jack    birthday:1998_5_20    score:96.0
id:0002    name:John    birthday:1992_5_20    score:67.0
id:0003    name:Hill    birthday:1994_5_2    score:36.5
id:0004    name:Christ    birthday:1996_5_20    score:86.5

import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.*;
 
class Student {
	String id, name, date;
	double score;
 
	public Student(String id, String name, String date, double score) {
		super();
		this.id = id;
		this.name = name;
		this.date = date;
		this.score = score;
	}
 
	String dateConvert(String date) throws ParseException {
		SimpleDateFormat sdf1 = new SimpleDateFormat("yyyy-MM-dd");
		SimpleDateFormat sdf2 = new SimpleDateFormat("yyyy_M_d");
		Date date1 = sdf1.parse(date);
		return sdf2.format(date1);
 
	}
 
	@Override
	public String toString() {
		String str = null;
		try {
			str = "id:" + id + "\tname:" + name + "\tbirthday:" + dateConvert(date) + "\tscore:"
					+ String.format("%.1f", score);
		} catch (ParseException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		return str;
	}
 
}
 
public class Mainn {
 
	public static void main(String[] args) {
		Scanner s = new Scanner(System.in);
		int n = s.nextInt();
		Map<String, Student> map = new HashMap<String, Student>();
 
		s.nextLine();
		s.nextLine();
 
		for (int i = 1; i <= n; i++) {
			Student stu = new Student(s.next(), s.next(), s.next(), s.nextDouble());
			map.put(stu.id, stu);
		}
		Set<String> keySet = map.keySet();
		List<String> list = new ArrayList<String>(keySet);
		Collections.sort(list);
		for (String id : list) {
			Student student = map.get(id);
			System.out.println(student);
		}
		s.close();
	}
 
}

 

附学习集合链接:

我们为什么要使用List和Set(List,Set详解):https://blog.csdn.net/qq_34149805/article/details/68943004,作者:ConardLi

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
掌握集合的概念、体系结构、分使用场景 2)了解Set接口及主要实现(HashSet、TreeSet) 3)了解List接口及主要实现(ArrayList、LinkedList、Vector) 4)了解Map接口及主要实现(HashMap、TreeMap、HashTable) 二、实验内容及步骤 1、编写程序练习将以下5个Person的对象放在一个HashSet中。 姓名:张三 身份证号:178880001 姓名:王五 身份证号:178880002 姓名:李 身份证号:178880003 姓名:王五 身份证号:178880002 姓名:李 身份证号:178880004 注意:因为Person是自定义,需要重写hashCode()方法和equals()方法,并规定只有姓名和身份证号都相等,则对象相等。 其中计算哈希码的算法:(31 + ((name == null) ? 0 : name.hashCode()))*31 + id (注:name:Person对象的姓名,id:Person对象的身份证号) 主方法中作如下测试: 1)创建一个可放置Person对象的HashSet; 2)依次添加上述5个对象到HashSet中; 3)把集合中的元素打印出来(使用迭代器Iterator) 2、编写程序练习List集合的基本使用: 1) 创建一个只能容纳String对象名为names的ArrayList集合; 2)按顺序往集合中添加5个字符串对象:"张三"、"李"、"王五"、"马六"、"赵七"; 3)对集合进行遍历,分别打印集合中的每个元素的位置与内容; 4)打印集合的大小,然后删除集合中的第3个元素,并显示删除元素的内容,然后再打印目前集合中第3个元素的内容,并再次打印集合的大小。 3、编写程序练习Map集合的基本使用: 1)创建一个只能容纳String对象的person的HashMap集合; 2)往集合中添加5个"键-值"对象: "id"-"1"; "name"-"张三"; "sex"-"男"; "age"-"25"; "hobby"-"爱Java" 3)对集合进行遍历,分别打印集合中的每个元素的键与值; 4)打印集合的大小,然后删除集合中的键为age的元素,并显示删除元素的内容,并再次打印集合的大小。 、思考题 1、集合中的List、Set、Map有哪些不同? 2、为什么使用集合框架,而尽可能少用数组作为存储结构? 3、如何使用TreeSet实现第一题?

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值