Karem Karem - 1 year ago 95
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");
Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download