I want to find if the property numb in the ArrayList grids contains a certain value or not. Then I want to get the index of the entry that contains that value.
public class Grid {
public int numb;
public GridSeg(int numb) {
this.numb = numb;
}
}
public ArrayList<Grid> grids = new ArrayList<Grid>();
for (int i = 0; i < 6; i++) {
Grid grid = new Grid(i);
grids.add(grid);
}
/pseudo code since I don't know how it is done
if (grids.numb.contains(12)) {
if (grid.numb == 12) //get grid index
}
numbproperty of the current element and if it matches return the current index (and of course return -1 or something similar if no such element has been found)?.containsif theArrayListis an array of primitives.list.contains()tells you whether an element is contained in the list (as defined byequals()) but it doesn't tell you where.for(int i; i < list.size(); i++) { if(list.get(i).numb == 12) return i; } return -1;isn't that much code (or something likeint index = 0; for( Grid g : list) { if( g.numb == 12) return index; index++; } return -1;if you want to use a foreach).