1

Okay so I have a LinkedList, and I have a String. I want to check if the String is contained within any of the LinkedList elements. For example:

String a = "apple";
String listelement = "a bunch of apples";
LinkedList list = new LinkedList();
list.add(listelement);
if(list.containsany(a){
 System.out.println("Hooray!");
}

Would result in printing "Hooray!"

Obviously list.containsany isn't a real LinkedList method, I'm just using it for this purpose.

So how can I simulate my example?

Thanks

asked Oct 19, 2011 at 22:33

4 Answers 4

9
String a = "apple";
String listelement = "a bunch of apples";
List<String> list = new LinkedList<String>();
list.add(listelement);
for(String s : list){
 if(s.contains(a)){
 syso("yes");
 }
}

This should do it, in order to find a node contains a particular string, you need to iterate through all the nodes. You can break the loop, if you want only 1 instance.

Also you want to use Generics. Look at the code. otherwise you will have to cast the node to a String.

answered Oct 19, 2011 at 22:35
Sign up to request clarification or add additional context in comments.

3 Comments

Does this work if the linked list has Strings and Objects in it?
this one works only for string. if you are using object, you need to cast it. and you cant store an object to a LinkedList<String>
Correct, now that you've added generics to the type of the list. :)
1
String a = "apple";
 String listelement = "a bunch of apples";
 LinkedList<String> list = new LinkedList<String>();
 list.add(listelement);
 Iterator<String> li = list.iterator();
 while (li.hasNext()) {
 if (li.next().contains(a)) {
 System.out.println("Hooray!");
 } 
 }
answered Oct 19, 2011 at 22:39

Comments

0
answered Oct 19, 2011 at 22:36

1 Comment

contains do full match of the node. not partial match of a string.
0

You would have to iterate across the list, and check each node's value to see if it was a string. If you can guarantee that all members of the linked list should be strings, using Java's Generics to force them all to be Strings may help.

 /*
 * 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);
 }
 }
 }
}
answered Oct 19, 2011 at 22:42

2 Comments

This is perfect! One question though, the "for" statement does not perform the "else" if the LinkedList contains no elements. Is this something that I can fix?
Sure, just surround it in an if statement that checks if the size of the list is 0, and in that case print out something appropriate. (If you need help with this, Javadocs are your friend)

Your Answer

Draft saved
Draft discarded

Sign up or log in

Sign up using Google
Sign up using Email and Password

Post as a guest

Required, but never shown

Post as a guest

Required, but never shown

By clicking "Post Your Answer", you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.