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?

Rifat
  • 1,700
  • 3
  • 20
  • 51
  • You want the index of an element, or an element from a index? – SteelToe Mar 26 '18 at 03:18
  • Even if there would be no such method still you could write own one (which you should learn on algorithms basics) – Selvin Mar 26 '18 at 03:21
  • index with its value. – Rifat Mar 26 '18 at 03:21
  • 2
    Possible duplicate of [How to find the index of an element in an array in Java?](https://stackoverflow.com/questions/1522108/how-to-find-the-index-of-an-element-in-an-array-in-java) – Selvin Mar 26 '18 at 03:28

4 Answers4

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;
    }
}
Tung Tran
  • 2,885
  • 2
  • 17
  • 24
  • 2
    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. – Jakob Probst Mar 26 '18 at 06:32
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);

Nicolas
  • 8,077
  • 4
  • 21
  • 51
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");
Jitindra Fartiyal
  • 107
  • 1
  • 1
  • 5
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);
shmosel
  • 49,289
  • 6
  • 73
  • 138