3

I'm looking for a way to find the index position of arrays and i want to store that value in String but how can we do this please help me some one

my arrayLists:-

  String[] planets = new String[] { "Mercury","Mercury", "Venus", "Earth", "Mars",
            "Jupiter", "Saturn", "Uranus", "Neptune"};

 String[] Ruppes = new String[] { "100", "200","1000", "300", "400",
            "500", "600", "700", "800"};
AbhiRam
  • 2,033
  • 7
  • 41
  • 94
  • [See this answer](http://stackoverflow.com/questions/6171663/how-to-find-index-of-int-array-in-java-from-a-given-value) – Talha Mar 10 '16 at 09:37

2 Answers2

23

you can do by using Arrays.asList

Arrays.asList(planets).indexOf("Mercury") // pass value

Arrays.asList(Rupps).indexOf("100") // pass value

or

Arrays.asList(planets).indexOf(2); //pass index
Nirav Ranpara
  • 13,753
  • 3
  • 39
  • 54
4
private int foundIndex(String query) {
    for (int i = 0; i < planets.length; i++) {
        if (planets[i].equals(query)) {
            return i;
        }
    }
    return -1;
}

... in code:

        int index = foundIndex("Venus");
        if(index!=-1){
            String currentRuppes =Ruppes[index]
        }
lionscribe
  • 3,413
  • 1
  • 16
  • 21
Leonid Veremchuk
  • 1,952
  • 15
  • 27