zoukankan      html  css  js  c++  java
  • 关于重读字母去掉的代码

    /**短语"hello world"中哪些字母重复出现,重复出现多少次,第一个重复的字母是哪一个。 以此写出基本代码及注释**/ 分析出l和o 首先将重复字母挑选出需要遍历及计数--数组 count("hello world")=11 for(i=0;i<11;i++){   } ------------ public static void main(String[] args) {         Map<Character, Integer> map = new HashMap<>();//创建一个map<字母,出现次数>         Character first=null;//创建一个char对象代表第一个出现重复的         String a= "hello world";         char[] charArray = a.trim().toCharArray();//获取字符串里所有字符         for (char c : charArray) { //循环字符数组             Integer num = map.get(c);//从map里获取次数,如为null,带表第一次出现             if (num==null) {                 num=1;             }else{                 if (first==null) {//如果不为空且上面声明的first对象为空,则是第一个出现重复的字母,将次数+1                     first=c;                 }                 num++;             }             map.put(c, num);         }         for (char c : map.keySet()) {             Integer num = map.get(c);             if (num>1) {    //次数大于1代表重复,打印出来                 System.out.println("字母"+c+"重复出闲"+map.get(c)+"次!");             }         }         System.out.println("第一个重复的字母是:"+first);     }                   char[] chars =  "hello world".toCharArray(); Map<Character, Integer> mappingCount = new HashMap<>(); for(char c : chars){     mappingCount.computeIfPresent(Character.valueOf(c),    (k,v) -> v  +1);     mappingCount.putIfAbsent(Character.valueOf(c), 1); } System.out.println(mappingCount); 对我有用[0] 丢个板砖[0] 引用 | 举报 | 管理 soton_dolphin     关注     soton_dolphin      soton_dolphin      本版等级:T6      #4 得分:0 回复于: 2017-07-06 15:40:16 Java code       char[] chars =  "hello world".toCharArray(); Map<Character, Integer> mappingCount = new HashMap<>(); for(char c : chars){     mappingCount.merge(Character.valueOf(c), 1, (oldV, newV) -> oldV + 1); } System.out.println(mappingCount);        soton_dolphin     关注     soton_dolphin      soton_dolphin      本版等级:T6 没看到还要报告第一个重复的字母 Java code       char[] chars =  "hello world".toCharArray();         Map<Character, Integer> mappingCount = new HashMap<>();         LinkedList<Character> repeatedChars = new LinkedList<>();         for(char c : chars){             mappingCount.computeIfPresent(Character.valueOf(c), (k, v) -> {repeatedChars.offer(c); return v + 1;});             mappingCount.putIfAbsent(Character.valueOf(c), 1);         }         System.out.println(mappingCount);         System.out.println("first repeated character: " + repeatedChars.poll());      char[] chars =  "hello world".toCharArray(); Map<Character, Integer> mappingCount = new HashMap<>(); for(char c : chars){     mappingCount.merge(Character.valueOf(c), 1, (oldV, newV) -> oldV + 1); } System.out.println(mappingCount);

    总结很重要哦 方法得当,坚持会有奇迹哦
  • 相关阅读:
    PredictionIO+Universal Recommender快速开发部署推荐引擎的问题总结(1)
    SpringJDBC的JdbcTemplate在MySQL5.7下不支持子查询的问题
    POP3接收邮件
    发送邮件
    电子邮件介绍
    线程优先级队列
    线程同步
    threading模块
    _thread模块
    使用线程
  • 原文地址:https://www.cnblogs.com/sunyuhuan/p/7200276.html
Copyright © 2011-2022 走看看