1.分析以下需求,并用代码实现:
(1)定义一个学生类Student,包含属性:姓名(String name)、年龄(int age)
(2)定义Map集合,用Student对象作为key,用字符串(此表示表示学生的住址)作为value
(3)利用四种方式遍历Map集合中的内容,格式:key::value
/*
public class Demo1_Test1 {
/**
(1)定义一个学生类Student,包含属性:姓名(String name)、年龄(int age)
(2)定义Map集合,用Student对象作为key,用字符串(此表示表示学生的住址)作为value
(3)利用四种方式遍历Map集合中的内容,格式:key::value
public static void main(String[] args) {
Map<Student, String> hmp = new HashMap<Student, String>(); //定义一个map集合
hmp.put(new Student("张三", 23), "北京"); //添加元素
hmp.put(new Student("李四", 24), "上海");
hmp.put(new Student("武王", 25), "广州");
Set<Student> set = hmp.keySet(); //获取所有键的结合
Iterator<Student> it = set.iterator(); //后去学生的迭代器
Iterator it1 = hmp.entrySet().iterator(); //
method4(hmp);
// method3(hmp);
// method2(map, set);
// System.out.println("-------------------------------------");
// method1(map, it);
}
public static void method4(Map<Student, String> hmp) {
Iterator<Map.Entry<Student, String>> it11 = hmp.entrySet().iterator();
while (it11.hasNext()) {
Map.Entry<Student, String> en = it11.next();
System.out.println(en.getKey().getName() + en.getKey().getAge() + en.getValue());
}
}
public static void method3(HashMap<Student, String> hmp) {
for (Map.Entry<Student, String> stu : hmp.entrySet()) {
System.out.println( stu.getKey().getName() + stu.getKey().getAge() + stu.getValue() );
}
}
public static void method2(Map<Student, String> map, Set<Student> set) {
for (Student s : set) {
System.out.println(s.getName() + s.getAge() + map.get(s));
}
}
public static void method1(Map<Student, String> map, Iterator<Student> it) {
while (it.hasNext()) {
Student s = it.next();
System.out.println(s.getName() + s.getAge() + map.get(s));
}
}
*/
2.分析以下需求,并用代码实现:
(1)利用键盘录入,输入一个字符串
(2)统计该字符串中各个字符的数量
(3)如:
用户输入字符串"If~you-want~to~change-your_fate_I_think~you~must~come-to-the-dark-horse-to-learn-java"
程序输出结果:-(9)I(2)_(3)a(7)c(2)d(1)e(6)f(2)g(1)h(4)i(1)j(1)k(2)l(1)m(2)n(4)o(8)r(4)s(2)t(8)u(4)v(1)w(1)y(3)~(6)
/*
public static void main(String[] args) {
Scanner sc1 = new Scanner(System.in); //创建键盘录入对象
System.out.println("请输入一个字符串:");
String s1 = sc1.nextLine(); //接收录入的字符串
char [] c1 = s1.toCharArray(); //将字符串转换成字符数组
LinkedHashMap<Character, Integer> lhm = new LinkedHashMap<>(); //创建map集合
Arrays.sort(c1); //调用sort方法,将字符数组排序
for (char d : c1) {
lhm.put(d, lhm.containsKey(d) ? lhm.get(d) + 1 : 1); //将字符存进集合的键,如果有重复的,值加1
}
for (Character c : lhm.keySet()) {
System.out.print(c + "(" + lhm.get(c) + ")"); //遍历并打印数组
}
}
*/
3.分析以下需求,并用代码实现:
(1)统计每个单词出现的次数
(2)有如下字符串"If you want to change your fate I think you must come to the dark horse to learn java"(用空格间隔)
(3)打印格式:
to=3
think=1
you=2
//........
/*
public static void main(String[] args) {
String s1 = "If you want to change your fate I think you must come to the dark horse to learn java";
String[] s2 = s1.split(" "); //将字符串切割字符数组
HashMap<String, Integer> hm= new HashMap<>(); //创建map集合
for (String st : s2) {
hm.put(st, hm.containsKey(st) ? hm.get(st) + 1 : 1); //遍历字符串数组,将字符添加到集合的键中,如果有重复,值加1
}
for (String s : hm.keySet()) {
System.out.println(s + " = " + hm.get(s)); //遍历集合
}
}
*/
4.练习今天的课堂代码
|
|