Warning: file_get_contents(/data/phpspider/zhask/data//catemap/7/css/33.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 字符串indexOf方法对字符串中的第一个字符无效_Java_String_Indexof - Fatal编程技术网

Java 字符串indexOf方法对字符串中的第一个字符无效

Java 字符串indexOf方法对字符串中的第一个字符无效,java,string,indexof,Java,String,Indexof,我正在实现一个代码,用于计算字符串中所有字符的出现次数。我使用了indexOf()方法来检查事件。但这对第一个角色不起作用。 以下代码适用于除第一个字符外的所有字符 public static void main(String[] args) { String str = "simultaneously"; int[] count = new int[str.length()]; //counter array for (int i = 0; i

我正在实现一个代码,用于计算字符串中所有字符的出现次数。我使用了indexOf()方法来检查事件。但这对第一个角色不起作用。 以下代码适用于除第一个字符外的所有字符

public static void main(String[] args) {
        String str = "simultaneously";
        int[] count = new int[str.length()]; //counter array

        for (int i = 0; i < count.length; i++) { //initializing counters
            count[i] = 0;
        }

        for (int i = 0; i < str.length(); i++) {
            int index = -1;
            char c = str.charAt(i);
            while (1 > 0) {

                if (str.indexOf(c,index) > 0) {  //This is not working for 
                                                 //the first characters
                    index = str.indexOf(c, index) + 1;
                    count[i]++;
                } else {
                    break;
                }
            }
        }

        for (int i = 0; i < count.length; i++) {
            System.out.println(str.charAt(i) + " occurs " + count[i] + " times");
        }
    }
publicstaticvoidmain(字符串[]args){
String str=“同时”;
int[]count=new int[str.length()];//计数器数组
对于(int i=0;i0){
如果(str.indexOf(c,index)>0){//这对
//第一个字符
索引=str.indexOf(c,索引)+1;
计数[i]++;
}否则{
打破
}
}
}
for(int i=0;i
str.indexOf(c,index)
将为第一个字符返回
0
,但您的条件检查str.indexOf(c,index)>0
。将其更改为
str.indexOf(c,index)>=0

java中数组的索引从0开始。 将条件从

if (str.indexOf(c,index) > 0) {


此外,用于初始化计数器的for循环是冗余的,默认情况下,int数组中的所有值都初始化为0

还有一些事情你需要知道

str.indexOf(c,index)
将从索引“index”中搜索字符c,在您的示例中,该索引为-1,对于第一个字符,它将永远不会找到,因为字符串的起点是0 也可以按以下方式改变您的状况

str.indexOf(c,index) >= 0

在Java中,数组和字符串(即字符数组)的索引始终从0开始

您还需要检查位置0,因此包括>=

       if (str.indexOf(c,index) >= 0) {
此外,使用休息有时会令人困惑。 在代码中,while循环是一个无限的True,然后在必要时中断它

请看下面的代码。它与你想要达到的目的是一样的。 它变得越来越清晰,因为它消除了whileloop中的中断,您只需在while循环的开头检查语句是否正确,而不是在它内部

        for (int i = 0; i < str.length(); i++) {
            int index = 0;
            char c = str.charAt(i);
            while (str.indexOf(c,index) >= 0) {
                index = str.indexOf(c, index) + 1;
                count[i]++;
            }
        }
for(int i=0;i=0){
索引=str.indexOf(c,索引)+1;
计数[i]++;
}
}

while(1>0)
条件更好,因为
while(true)
无需将数组元素初始化为零:Java根据规范将新数组的元素初始化为默认值,int的默认值为零。
        for (int i = 0; i < str.length(); i++) {
            int index = 0;
            char c = str.charAt(i);
            while (str.indexOf(c,index) >= 0) {
                index = str.indexOf(c, index) + 1;
                count[i]++;
            }
        }