List<String> strings; // contains "foo", "bar", "baz", "xyz"
Run Code Online (Sandbox Code Playgroud)
如果给定输入,"baz"则函数重新排列(String输入)应该返回字符串
"baz", "foo", "bar", "xyz"
Run Code Online (Sandbox Code Playgroud)
如果给定输入,"bar"则函数重新排列(String输入)应该返回字符串
"bar", "foo", "baz", "xyz"
Run Code Online (Sandbox Code Playgroud)
sst*_*dal 18
首先,删除该项目,然后在位置1再次添加该项目.
List<String> strings;
List<String> rearrange(String input) {
strings.remove(input);
strings.add(0,input);
return strings;
}
Run Code Online (Sandbox Code Playgroud)
小智 7
public static <T> void setTopItem(List<T> t, int position){
t.add(0, t.remove(position));
}
Run Code Online (Sandbox Code Playgroud)
public static <T> List<T> rearrange(List<T> items, T input) {
int index = items.indexOf(input);
List<T> copy;
if (index >= 0) {
copy = new ArrayList<T>(items.size());
copy.add(items.get(index));
copy.addAll(items.subList(0, index));
copy.addAll(items.subList(index + 1, items.size()));
} else {
return items;
}
return copy;
}
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
12618 次 |
| 最近记录: |