Java 如何在某个字符串之前从数组列表中删除所有名称?
在数组列表中,如果您添加一个字符串,然后对其进行排序,然后要删除该字符串之前的所有内容,您会怎么做?遍历该列表并不断删除元素,直到找到该字符串为止Java 如何在某个字符串之前从数组列表中删除所有名称?,java,collections,arraylist,Java,Collections,Arraylist,在数组列表中,如果您添加一个字符串,然后对其进行排序,然后要删除该字符串之前的所有内容,您会怎么做?遍历该列表并不断删除元素,直到找到该字符串为止 for (int i=0; i<list.size(); i++) { if (!(list.get(i).equal("String"))) { list.remove(0); } } for(inti=0;i您应该能够通过使用和来实现这一点 考虑阅读链接文档以了解更多信息 代码示例: list.subList(0, lis
for (int i=0; i<list.size(); i++) {
if (!(list.get(i).equal("String"))) {
list.remove(0);
}
}
for(inti=0;i您应该能够通过使用和来实现这一点
考虑阅读链接文档以了解更多信息
代码示例:
list.subList(0, list.indexOf(searched_string)).clear();
获取新添加元素的索引。使用此索引获取子列表,并将子列表传递给removeAll
方法:
删除
//addition = newly added String
list.removeAll(new ArrayList<String>(list.subList(0,list.indexOf(addition))));
//addition=新添加的字符串
removeAll(新的ArrayList(list.subList(0,list.indexOf(addition)));
完整示例
public class ArrayListSortDelete {
public static void main(String[] args) {
String addition = "BB";
List<String> list = new ArrayList(Arrays.asList(new String[] { "A", "B", "C", "D" }));
list.add(addition);
Collections.sort(list);
System.out.println("Before");
for (String s : list) {
System.out.println(s);
}
//new ArrayList<String>() avoids concurrent modification.
list.removeAll(new ArrayList<String>(list.subList(0,list.indexOf(addition))));
System.out.println("After:");
for (String s : list) {
System.out.println(s);
}
}
}
公共类ArrayListSortDelete{
公共静态void main(字符串[]args){
字符串加法=“BB”;
List List=newarraylist(Arrays.asList(新字符串[]{“A”、“B”、“C”、“D”}));
列表。添加(添加);
集合。排序(列表);
系统输出打印项次(“之前”);
用于(字符串s:列表){
系统输出打印项次;
}
//新建ArrayList()可避免并发修改。
removeAll(新的ArrayList(list.subList(0,list.indexOf(addition)));
System.out.println(“之后:”);
用于(字符串s:列表){
系统输出打印项次;
}
}
}
这应该没问题
ArrayList<String> yourList;
String query;
int index = yourList.indexOf(query);
for (int a=0; a<index; a++) {
yourList.remove(0);
}
arraylistyourlist;
字符串查询;
int index=yourList.indexOf(查询);
对于(inta=0;a虽然凯文·鲍沃斯已经给出了答案,但我在这里给你一个选择:)
另一种解释问题的方法是:
给定一个数组列表和一个新元素,删除数组列表中小于新元素的所有元素,然后将新元素放入列表并对列表进行排序
这里有一个方法:
ArrayList<String> oList = .....;
List<String> newList = new LinkedList<String>(); // tmp list to store the result
String newElement = ...;
for (String s: oList) {
if (s.compareTo(newElement) >= 0) { // s >= newElement
newList.add(s);
}
}
newList.add(newElement);
// put the result back to oList
oList.clear();
oList.putAll(newList);
// sort the result
Collections.sort(oList);
// now oList contains what you want
ArrayList-oList=。。。。。;
List newList=newlinkedlist();//用于存储结果的tmp列表
字符串newElement=。。。;
for(字符串s:oList){
如果(s.compareTo(newElement)>=0{//s>=newElement
新列表。添加(s);
}
}
newList.add(新元素);
//把结果放回奥利斯特
oList.clear();
oList.putAll(新列表);
//排序结果
集合。排序(oList);
//现在,oList包含您想要的内容
您甚至可以在oList中执行此操作,而无需临时列表的帮助(但删除arrayList并不高效).如果字符串是唯一的,您可以使用树集
。这属于经典的字符串比较错误。将字符串与等于,而不是=
进行比较。如果它没有输入错误,并且实际上是最佳解决方案,则可以接受(使用等于(对象)
而不是=
进行字符串比较)从外观上看,您的想法是正确的。只需修复错误。您有错误:您使用了类名而不是对象名。您使用了错误的方式来比较字符串。@user3196301很高兴我能提供帮助。可能最好使用集合。binarySearch
而不是indexOf
,因为列表
已排序,这将是m“对于大型列表,速度会更快。@BoristheSpider感谢您建议进行二进制搜索,我今晚将进行调查。