简体   繁体   English

打印字符串中字符的位置

[英]Printing the position of character in a String

i have a method that prints the index of given character in a given String. 我有一种方法可以打印给定字符串中给定字符的索引。 If String doesn't contain given character the result is -1. 如果String不包含给定字符,则结果为-1。 My code looks like this: 我的代码如下所示:

public static int strpos(String text, char z) {

    int x = 0;

    char[] array = text.toCharArray();
    for (int i = 0; i < array.length; i++) {
        if (array[i] == z) {
            x = i;
            break;
        } else {
            x = -1;
        }
    }
    return x;
}

public static void main(String[] args) {
    System.out.println(strpos("abcdefghc", 'c'));
}

In this situation the result is 2 , and if i delete break , the result is 8 . 在这种情况下,结果为2 ,如果我删除break ,则结果为8 How can i modyfi my code to get a result of both 2 and 8? 如何修改我的代码以同时得到2和8的结果?

You can't get the result of both with a single return. 您无法一次获得两个结果。
You can, however, return an ArrayList<Integer> . 但是,您可以返回ArrayList<Integer>
You'll need to: 您需要:

  • Initialize the list at the head of the function. 初始化函数顶部的列表。
  • Each time you find a number, instead of breaking, add it your list. 每次您找到一个号码(而不是破坏号码)时,都addadd到列表中。
  • When you finish the search, return the list. 完成搜索后,返回列表。

That's how you can return multiple values easily. 这样便可以轻松地返回多个值。

Also, why is the else there? 另外,为什么还有其他?

One solution would be to store the indices in a List<Integer> and change the method to return it: 一种解决方案是将索引存储在List<Integer>并更改方法以将其返回:

public static List<Integer> strpos(String text, char z) {
    List<Integer> indices = new ArrayList<>();

    char[] array = text.toCharArray();

    for (int i = 0; i < array.length; i++) {
        if (array[i] != z) {
            continue;
        }

        list.add(i);
    }

    return indices.isEmpty() ? Arrays.asList(-1) : indices;
}

You can change the return type to List<Integer> and collect all the matching indexes in a list (or another collection of your choice). 您可以将返回类型更改为List<Integer>并将所有匹配的索引收集到一个列表(或您选择的另一个集合)中。 And in that case, instead of using -1 as a special value to indicate that a character doesn't exist in the string, return empty list instead, naturally. 在这种情况下,不要使用-1作为特殊值来指示字符串中不存在字符,而是自然返回空列表。

Here's one way to implement that: 这是一种实现方法:

List<Integer> strpos(String text, char z) {
    return IntStream.range(0, text.length())
            .filter(i -> text.charAt(i) == z)
            .boxed()
            .collect(Collectors.toList());
}

Alternatively, without Java 8 streams, but benefitting from .indexOf : 或者,不使用Java 8流,但可以从.indexOf受益:

List<Integer> strpos(String text, char z) {
    List<Integer> indexes = new ArrayList<>();
    int index = -1;
    while (true) {
        index = text.indexOf(z, index + 1);
        if (index == -1) {
            break;
        }
        indexes.add(index);
    }
    return indexes;
}

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

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