Logo

Programming-Idioms

History of Idiom 222 > diff from v31 to v32

Edit summary for version 32 by programming-idioms.org:
[Java] i is -1 by default

Version 31

2022-02-01, 22:39:56

Version 32

2022-02-01, 22:40:35

Idiom #222 Find the first index of an element in list

Set i to the first index in list items at which the element x can be found, or -1 if items does not contain x.

Idiom #222 Find the first index of an element in list

Set i to the first index in list items at which the element x can be found, or -1 if items does not contain x.

Variables
i,x,items
Variables
i,x,items
Extra Keywords
position
Extra Keywords
position
Code
for(int j=0;j<items.length;j++){
	if(items[j].equals(x)){
		i = j;
		break;
		}
	}
Code
int i = -1;
for(int j=0;j<items.length;j++){
	if(items[j].equals(x)){
		i = j;
		break;
	}
}
Comments bubble
The .equals() method equates objects.

If equating primitives you would use the “x == y” expression
Comments bubble
The .equals() method equates objects.

If equating primitives you would use the “x == y” expression