//题目要的是下标,不是个数
public class Test {
public static void main(String args[]) {
String s = "123456781234568";
char c = '2';
System.out.println(test1(c, s));
System.out.println(test2(c, s));
System.out.println("count = " + test2(c, s).size());
}
static List<Integer> test1(char c, String s) {
List<Integer> result = new ArrayList<Integer>();
char[] sc = s.toCharArray();
for (int i = 0; i < sc.length; i++) {
if (c == sc[i]) {
result.add(i);
}
}
return result;
}
static List<Integer> test2(char c, String s) {
List<Integer> result = new ArrayList<Integer>();
int index = s.indexOf(c);
while (index != -1) {
result.add(index);
index = s.indexOf(c, index + 1);
}
return result;
}
}