简体   繁体   English

在java中使用错误startsWith

[英]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 中以 (") 开头的节点。

  1. startsWith is a method in the String class; startsWithString类中的一个方法; you are using the raw LinkedList type, and thus it is treated like a LinkedList<Object> .您使用的是原始 LinkedList 类型,因此它被视为LinkedList<Object> If you wish to use Strings, make it a LinkedList<String> .如果您希望使用字符串,请将其设为LinkedList<String>
  2. startsWith accepts only String arguments, not char arguments. startsWith只接受String参数,不接受char参数。 Use startsWith("\\"") instead.使用startsWith("\\"")代替。
  3. The second parameter you've supplied to startsWith is superfluous;您提供给startsWith的第二个参数是多余的; providing no second parameter will assume starting position as 0.不提供第二个参数将假定起始位置为 0。
  4. You have an extraneous semicolon after your 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:补充说明:

  1. Your for loop starts at index 1. Note that this will not remove over the first element.您的 for 循环从索引 1 开始。请注意,这不会删除第一个元素。 I'm sure if this is your desired behavior.我确定这是否是您想要的行为。
  2. As you remove elements in the list, the indices of the latter elements in the list will change.当您删除列表中的元素时,列表中后一个元素的索引将发生变化。

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 类型的引用,您可以使用它的方法。

  1. 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))
  2. The String.startsWith() method takes a String argument, not a char. String.startsWith()方法接受一个字符串参数,而不是一个字符。

     if (list1.get(i).startsWith("\\""))
  3. List indices are 0-based.列表索引是从 0 开始的。

     for (int i = 0; i < list1.count(); i++) ^
  4. 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>();
  5. 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.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM