[英]startsWith using error in java
defined variable:定义变量:
LinkedList list1=new LinkedList();
Object get()
in list1 obtains a node of list1 list1 中的
Object get()
获取 list1 的一个节点
Object remove()
in list1 deletes a node of list1 list1 中的
Object remove()
删除 list1 的一个节点
count()
is length of list1 count()
是 list1 的长度
for(int i=1;i<list1.count();i++){
if(list1.get(i).startsWith('"',0)) //Error here
list1.remove(i);
}
Error: cannot find symbol错误:找不到符号
symbol: method charAt(int)
符号:方法 charAt(int)
location: class Object位置:类对象
how to fix this problem?如何解决这个问题? I would like to delete the node in list1 which starts with (").
我想删除 list1 中以 (") 开头的节点。
startsWith
is a method in the String
class; startsWith
是String
类中的一个方法; you are using the raw LinkedList type, and thus it is treated like a LinkedList<Object>
.LinkedList<Object>
。 If you wish to use Strings, make it a LinkedList<String>
.LinkedList<String>
。startsWith
accepts only String
arguments, not char
arguments. startsWith
只接受String
参数,不接受char
参数。 Use startsWith("\\"")
instead.startsWith("\\"")
代替。startsWith
is superfluous;startsWith
的第二个参数是多余的; providing no second parameter will assume starting position as 0.if
statement.if
语句之后有一个无关的分号。 This will cause the if
body to be treated as empty.if
主体被视为空。 Definitely remove this semicolon and optionally use curly braces. Your modified solution may look something like this:您修改后的解决方案可能如下所示:
LinkedList<String> list1 = new LinkedList<String>();
// [...] Populate the list accordingly here
for(int i=1; i < list1.count(); i++){
if (list1.get(i).startsWith("\"")) {
list1.remove(i);
}
}
Additional notes:补充说明:
For example:例如:
[ "a", "b", "c", "d" ]
^
(remove element at index 0)
[ "b", "c", "d" ]
^
(remove element at index 1... uh oh, we missed "b"!)
[ "b", "d" ]
^
(remove element at index 2... ERROR; index out of bounds)
If it's a list of strings, you need to tell the compiler so.如果它是一个字符串列表,则需要告诉编译器。 Java does not duck-type, you have to tell the compiler explicitly that the Object is a String.
Java 没有鸭子类型,您必须明确告诉编译器 Object 是一个字符串。
List<String> list1=new LinkedList<String>();
When it's declared that way, get(i)
will return a reference of type String and you can use its methods.当以这种方式声明时,
get(i)
将返回一个 String 类型的引用,您可以使用它的方法。
If you don't specify the list type, Java will treat is as a list of Object
s, and there is no Object.charAt()
method.如果不指定列表类型,Java 会将 is 视为
Object
的列表,并且没有Object.charAt()
方法。 Add the <String>
type:添加
<String>
类型:
LinkedList<String> list1 = new LinkedList<String>();
Or, absent that, add a cast to the get
call:或者,如果没有,向
get
调用添加一个演员表:
if (((String) list1.get(i)).startsWith("\\"", 0))
The String.startsWith()
method takes a String argument, not a char. String.startsWith()
方法接受一个字符串参数,而不是一个字符。
if (list1.get(i).startsWith("\\""))
List indices are 0-based.列表索引是从 0 开始的。
for (int i = 0; i < list1.count(); i++) ^
You shouldn't use get(int)
with a LinkedList.您不应该将
get(int)
与 LinkedList 一起使用。 This method is for random access .此方法用于随机访问。 Linked lists can be traversed in order quickly, but random access is slow.
链表可以快速按顺序遍历,但随机访问速度较慢。 You should either iterate over the list sequentially, or use an ArrayList which does support fast random access.
您应该按顺序遍历列表,或者使用支持快速随机访问的ArrayList 。
for (String str: list1) { } // or List<String> list1 = new ArrayList<String>();
Removing elements from a list while you're iterating over it is tricky.在迭代列表时从列表中删除元素很棘手。 The best way to do it is to use an iterator.
最好的方法是使用迭代器。
Iterator<String> it = list1.iterator(); while (it.hasNext()) { String str = it.next(); if (str.startsWith("\\"")) { it.remove(); } }
you are trying to get startWith on Object , typecast as String.您正在尝试在 Object 上使用 startWith,将其类型转换为 String。
if(((String)list1.get(i)).startsWith('"',0)) if(((String)list1.get(i)).startsWith('"',0))
if(list1.get(i).startsWith("\\"",0))
List<String> itemsToBeRemoved = new ArrayList<String>();
for(int i=1;i<list1.count();i++){
String str = list1.get(i);
if(str.startsWith("\""))
itemsToBeRemoved.add(str);
}
list1.removeAll(itemsToBeRemoved);
You should specify the object types that your LinkedList will hold or perform a cast when trying to call the charAt
method.在尝试调用
charAt
方法时,您应该指定 LinkedList 将保存的对象类型或执行charAt
。 Something like:就像是:
LinkedList<String> list1=new LinkedList<String>();
Or casting to String
或转换为
String
if (((String) list1.get(i)).startsWith("\""))
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.