简体   繁体   中英

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. 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 . 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.

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.

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

The OP shold not read more ;p

The first is the location this kind of method should be placed in some util class and be static IMHO.

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);
    }
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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