简体   繁体   English

如何比较单个字符串中的元素?

[英]How to compare elements in a single string?

I need to print the indexes of the elements in a string of 1s and 0s if and only if the 1s and 0s repeat more than once side by side. 当且仅当1和0并排重复多次时,我需要以1和0的字符串打印元素的索引。

For example: input: "0010011" output: "0, 3, 5" 例如:输入:“0010011”输出:“0,3,5”

A repeat starts at indexes 0 , 3, and 5. 重复从索引0,3和5开始。

Here is what I currently have 这是我现在拥有的

Scanner keyboard = new Scanner(System.in);
    ArrayList runs = new ArrayList();


    System.out.println("Enter non empty string of 1s and 0s");
    String input = keyboard.nextLine();
    char[] array = input.toCharArray();

    for(int i = 0; i < input.length(); i++)
    {
        if(array[i] == array[i++])
        {
            runs.add(i);
        }

    }
    for(int i = 0; i<= array.length; i++)
    {
        System.out.println(runs);
    }

When I test this, I try input of "00100" and get an output of "1,3,5". 当我测试这个时,我尝试输入“00100”并获得“1,3,5”的输出。 Another test input from above "0010011" and get "1,3,5,7". 另一个测试输入来自“0010011”以上并获得“1,3,5,7”。 It seems to be printing odd numbers, not the indexes where numbers start to repeat. 它似乎是打印奇数,而不是数字开始重复的索引。 Can anyone spot what I am doing wrong? 谁能发现我做错了什么? I have a feeling its coming from my comparing in the first for loop. 我感觉它来自我在第一个for循环中的比较。

This should work: 这应该工作:

Scanner keyboard = new Scanner(System.in);

System.out.println("Enter non empty string of 1s and 0s");
String input = keyboard.nextLine();
char lastChar = '~'

for(int i=0; i<input.length() - 1; i++) {
    if(input.charAt(i) == input.charAt(i + 1) && (i == 0 || input.charAt(i) != input.charAt(i - 1))) {
        System.out.println(i);
    }
}

There are 3 immediate issues that I can point out: 我可以指出有三个直接的问题:

  • First is obvious. 首先是显而易见的 You are using i++ , while you should be using i + 1 . 您正在使用i++ ,而您应该使用i + 1 i++ will increment the value of i , so you're missing out an index there for next iteration. i++将增加的价值i ,所以你就错过了一个索引那里下一次迭代。
  • Second, you should iterate till input.length() - 2 , else you will get an ArrayIndexOutOfBounds exception, when you access array[i + 1] for the last index. 其次,你应该迭代到input.length() - 2 ,否则当你访问最后一个索引的array[i + 1]时,你会得到一个ArrayIndexOutOfBounds异常。
  • Your current logic will fail if you've more than 2 0's or 1's in sequence. 如果按顺序超过2 0's1's则当前逻辑将失败。 It will print both 0 and 1 index for say, 00010 . 它将打印01索引,例如00010

For the third point, what you should do is, once you find two consecutive characters same, you should skip upcoming character that are same. 对于第三点,你应该做的是,一旦你发现两个连续的字符相同,你应该跳过即将到来的相同角色。 You will need an inner loop here. 你需要一个内循环。 Probably a do-while . 可能是一段do-while

You should modify your for loop to this: 您应该将for循环修改for

for(int i = 0; i < input.length() - 1; i++)
{
    if(array[i] == array[i+1])
    {
        System.out.println(i);
        do {
            i++;
        } while (i < input.length() - 1 && array[i] == array[i + 1]);
    }
}

Also, ArrayList doesn't have a length attribute. 此外, ArrayList没有length属性。 You should use size() method to get maximum size. 您应该使用size()方法来获得最大大小。


BTW, this can also be done using regex. 顺便说一句,这也可以使用正则表达式来完成。 Well, you might not have been taught about this yet, but this is just for another possible way: 好吧,你可能还没有被教过这个,但这只是另一种可能的方式:

Pattern pattern = Pattern.compile("0{2,}|1{2,}");
Matcher matcher = pattern.matcher(input);

while (matcher.find()) {
    System.out.println(matcher.start());
}

Two problems there: 那里有两个问题:

First, when the postfix ++ return the value before take effect, so you are comparing array[i] with itself. 首先,当postfix ++在生效之前返回值时,所以你将array [i]与它自己进行比较。

Also, you are doing two plus 1 each loop, so there are only odd numbers. 另外,你每个循环都做两个加1,所以只有奇数。

Try this :- 尝试这个 :-

Scanner keyboard = new Scanner(System.in);
    ArrayList runs = new ArrayList();


    System.out.println("Enter non empty string of 1s and 0s");
    String input = keyboard.nextLine();
    char[] array = input.toCharArray();

    for(int i = 0; i < input.length()-1; i++)
    {
        if(array[i] == array[i+1])
        {
            runs.add(i);
        }

    }
    for(int i = 0; i<= array.length; i++)
    {
        System.out.println(runs);
    }

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM