Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Java: Check if next "field" in array exists

Tags:

java

arrays

Ive made this:

if( tal[i+1] ){
    if( tal[i] == tal[i+1]){
        match=true;
    }
}

But it doesnt seem to work.

I want to check whether the field next to the current (i) exists, in the array tal[].

How can i fix this?

like image 274
Karem Avatar asked Dec 04 '22 09:12

Karem


1 Answers

If by "exists" you mean "is not out of bounds", then you have to check the length:

if (i+1 < tal.length) {
  // i+1 is a valid index in tal here
}
like image 54
Joachim Sauer Avatar answered Dec 14 '22 17:12

Joachim Sauer