我是java新手。正在尝试查找字符串中的第一个重复字符。我不知道它错在哪里。下面是我的代码

我是java新手。正在尝试查找字符串中的第一个重复字符。我不知道它错在哪里。下面是我的代码,java,indexoutofboundsexception,Java,Indexoutofboundsexception,} 输入字符串 ABCD 线程主java.lang.ArrayIndexOutOfBoundsException中的异常: 65 at strs.findtherepeatecharstrs.java:18 at strs.mainstrs.java:12 提前感谢您一些小的更新—阵列作为最重要的一个就足够了: public static void main(String[] args) throws IOException { InputStreamReader in=new Inpu

}

输入字符串

ABCD

线程主java.lang.ArrayIndexOutOfBoundsException中的异常: 65 at strs.findtherepeatecharstrs.java:18 at strs.mainstrs.java:12


提前感谢您

一些小的更新—阵列作为最重要的一个就足够了:

public static void main(String[] args) throws IOException {
    InputStreamReader in=new InputStreamReader(System.in);
    BufferedReader br=new BufferedReader(in);
    System.out.println("Enter the string");
    String s=br.readLine();
    char[] ch=s.toCharArray();
    findtherepeatechar(ch);
}
private static char findtherepeatechar(char[] ch) {
    int i;
    int count[]= {0};
    for( i=0;i<ch.length;i++) {
        count[ch[i]]++;
    }
    for(i=0;i<ch.length;i++)
        if(count[ch[i]]>1) {
            return ch[i];
        }else {
            return '\0';
        }
    return 0;
}

由于ch[i]值的范围为0到255,因此。。。不是100%正确您需要长度为256的数组。您还希望在main方法的末尾添加System.out.println。

代码中有一些问题

首先,不影响编译器,但影响我们的眼睛和您的代码的清晰度,看看 将camel case用于lisibility,findTheRepeatedChar代替findTheRepeatedChar

同时也要考虑 int[]count={0}将定义一个固定大小为1的数组,因此当您尝试访问更大的索引时,它将引发异常

因此,您最好定义一个更大的数组int bigArray=new int[bigNumber],或者根据您的应用程序使用另一种结构,如列表或映射。

您可以尝试此方法

public static void main(String[] args) throws IOException {
    InputStreamReader in = new InputStreamReader(System.in);
    BufferedReader br = new BufferedReader(in);
    System.out.println("Enter the string");
    String s = br.readLine();
    char[] ch = s.toCharArray();
    System.out.println(findtherepeatechar(ch));
}

private static char findtherepeatechar(char[] ch) {
    int i;
    int count[] = new int[256];
    for (i = 0; i < ch.length; i++) {
        count[ch[i]]++;
        if (count[ch[i]] > 1) {
            return ch[i];
        }
    }
    return 0;
}
你为什么不用a和a呢


我对代码进行了注释,以使您理解其逻辑。

逻辑非常简单。如果我们没有看到字符串中的字符,我们将其放入哈希表中,否则我们将打印它

private static void findTheRepeatedChar(char[] characters) {
    /*
     * logic: char are inserted as keys and their count as values. If map
     * contains the char already then increase the value by 1
     */
    Map<Character, Integer> map = new HashMap<Character, Integer>();
    for (Character character : characters) {
        if (map.containsKey(character)) {
            map.put(character, map.get(character) + 1);
        } else {
            map.put(character, 1);
        }
    }
    // Obtaining set of keys
    Set<Character> keys = map.keySet();
    /*
     * Display count of chars if it is greater than 1. All duplicate chars
     * would be having value greater than 1.
     */
    for (Character ch : keys) {
        if (map.get(ch) > 1) {
            System.out.println("Char " + ch + " " + map.get(ch));
        }
    }
}

调试器会很快告诉您。那个计数数组显然是不对的。您似乎希望每个字母都有一个计数,但这不是您在方法开始时分配的。另外,请注意IndexOutOfBoundsException意味着您已超出数组。将int count={0}更改为int count[]=new int[1000];
private static void findTheRepeatedChar(char[] characters) {
    /*
     * logic: char are inserted as keys and their count as values. If map
     * contains the char already then increase the value by 1
     */
    Map<Character, Integer> map = new HashMap<Character, Integer>();
    for (Character character : characters) {
        if (map.containsKey(character)) {
            map.put(character, map.get(character) + 1);
        } else {
            map.put(character, 1);
        }
    }
    // Obtaining set of keys
    Set<Character> keys = map.keySet();
    /*
     * Display count of chars if it is greater than 1. All duplicate chars
     * would be having value greater than 1.
     */
    for (Character ch : keys) {
        if (map.get(ch) > 1) {
            System.out.println("Char " + ch + " " + map.get(ch));
        }
    }
}
 public static void main(String[] args) {
    String[] dummyList= {"ABCA", "BCABA", "ABC", "DBCABA"};
    for (String s : dummyList) {
        System.out.println("Result=" + findIfStringHasRecurringChar(s));
    }
 }

 private static Character findIfStringHasRecurringChar(String s) {
    Hashtable<Character, Integer> letters = new Hashtable<>();
    for (char c : s.toCharArray()) {
        if (!letters.containsKey(c)) 
            letters.put(c, 1);
        else 
            return c;      
    }
    return null;
 }
Result=A
Result=B
Result=null
Result=B