2

If I have a array list say a string array, then how can I get index of an element with its value?

Code exammple:

    private static final String[] TEST = new String[]{
      "abc","bcd","def"
}

require its index with something like:

       int i = TEST.getPosition("abc");/TEST.indexof(...);//not available

Also I tried with creating ArrayAdapter.

Is there anything like this? Or maybe I need to assign it with that adapter or something?

Or,

How can I get such index of an element?

4
  • You want the index of an element, or an element from a index? Commented Mar 26, 2018 at 3:18
  • Even if there would be no such method still you could write own one (which you should learn on algorithms basics) Commented Mar 26, 2018 at 3:21
  • index with its value. Commented Mar 26, 2018 at 3:21
  • 2
    Possible duplicate of How to find the index of an element in an array in Java? Commented Mar 26, 2018 at 3:28

4 Answers 4

5

Simplest idea:

Convert array to list and you can get position of an element.

List<String> abcd  = Arrays.asList(yourArray);
int i = abcd.indexOf("abcd");

Other solution, you can iterator array:

int position = 0;
for (String obj : yourArray) {
    if (obj.equals("abcd") {
       return position;
    }
    position += 1;
} 

//OR
for (int i = 0; i < yourArray.length; i++) {
    if (obj.equals("abcd") {
       return i;
    }
}
Sign up to request clarification or add additional context in comments.

1 Comment

I‘d rather use a normal for-loop than a foreach-loop like you did for this example, you do not have to worry about any external variable and it’s more compact.
1

You can refer to this question and use the Java 8 Lambda filter function.

Optional<String> optional = Arrays.stream(presents)
                               .filter(x -> "MyString".equals(x.getName()))
                               .findFirst();

if(optional.isPresent()) {//Check whether optional has element you are looking for
    String s = optional.get();//get it from optional
}

the filter function take a lambda callback and will returns a list with only the values that match the condition in the lambda. In our case, we also want only the first one. That's why are add the findFirst() which will returns only the first value.
The function will returns an Optional, we can check if it contains a value using options.isPresent().
The optional.get() will returns the value.
Replace the "MyString" by your value and it should work.

If you wish to find an index, you can use the indexOf() function after changing your static array to an list : Arrays.asList(array);

Comments

1

There can be two methods which you can opt for -

1) First, as it is an array, you can simply run a for loop and compare using equals() and find the position.

2) Secondly, convert it into an List<> and run indexOf() and find the position.

NOTE: When you convert an array to List, it become a backed version List, it is fixed and size can't be alter --> but it is perfectly fine in your case as your array is final.

List<String> str = Arrays.asList(TEST);
int pos = str.indexOf("abc");

Comments

1

As others have said, the simplest method is Arrays.asList(TEST).indexOf("abc"). But here's an alternative using streams:

int index = IntStream.range(0, TEST.length)
        .filter(i -> TEST[i].equals("abc"))
        .findFirst()
        .orElse(-1);

Comments

Your Answer

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.