static int [] indexOfArray(char character, String word){ java.util.ArrayList<Integer> arrayList = new java.util.ArrayList<Integergt;(); // loop through word to find the occurrence of character for(int i = 0 ; i < word.length() ; i++){ if( word.charAt(i) == character){ arrayList.add(i); // if character found add i to arrayList } } int [] array = new int[arrayList.size()]; int i =0; // add all elements in arrayList to int array for(int x : arrayList){ array[i] = arrayList.get(i); i++; } return array; } static void printAll(int array[]){ for(int i =0; i < array.length ; i++){ System.out.println(array[i]); } }
Wednesday, June 22, 2011
indexOf character in word - return all indices of occurance - java
Subscribe to:
Post Comments (Atom)
No comments:
Post a Comment