Karem Karem - 14 days ago 6
Java Question

java: array if all the same

I have a array:

int tarningar[] = new int[5];


That holds numbers. When all the numbers are the same, system.out.println('ok');

How can I do this?

Answer
boolean flag = true;
int first = tarningar[0];
for(int i = 1; i < 5 && flag; i++)
{
  if (tarningar[i] != first) flag = false;
}
if (flag) System.out.println("ok");
Comments