翻到好久之前一个问题,然后咋做都不会了
part1
public static int[] getIndices(String[] sa, String s) {
int[] result = null;
int f = 0 ;
int count =0;
for (int i= 0;i < sa.length;i ++ ) {
if (sa[i].equals(s)) {
f=i;
count ++;
}
result = new int [count];
for(int j =0;j<count;j++) {
result[j]=f;
}
}
return result;
}
}
part2:@Test
public void test_getIndices_01() {
String[] input = {"d", "c", "a", "b", "c", "d", "a", "a", "d", "a"};
int[] result = Utilities.getIndices(input, "a");
int[] expected = {2, 6, 7, 9};
assertArrayEquals(expected, result);
}
@Test
public void test_getIndices_02() {
String[] input = {"d", "c", "a", "b", "c", "d", "a", "a", "d", "a"};
int[] result = Utilities.getIndices(input, "b");
int[] expected = {3};
assertArrayEquals(expected, result);
}
@Test
public void test_getIndices_03() {
String[] input = {"d", "c", "a", "b", "c", "d", "a", "a", "d", "a"};
int[] result = Utilities.getIndices(input, "e");
int[] expected = {};
assertArrayEquals(expected, result);
}
}
给出一个String []然后里面有几个字母;再输入一个string,若输入的string和String[]里的元素相同则输出result[] result的元素就是string在 String[] 所在的位置;
以上代码的part1 是我写的 part2 是junittest, 运行之后result里的元素只有string在String[] 最后出现的位置。
望大家可以找出问题并解答
不胜感激