有 Java 编程相关的问题?

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

java如何在字符串数组中查找重复字符串

我有下面的代码,我只想检索那些重复的值,但找不到正确的结果。我不知道我错在哪里

public class TestDummy {
    public static void main(String args[]){
       String arr[] ={"lady", "bird", "is","bird","lady","cook"};
       int len = arr.length;
       System.out.println("Size "+len);
       for(int i=0 ; i<=len;i++){
           for(int j=1 ; j< len-1;j++){
            if(arr[i]==arr[j]){
              System.out.println("Duplicate "+arr[i]);
          } 
       }  
           }
    }


}

共 (6) 个答案

  1. # 1 楼答案

    这将为您提供一个包含重复项及其计数的列表:

    var duplicates =
    from word in arr
    group word by word into g
    where g.Count() > 1
    select new { g.Key, Count = g.Count() };    
    
  2. # 2 楼答案

    有几个问题

       for(int i=0 ; i<len;i++){
           for(int j=i +1 ; j< len;j++){
            if(arr[i].equals(arr[j])){
              System.out.println("Duplicate "+arr[i]);
          } 
       }  
           }
    

    注意:我把j=1改为j=i,把==改为.equals,把<=改为<,把len -1改为len

  3. # 3 楼答案

    您必须将代码更改为:

    public static void main(String args[]) {
            String arr[] = { "lady", "bird", "is", "bird", "lady", "cook" };
            int len = arr.length;
            System.out.println("Size " + len);
            for (int i = 0; i < len; i++) { // not <= but only <
                for (int j = i + 1; j < len; j++) {  // start from i+1 and go upto last element
                    if (arr[i].equals(arr[j])) { // use equals()
                        System.out.println("Duplicate " + arr[i]);
                    }
                }
            }
        }
    

    O/p:

    Size 6
    Duplicate lady
    Duplicate bird
    
  4. # 4 楼答案

    第一个字符串将与.equals()而不是==进行比较

    if(arr[i].equals(arr[j]))
    {
       System.out.println("Duplicate "+arr[i]);
    }
    

    我建议您使用sets,因为它们不允许在其中输入重复的值,或者使用列表检查它是否已经存在。contains()方法

    List<String> list = new ArrayList();
    
    for(int i = 0; i < arr.length; i++)
       if(list.contains(arr[i])
         System.out.println("Duplicate" + arr[i]);
       else
         list.add(arr[i]); 
    
  5. # 5 楼答案

        String arr[] ={"lady", "bird", "is","bird","lady","cook"};
        Map<String, Integer> map = new HashMap<>();
        for(String str: arr) {
            if(map.containsKey(str)) {
                map.put(str, map.get(str)+1);
            } else{
                map.put(str, 1);
            }
        }
        for(String str: map.keySet()) {
            if(map.get(str) > 1) {
                System.out.println("Duplicate: "+ str+" count:"+map.get(str));
            }
        }
    

    输出:

    Duplicate: bird count:2
    Duplicate: lady count:2
    
  6. # 6 楼答案

    您可以使用以下更简单的方法

    Set<String> strings=new Hashset<String>();
    for(int i=0;i<len;i++){
        if(strings.add(arr[i])==false){
            System.out.println(arr[i]+" is a duplicate");
        }
    }
    

    在您现有的代码中,像arr[i].equals(arr[j])一样查看值是否相等(如果相等,则重复)

    ==检查引用等价性,而equals()方法检查值等价性,所以当需要检查两个对象的等价性时,应该使用equals方法

    希望有帮助

    祝你好运