How would I be able to access specific strings with and array list?

List<String> myList = new ArrayList<String>();
myList.Add("Hi");
myList.Add("Pizza");
myList.Add("Can");

So if I did that and then I do:

for (String s : myList)
    if (s == "Hi")
        system.out.println("Hello"):

It would not print "Hello".

I need the size to change and see if the string exists but I can't seem to get this to work, any ideas?

share|improve this question
1  
You can't use == to test non-primitive objects for equality, generally. Override Object.equals() meaningfully and use it. – Victor Sorokin 48 mins ago
1  
seems you are just using the loop for checking whether "Hi" is present or not. So for that list.contains("Hi") can also be used. – Narendra Pathai 45 mins ago
feedback

7 Answers

- Objects in Java are compared using equals(), and String is an Object in Java, so it follows the same rule.

- == will be used to compare primitive or to check if two or more Object Reference Variables are pointing to the same Object on the heap or not.

- So you should use equals() or equalsIgnoreCase() (if case doesn't matters) to compare the String Objects.

for (String s : myList){
    if (s.equals("Hi"))
        system.out.println("Hello");
}
share|improve this answer
Please refresh the answer before reading it........ – Kumar Vivek Mitra 46 mins ago
feedback
 if (s == "Hi")

change it to

 if (s.equals("Hi"))

It is always better to use equals() while comparing objects than using == (except String literals)

== compares reference equality. equals() compares object content equality.

share|improve this answer
feedback

You should write below code

for (String s : myList)
    if (s.equals("Hi"))
        system.out.println("Hello"):
share|improve this answer
feedback

You could use s.equalsIgnoreCase("Hi") if you do not care about upper-/lowercase

share|improve this answer
but s.equals("Hi") is more than enough. – vels4j 44 mins ago
feedback

You should use .equals() method it will check the value of the string while == checke Object reference.

share|improve this answer
feedback

Method name is add(object) not `Add(). Here is an another answer

        List<String> myList = new ArrayList<String>();
        myList.add("Hi");
        myList.add("Pizza");
        myList.add("Can");
        // method 1
        if( myList.contains("Hi")) {
            System.out.println("Found");
        }
        // method 2
        for( String word : myList ) {
            if( word.equals("Hi")) {
                System.out.println("found");
                // break the loop to avoid continues iteration
                break;
            }
        }
share|improve this answer
feedback

Why not use List.contains()?

if (myList.contains("Hi"))
    system.out.println("Hello");
share|improve this answer
feedback

Your Answer

 
or
required, but never shown
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.