【问题标题】:Counting the occurrences of a String in an ArrayList计算 ArrayList 中字符串的出现次数
【发布时间】:2013-11-13 22:39:09
【问题描述】:

我正在使用 ArrayList 来保存节目的名称、日期和时间。我的程序要求我输出一周中每一天播放的节目数量。我输入了 4 个不同的节目,其中两个在同一天。到目前为止,输出给我的唯一信息是“星期四有/是 2 个节目”。另外两个没有出现。我怎样才能让它显示我输入的每一天的节目数量?这是我的代码:

    String showDay = ((showInfo)show.get(0)).day.toString();
    int totalShows = 0;

    //print occurences for how many shows play each day
    for (int i = 0; i < show.size(); i++) {
           if (showDay.equals(((showInfo)show.get(i)).day.toString())) {
                totalShows++;
           }
      }

      System.out.println("On " + showDay + " there are/is " + totalShows + " show(s).");
 }

这是我输入的节目的代码:

//input information
    do{
        showInfo temp = new showInfo();

        System.out.print("Enter the name of show: ");
        String showName = br.readLine();
        temp.name = showName;

        System.out.print("Enter which day of the week a new episode premieres: ");
        String showDay = br.readLine();
        temp.day = showDay;

        System.out.print("Enter time in 24-hour format (e.g. 2100, 1900): ");
        int showTime = Integer.valueOf(br.readLine()).intValue();
        temp.time = showTime;

        show.add(temp);            

        System.out.print("Would you like to add another show? (y/n) ");
    }
    while((br.readLine().compareTo("n")) != 0);

请记住,我使用的是 Java 1.4。没有其他选择。应老师要求。

这可能很明显,但我现在忘记了。任何帮助都会很棒!提前致谢!

编辑:

String showDay = ((showInfo)show.get(0)).day.toString();

    if("sunday".equalsIgnoreCase(showDay)){
        showdayCount[0]++;
        System.out.println("There are/is " + showdayCount[0]++ + " show on Sunday.");
    }else if("monday".equalsIgnoreCase(showDay)){
        showdayCount[1]++;
        System.out.println("There are/is " + showdayCount[1]++ + " show on Monday.");
    }else if("tuesday".equalsIgnoreCase(showDay)){
        showdayCount[2]++;
        System.out.println("There are/is " + showdayCount[2]++ + " show on Tuesday.");
    }else if("wednesday".equalsIgnoreCase(showDay)){
        showdayCount[3]++;
        System.out.println("There are/is " + showdayCount[3]++ + " show on Wednesday.");        
    }else if("thursday".equalsIgnoreCase(showDay)){
        showdayCount[4]++;
        System.out.println("There are/is " + showdayCount[4]++ + " show on Thursday.");
    }else if("friday".equalsIgnoreCase(showDay)){
        showdayCount[5]++;
        System.out.println("There are/is " + showdayCount[5]++ + " show on Friday.");
    }else if("saturday".equalsIgnoreCase(showDay)){
        showdayCount[6]++;
        System.out.println("There are/is " + showdayCount[6]++ + " show on Saturday.");
    }

这也只给我一行。我究竟做错了什么?! :(

编辑:

我想要的是输入电视节目的名称/日期/时间,然后能够显示该特定日期的节目数量!

例如,生活大爆炸和社区都在星期四。所以代码会输出类似

There are 2 shows on Thursday.

到目前为止,没有任何效果。

【问题讨论】:

  • 那个老老师不想更新他的JDK,因为他什么都不懂!
  • 你的问题到底是什么?
  • 为什么不使用 getter 和 setter 方法?为什么不使用getDate(),让它返回一个字符串并将其与所需的星期几进行比较?
  • System.out.println("On " + showDay + " there are/is " + totalShows + " show(s)."); 这只会被调用一次。你怎么能指望它打印另外两个?
  • 我真的要问这个问题:为什么老师让你写Java 1.4代码?为什么?他们在伤害你。

标签: java arraylist variable-assignment


【解决方案1】:

你只能从这条线上得到一天:

String showDay = ((showInfo)show.get(0)).day.toString(); 

这是我的 hack 解决方案(可能有一些错误,例如命名错误的变量,但您应该能够修复它):

 //Create a map that maps days to the shows that are on that day
 LinkedHashMap showDays=new LinkedHashMap(); //String to Set map
 String[] days={"Sunday","Monday", ....};//put the rest of the days in here
 for(int dayIndex=0;dayIndex<days.length;dayIndex++){
     String day=days[dayIndex];
     showDays.put(day,new HashSet());
 }

 //iterate through the shows and put them in their respective days in the map
 for (int i = 0; i < show.size(); i++) {
     String dayForShow=((showInfo)show.get(i)).day.toString();
     showDays.get(dayForShow).put(show.get(i));
 }

 //now print out how many shows are on each day
 for(int dayIndex=0;dayIndex<days.length;dayIndex++){
     String day=days[dayIndex];
     int showsToday=showDays.get(day).size();
    ///do your print out now
 }

您可以将 'LinkedHashMap showDays' 设为类变量,并在每次添加到 show 时添加到其中(每次从 show 中删除时都可以从中删除)。

PS。告诉你的老师,现在已经不是 2002 年了,技术在进步,它们让我的工作变得更加困难。

【讨论】:

    【解决方案2】:

    创建一个Show

    public class Show {
        String showName;
        String showDay;
        int showTime;
        static int[] showdayCount = new int[7];
    
        public Show(String showName, String, showDay, iny showTime){
            this.showName = showName;
            this.showDay = showDay;
            this.showTime = showTime;
    
            // increment showDayCOunt[] according to showDay from 
            switch(showDay){
                case "Sunday": showDayCount[0]++; break;
                case "Monday": showDayCount[1]++; break;
                case "Tuesday": showDayCount[2]++; break;
                case "Wednesday": showDayCount[3]++; break;
                case "Thursday": showDayCount[4]++; break;
                case "Friday": showDayCount[5]++; break;
                case "Saturday": showDayCount[6]++; break;
    
            }
    
        }
    }
    

    这是 YourClass 中的主要方法

    public static void main(String[] args){
        // Create Array of Shows
        Show[] shosList = new Show[4]; // or how many ever shows you have
    
        // Get your inputs for showName, showDay, showTime
        Sting showName = 
        String showDay = 
        int showTime = 
    
    
        // Create show Object
        Show show = new Show(showName, showDate, showTime);
    
        // add show to ArrayList
        showList[someIndex] = show;
    
        // Do all other stuff then print
        // when you print, you can get the showDayCount directly from the Show class
    
    }
    

    现在我不想为你做所有事情。我只是想让你朝着正确的方向前进。请注意,如果您希望上述情况发生多次,请考虑放入循环中。

    编辑:使用 getShowdayCount

    将此添加到我上面的 Show 类中

    public int getShowdayCount(String showday){
        switch(showDay){
            case "Sunday": return showDayCount[0]; break;
            case "Monday": return showDayCount[1]; break;
            case "Tuesday": return showDayCount[2]++; break;
            case "Wednesday": return showDayCount[3]++; break;
            case "Thursday": return showDayCount[4]++; break;
            case "Friday": return showDayCount[5]++; break;
            case "Saturday": return showDayCount[6]++; break;
    
        }
    }
    

    编辑:if/else if 为例,来自 switch 语句

    // for main method inputs
    if ("sunday"equalsIgnoreCase(showDay){
        showdayCount[0]++
    } else ("monday".equalsIgnoreCase(showDay){
        showdayCount[1]++
    } // finish if/else if statement
    
    
    // for getShowdayCount method
    if ("sunday"equalsIgnoreCase(showDay){
        return showdayCount[0];
    } else ("monday".equalsIgnoreCase(showDay){
        return showdayCount[1];
    } // finish if/else if statement
    

    您可以像这样从 main 类中调用它

    show.getShowdayCount(show.showday);
    

    编辑:实际上,只需使用main 方法将上述编辑放入您的类中

    YourClassWithMainMethod{
    
        static int[] showdayCount = new int[7];        
    
        public static void mina(String[] args){
    
            // call getShowdayCount here
            getShowdayCount(String showday);
        }
    
        public static int getShowdayCount(String showday){
    
        }
    }
    

    编辑:你想要什么和你不想要什么

    你不需要这个 if 语句

     System.out.println("There are/is " + showdayCount[5]++ + " show on Friday.");
    

    当你想打印所有节目时:

    for (int i = 0; i < showdayCount.length; i++){
        int showday;
    
        if (i == 0){
           showday = "Sunday
           finish the if/else if statements
        }
        System.out.println("There is " + showDayCount[i] + " shows on " + showday);
    
    }
    

    【讨论】:

    • 他使用的是Java 1.4,所以不能使用泛型。他必须投。
    • @HovercraftFullOfEels 如果我是他,我会使用 NIO 的课程来激怒老师。
    • 谢谢!但我确实有课。不知道我会如何合并它。有没有办法可以修复我所做的工作以使其正常工作?是的,仿制药是不行的……@John 不想惹恼我的老师。我的考试是星期一!
    • @Sal,查看我的编辑。你可以像我一样用数组实现你的showinfo 类,然后像我在编辑中那样添加一个方法
    • @Sal,直到现在我才知道你到底想做什么。看看我上次的编辑
    猜你喜欢
    • 2014-04-24
    • 1970-01-01
    • 1970-01-01
    • 2021-07-06
    • 2012-06-24
    • 2020-02-21
    • 2012-02-12
    相关资源
    最近更新 更多