服务器租用网站模板河南第二建设集团网站视频
web/
2025/9/26 15:32:11/
文章来源:
服务器租用网站模板,河南第二建设集团网站视频,专业建设内容,泉州网站建设效率网络1.键盘录入一个字符串#xff0c;求该字符串中每一个字符出现的次数。 要求#xff1a;按照字母顺序打印 如: 录入的字符串为apple#xff0c;打印 a(1) e(1) l(1) p(2)
public class Demo4 {public static void main(String[] args) {//键盘录入Scanner sc n…1.键盘录入一个字符串求该字符串中每一个字符出现的次数。 要求按照字母顺序打印 如: 录入的字符串为apple打印 a(1) e(1) l(1) p(2)
public class Demo4 {public static void main(String[] args) {//键盘录入Scanner sc new Scanner(System.in);System.out.println(请输入字符串);String str sc.nextLine();//创建treeMap存储字符【键】个数【值】TreeMapCharacter, Integer map new TreeMap();//遍历输入的字符串//方法一str.length() str.charAt(i)得到每一个字符for (int i 0; i str.length(); i) {//得到每一个字符char ch str.charAt(i);//判断键在集合中是否存在if(map.containsKey(ch)){//如果存在则获取对应的值Integer value map.get(ch);//将值1并存入集合中map.put(ch,value1);}//否则不存在则值为1直接存入else {map.put(ch,1);}}//方法二s/*char[] charArr str.toCharArray();for (int i 0; i charArr.length; i) {if(map.containsKey(charArr[i])){//如果存在则获取对应的值Integer value map.get(charArr[i]);//将值1并存入集合中map.put(charArr[i],value1);}//否则不存在则值为1直接存入else {map.put(charArr[i],1);}}*///打印集合//录入的字符串为apple打印 a(1) e(1) l(1) p(2)SetMap.EntryCharacter, Integer entries map.entrySet();for (Map.EntryCharacter, Integer entry : entries) {System.out.print(entry.getKey()(entry.getValue()) );}System.out.println();}
}打印结果
-----------------------------------------------------------------
请输入字符串
asdagfasfas
a(4) d(1) f(2) g(1) s(3) 使用HashMap集合存储Student对象作为键学生的家庭住址作为值 要求要求学生的姓名和年龄不能重复 使用TreeMap集合存储Student对象作为键学生的家庭住址作为值 要求先按照年龄进行排序再按照姓名进行排序
测试类
public class Demo3 {public static void main(String[] args) {//1. 使用HashMap集合存储Student对象作为键学生的家庭住址作为值// 要求要求学生的姓名和年龄不能重复HashMapStudent, String hashMap new HashMap();hashMap.put(new Student(张三,20),武汉);hashMap.put(new Student(李四,21),孝感);hashMap.put(new Student(王五,20),武汉);hashMap.put(new Student(张三,22),安陆);hashMap.put(new Student(张三,20),汉川);//键值对遍历SetMap.EntryStudent, String entries hashMap.entrySet();for (Map.EntryStudent, String entry : entries) {System.out.println(entry.getKey()...entry.getValue());}//2. 使用TreeMap集合存储Student对象作为键学生的家庭住址作为值// 要求先按照年龄进行排序再按照姓名进行排序System.out.println(----------------------);TreeMapStudent, String treeMap new TreeMap();treeMap.put(new Student(zhangsan,20),武汉);treeMap.put(new Student(lisi,21),孝感);treeMap.put(new Student(wangwu,20),武汉);treeMap.put(new Student(zhangsan,22),安陆);treeMap.put(new Student(zhangsan,20),汉川);//键找值遍历SetStudent keys treeMap.keySet();for (Student key : keys) {String value treeMap.get(key);System.out.println(key...value);}}
}打印结果
---------------------------------------------------------------
Student{name王五, age20}...武汉
Student{name张三, age22}...安陆
Student{name张三, age20}...汉川
Student{name李四, age21}...孝感
----------------------
Student{namewangwu, age20}...武汉
Student{namezhangsan, age20}...汉川
Student{namelisi, age21}...孝感
Student{namezhangsan, age22}...安陆学生类
public class Student implements ComparableStudent {private String name;private int age;public Student(String name, int age) {this.name name;this.age age;}public Student() {}public String getName() {return name;}public void setName(String name) {this.name name;}public int getAge() {return age;}public void setAge(int age) {this.age age;}Overridepublic String toString() {return Student{ name name \ , age age };}Overridepublic boolean equals(Object o) {if (this o) return true;if (o null || getClass() ! o.getClass()) return false;Student student (Student) o;if (age ! student.age) return false;return name ! null ? name.equals(student.name) : student.name null;}Overridepublic int hashCode() {int result name ! null ? name.hashCode() : 0;result 31 * result age;return result;}Overridepublic int compareTo(Student o) {int result this.age-o.age;result result 0 ? this.getName().compareTo(o.getName()) : result;return result;}
}
本文来自互联网用户投稿,该文观点仅代表作者本人,不代表本站立场。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如若转载,请注明出处:http://www.mzph.cn/web/82259.shtml
如若内容造成侵权/违法违规/事实不符,请联系多彩编程网进行投诉反馈email:809451989@qq.com,一经查实,立即删除!