Karem Karem - 10 months ago 59
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 Source
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");