您必须遍历列表,并检查每个节点的值以查看它是否为字符串。如果您可以保证链表的所有成员都应该是字符串,那么使用 Java 的泛型将它们全部强制为字符串可能会有所帮助。
/*
* To change this template, choose Tools | Templates
* and open the template in the editor.
*/
package javaapplication1;
import java.util.LinkedList;
public class JavaApplication1 {
/**
* @param args the command line arguments
*/
public static void main(String[] args) {
String a = "apple";
String listelement = "a bunch of apples";
LinkedList<String> list = new LinkedList<String>();
list.add(listelement);
list.add(new String("boogie"));
for (String s : list) {
if (s.contains(a)) {
System.out.println("yes," + s + " contains " + a);
} else {
System.out.println("no," + s + " does not contain " + a);
}
}
}
}