Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/java/307.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/3/arrays/14.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Java 获取名称列表/数组,并计算每个唯一名称的列出次数_Java_Arrays_String_List - Fatal编程技术网

Java 获取名称列表/数组,并计算每个唯一名称的列出次数

Java 获取名称列表/数组,并计算每个唯一名称的列出次数,java,arrays,string,list,Java,Arrays,String,List,此代码的输入为: "John, Mary, Joe, John, John, John, Mary, Mary, Steve." 我的目标是打印出: “(姓名)获得(票数)票。” 以获胜者的声明结束 但我似乎无法调试我的代码。这是我的代码: static void popularity_contest(List<String> name_list) { largest_count = ""; largest_count = 0; int n = name_

此代码的输入为:

"John, Mary, Joe, John, John, John, Mary, Mary, Steve."
我的目标是打印出:

“(姓名)获得(票数)票。”

以获胜者的声明结束

但我似乎无法调试我的代码。这是我的代码:

static void popularity_contest(List<String> name_list) {

    largest_count = "";
    largest_count = 0;
    int n = name_list.size();
    int count = 1;
    int y = sorted(name_list);
      for(i=1; i<name_list.length; i++){
          if (n[i] == n[i-1]){
              count += 1;
          }
              else
              {
                  name = n[i-1];
                  System.out.println(n[i-1] + " got " + str.length(count) + " votes.");
                  if (count > largest_count)
                  {
                          largest_count = count;
                          largest_name = name;
                          count = 1;
                   }
        System.out.println(str.length(y)-1 + " got " + str.length(count) + " votes.");
        name = str.length(y)-1;           
      }
          if (count > largest_count)
          {
              largest_count = count;
              largest_name = name;
            System.out.print(largest_name + " Wins!");
          }
      }
} 
static void popularity\u竞赛(列表名称\u列表){
最大_计数=”;
最大计数=0;
int n=name_list.size();
整数计数=1;
int y=已排序(名称列表);
对于(i=1;i最大_计数)
{
最大计数=计数;
最大名称=名称;
计数=1;
}
System.out.println(str.length(y)-1+“got”+str.length(count)+“vots”);
name=str.length(y)-1;
}
如果(计数>最大计数)
{
最大计数=计数;
最大名称=名称;
System.out.print(最大名称+Wins!”);
}
}
} 

如果允许您使用Java 8,使用streams和
收集器可以非常轻松地完成此操作。groupingBy()


虽然这可能太高级了,因为您是java新手。

如果允许您使用java 8,使用streams和
收集器可以非常轻松地完成此操作。groupingBy()


虽然这可能太高级了,因为您是java新手。

最大的错误在于您对名称的比较。当需要使用方法equals()或equalsIgnoreCase()时,您使用==来比较字符串。最后,您可以使用给定的列表,并在访问特定索引处的元素时使用get()方法

if (name_list.get(i).equalsIgnoreCase(name_list.get(i-1))
{
    count += 1;
}

最大的错误在于你对名字的比较。当需要使用方法equals()或equalsIgnoreCase()时,您使用==来比较字符串。最后,您可以使用给定的列表,并在访问特定索引处的元素时使用get()方法

if (name_list.get(i).equalsIgnoreCase(name_list.get(i-1))
{
    count += 1;
}

我喜欢@SchiduLuca的答案,但我想我会提出一个不使用流的解决方案。(在我的代码中,两个或多个获胜者之间可能有平局)

static void popularity\u竞赛(列表名称\u列表){
映射结果=新的HashMap();
用于(字符串名称:名称列表){
整数计数=result.get(name);
如果(计数=null){
计数=新整数(1);
结果。输入(姓名、计数);
}否则{
结果。输入(名称、计数+1);
} 
}
//打印结果并查找最大投票数
整数maxVotes=新整数(0);
对于(参赛者:result.entrySet()){
System.out.println(String.format(“%s获得了%d票”,competite.getKey(),competite.getValue().intValue());
if(competitor.getValue()>maxVotes){
maxVotes=Competitor.getValue();
}
}
//打印所有获奖者
System.out.println(“***获胜者)***”);
对于(参赛者:result.entrySet()){
if(competitor.getValue()==maxVotes){
System.out.println(String.format(“%s获得了%d张选票,并且是赢家”)、competitor.getKey()、competitor.getValue().intValue());
}
}
}

我喜欢@SchiduLuca的答案,但我想我会提出一个不使用流的解决方案。(在我的代码中,两个或多个获胜者之间可能有平局)

static void popularity\u竞赛(列表名称\u列表){
映射结果=新的HashMap();
用于(字符串名称:名称列表){
整数计数=result.get(name);
如果(计数=null){
计数=新整数(1);
结果。输入(姓名、计数);
}否则{
结果。输入(名称、计数+1);
} 
}
//打印结果并查找最大投票数
整数maxVotes=新整数(0);
对于(参赛者:result.entrySet()){
System.out.println(String.format(“%s获得了%d票”,competite.getKey(),competite.getValue().intValue());
if(competitor.getValue()>maxVotes){
maxVotes=Competitor.getValue();
}
}
//打印所有获奖者
System.out.println(“***获胜者)***”);
对于(参赛者:result.entrySet()){
if(competitor.getValue()==maxVotes){
System.out.println(String.format(“%s获得了%d张选票,并且是赢家”)、competitor.getKey()、competitor.getValue().intValue());
}
}
}

您所说的“我似乎无法调试我的代码”是什么意思?你试过什么?你的工作环境是什么?我似乎不能调试我的代码为什么不能?你的意思是你甚至不能编译你的代码,对吗?你每次至少需要两个循环来比较名称。你说的“我似乎不能调试我的代码”是什么意思?你试过什么?你的工作环境是什么?我似乎不能调试我的代码为什么不?你的意思是你甚至不能编译你的代码,对吗?你至少需要2个循环来比较每次的名称。
if (name_list.get(i).equalsIgnoreCase(name_list.get(i-1))
{
    count += 1;
}
static void popularity_contest(List<String> name_list) {

  Map<String, Integer> result = new HashMap<>();

  for (String name : name_list) {
    Integer count = result.get(name);
    if (count == null) {
      count = new Integer(1);
      result.put(name, count);
    } else {
      result.put(name, count + 1);
    } 
  }

  //Print result and look for max # votes
  Integer maxVotes = new Integer(0);

  for (Entry<String, Integer> contestant : result.entrySet()) {
    System.out.println(String.format("%s got %d votes", contestant.getKey(), contestant.getValue().intValue()));
    if (contestant.getValue() > maxVotes) {
      maxVotes = contestant.getValue();
    }
  }

  //Print all winners
  System.out.println("*** Winner(s) ***");
  for (Entry<String, Integer> contestant : result.entrySet()) {        
    if (contestant.getValue() == maxVotes) {
      System.out.println(String.format("%s got %d votes and is a winner", contestant.getKey(), contestant.getValue().intValue()));
    }
  }
}