'Getting / Searching in array

How can I get the value of int "icon" from the Int values array with an example:

2 = R.drawable.ic_blue?

or: how to get: R.drawable.ic_blue? knowing id: 2?

public class iconsList {

 public class IntValues {
     public int id;
     public int icon;
     public IntValues(int id, int icon){
        this.id=id;
        this.icon=icon;
     }
 }

 IntValues[] icons = new IntValues[] {
     new IntValues(0, R.drawable.ic_default),
     new IntValues(1, R.drawable.ic_red),
     new IntValues(2, R.drawable.ic_blue),
 };

}


Solution 1:[1]

Add this method to your iconsList class

  public IntValues find(int num) {
    for (int i = 0; i < icons.length; i++) {
      if (num == icons[i].icon) {
        return icons[i];
      }
    }
    return null;
  }

You can return the index ( i ) or the class IntValues with id..

Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source
Solution 1 obeid_s