a = 0
is wrong since a
is an int
array. You’re probably looking for a counter variable. It could be like this:
boolean existsData = false;
for(int i=0; i<arr.length; i++)
//arr[i] = 0 means assign 0 to arr[i]
//note that I'm using different (!=)
existsData = (arr[i] != 0);
if (existsData) {
break;
}
}
return existData;
Or even easier, without using a boolean
variable:
for(int i=0; i<arr.length; i++)
if (arr[i] != 0) {
return false;
}
}
return true;
Or, if you really mean no elements in the array, and not “no 0 elements”:
boolean isEmpty(int[] arr) {
return arr.length == 0;
}
2
solved Please some information regarding Java code [closed]