-3

What is the diffrence of between this code:

{
...
         int counter = 0;
    for (String item : Names)
    {
     int i = item.indexOf("a");
     counter = i;
    }
    return counter;
...
}

and this :

{
...
                int counter = 0;
        for (String item : Names)
        {
         if(item == "a")
                   return counter;
                 else
                   counter++;
        }
        return null ;
...
}

In facti, How to work the indexOf method in java? Thanks.

Everv0id
  • 1,862
  • 3
  • 25
  • 47
oop12
  • 17
  • 5
  • 2
    What part of the [Javadoc for `indexOf()`](https://docs.oracle.com/javase/7/docs/api/java/util/List.html#indexOf%28java.lang.Object%29) do you find confusing? Also, `item == "a"` will never produce the desired result, as that's not [how string comparisons are done](http://stackoverflow.com/questions/767372/java-string-equals-versus) in Java. – MH. Apr 12 '15 at 09:05

5 Answers5

0

In your first code, if your string contain "a" then it will return index of "a". In your second code if you are checking string == "a".

indexOf() Property of String is use to get index of specific character in String. Once you get index you can create substring using that index.

Prashant Bhoir
  • 900
  • 6
  • 8
0

You can find the documentation of indexOf here:

Returns the index within this string of the first occurrence of the specified character

Your first piece of code checks at which index a occurs in the last string of the list (eg [a,ab,abc,bcaba] -> 2, because a is at position 2 in bcaba; [a,b,c] -> -1, because c doesn't contain a).

Your second piece of code checks at which position in a list a is stored (eg [aa,cc,bb,a,b,c] -> 3).

But do note that you use reference equality in your second code, when you should be using equals, and thus your method will most likely return null, which might also explain that it doesn't work for you.

tim
  • 1,999
  • 17
  • 32
0

The first snippet returns the index of the first occurrence of character a in string item and then exits.

The second snippet looks through names and returns the index of a string exactly matching "a" and then exits.

indexOf simply returns the first occurence of a character or substring within a string.

http://docs.oracle.com/javase/7/docs/api/java/lang/String.html

0

The main difference is that you are testing different conditions: in one, the presence of "a" inside the string; in the second, the identity of the item with the literal "a".

The next difference is that the first version pointlessly continues the search even after a match is found.

user207421
  • 305,947
  • 44
  • 307
  • 483
0

There are many differences in your snippets of code. In the first case you are using an enhanced for loop to scan all elements of Names array (usually the variable name don't begin with a capital letter), for each alement you are trying to get the index of the the first occorrence of a string. For example if you have the following situation:

String [] Names = {"house","main","method"}

in this three string the character a is present just in main word, and then you program will return 1, the position of a inside the word main. In the second case for each element you are trying to confront the references of item and a. I want to remember that the check for equality about string is made using equals and not ==. Anyway in the second case you are lokking for a string a inside your array. If you find it you return the position about this string otherwhise you get numm

Skizzo
  • 2,883
  • 8
  • 52
  • 99