Home > other >  Getting / Searching in array
Getting / Searching in array

Time:05-15

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),
 };

}

CodePudding user response:

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..

  • Related