简体   繁体   English

如何在Java中返回字符串中字符的索引数组

[英]How to return the index array of a character in a string in Java

I need to write a method to return the index array of a character in a string in Java. 我需要编写一个方法来返回Java中字符串中字符的索引数组。 Is the following good (correctness, efficiency, as short code as possible) enough? 下面的好(正确性,效率,尽可能短代码)是否足够?

int[] charIndexArray(String s, char c) {
    int start = 0;
    List<Integer> list = new ArrayList<Integer>();
    while ((start = s.indexOf(c, start)) != -1) {
        list.add(start);
        start++;
    }
    int arr[] = new int[list.size()];
    for (int i = 0; i < ret.length; i++)
        arr[i] = list.get(i);
    return arr;
}

You can replace the code at the end that copies it to an array with a call to the toArray() method . 您可以替换最后的代码,通过调用toArray()方法将其复制到数组。 Other than that, looks pretty good. 除此之外,看起来还不错。

Instead of: 代替:

while ((start = s.indexOf(c, start)) != -1) {
    list.add(start);
    start++;
}

consider: 考虑:

for (int i = 0; i < s.length(); i++) {
    if (s.charAt(i) == c) {
      list.add(i);
    }
 }

because the indexOf causes the creation of a whole other loop to search for the next instance of your character. 因为indexOf会导致创建一个完整的其他循环来搜索角色的下一个实例。

You code is quietly doing: 你的代码正在悄悄地做:

while (start != -1) {
    start = -1;
    for ( int i=start;i<s.length();i++){
      if ( charAt(i) == c ) {
        start = i;
        break;
      }
    }
    if ( start != -1 ) { 
    list.add(start);
    start++;
  }
}

Which does not seem more efficient. 哪个看起来效率不高。 But it turns out that after spending way too much time on this: 但事实证明,在花费太多时间之后:

static int[] charIndexArrayByBits(String s, char c) {
    int start = 0;
    int[] list = new int[s.length()];
    int count = -1;
    while ((start = s.indexOf(c, start)) != -1) {
      list[++count] = start;
      start++;
    }
    return Arrays.copyOf(list, count);
  }

is faster. 是比较快的。 But I would not consider it more efficient in the general case because you are allocating an int array which would be larger space wise. 但是我不认为它在一般情况下更有效,因为你正在分配一个空间更大的int数组。

The code do not look good. 代码看起来不太好。

You use two loop instead of one. 你使用两个循环而不是一个循环。

Try to use methods. 尝试使用方法。

charAt(int pos) for string and Arrays.copy charAt(int pos)表示字符串和Arrays.copy

The OP shold not read more ;p OP不会更多地阅读; p

The first is the location this kind of method should be placed in some util class and be static IMHO. 第一个是这种方法应放在某些util类中的位置,并且是静态的恕我直言。

public class CharSequenceUtil {

    private static int[] EMPTY_INT_ARRAY = new int[0];

    /**
    * Method search the position of given character in char sequence.
    *
    * @param CharSequence seq - Sequence of char that will be investigate 
    * @param char c - Character that is analysed.
    *
    * @return int array with positions of char c in CharSequence instanace
    * @throws NullPointerException if seq is null.
    */
    public static int[] charIndexArray(CharSequence seq, char c) {

      if(seq == null) {
        throw new NullPointerExcetion("The seq must not be null");
      }

      if(seq.length() == 0) {
        return EMPTY_INT_ARRAY;
      }

      int[] positions = new int[seq.lenth()];
      int stor = -1; 

      for(int pos = 0; pos < seq.length(); seq++) {
         if(c == seq.charAt(pos)) {
          positions[++stor] = pos;
         }
      }

      if(stor == -1) {
        return EMPTY_INT_ARRAY;
      }

      return Arrays.copyOf(positions, stor);
    }
}

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

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