有 Java 编程相关的问题?

你可以在下面搜索框中键入要查询的问题!

java For循环混乱,为什么不是循环?

所以我有一个方法,在这个方法中我传递了一个ArrayList,这个方法的思想是获取输入,并将每一组匹配的字符串分割成各自独立的列表。这里有一些伪代码

Array = (a,a,a,b,c,d,d,e,e,e,e,e,f,f,s)

所以我想要这个算法做的是把这个数组分成一个元素相等的二维数组。像这样

A[0][] = (a,a,a)
A[1][] = (b)
A[2][] = (c)
A[3][] = (d,d)
A[4][] = (e,e,e,e,e)
A[5][] = (f,f)
A[6][] = (s)

所以我试着把它放在一个for循环中,检查前面的一个额外元素,看它是否不等于,然后它知道t

 int equalStringGroupIndex = 0;
    int i = 0;

    for(int first = 0, second = 0 ; input.get(first).equals(input.get(second)); second++){
        equalStringGroups[equalStringGroupIndex][i] = input.get(second);
        i++;
        //This if statment checks the element ahead then equals first = second, But when it jumps back to the top of the loop in the debugger it does'nt seem to check it even though in my Watches it's True
        if(!input.get(first).equals(input.get(second + 1))){
            equalStringGroupIndex++;
            i = 0;
            first = second;
        }
    }

为什么在将第一组“a”添加到2D数组后不循环 谢谢

更新: 谢谢你的帮助,我决定走HashMap路线。这是我想到的。看来这很管用

private HashMap<String, Integer> countDuplicates(ArrayList<String> input){

    HashMap<String, Integer> duplicates = new HashMap<>();

    //Value init loop, sets all values to 0;
    for (String s : input){
        Integer valueInitVar = 0;
        duplicates.put(s, valueInitVar);
    }

    //Increases the value by 1  each time the same key is encountered;
    for (String s : input){
        Integer tempDuplicateAmount = duplicates.get(s);
        //I could use the '++' operator but I feel ' var += 1' is much nicer to read;
        tempDuplicateAmount += 1;
        duplicates.put(s, tempDuplicateAmount);
    }

    return duplicates;
}

共 (2) 个答案

  1. # 1 楼答案

    我还建议:

    List<List<Whatever>> splitSame(List<Whatever> input) {
      List<List<Whatever>> rv = new ArrayList<>();
      List<Whatever> currentValues = new ArrayList<>(); 
      Whatever lastChecked = input.get(0);
      for (Whatever what : input) {
        if (lastChecked.equals(what)) {
          currentValues.add(what);
        } else {
          rv.put(currentValues);
          currentValues = new ArrayList<>();
        }
        lastChecked = what;
      }
      rv.put(currentValues);
      return rv;
    

    要点是:您可以轻松地遍历一个输入列表,以在子列表中收集相等的对象。最后,你只需要把它们放在一起

    我相信一些java8大师可以使用streams将上述内容改写成更简洁、更不复杂的内容

    这里根本不需要额外的二维阵列复杂性

    (提示:刚刚写下的代码可能包含拼写错误,当last元素不同时,代码中有一个微妙的错误,但是:它是为了让您了解如何以不同的方式进行操作;更多细节留给读者)

  2. # 2 楼答案

    如果要坚持这样做,我建议将for循环中的条件更改为second < input.size (or length)

    int equalStringGroupIndex = 0;
    int i = 0;
    
    for(int first = 0, second = 0 ; second < input.size(); second++){
        equalStringGroups[equalStringGroupIndex][i] = input.get(second);
        i++;
    
        if(!input.get(first).equals(input.get(second + 1))){
            equalStringGroupIndex++;
            i = 0;
            first = second+1;
        }
    }
    

    这样,您只在数组中循环,没有越界异常

    我还将您的first = second更改为first = second+1,因为一旦您发现它们不相等,您应该将第一个索引设置为下一个索引,通过执行first = second,您就不相等了