array t1={2,6,4,9}
can anyone give a for or while loop code such that i need the largest and the secondlargest values from this array
ie the values 6,9
Printable View
array t1={2,6,4,9}
can anyone give a for or while loop code such that i need the largest and the secondlargest values from this array
ie the values 6,9
Quote:
array t1={2,6,4,9}
what is array is this a Class
java.sql.Array or reflect.Array :confused:
In simple way, loop the array on each element and at the same time maintain two variables to hold largest values.
...in case you think of replacing the simple array with a ArrayList obj you can very well go for the Collections.max(Collection obj) to get the maximum value.
Regards,
Abhishek.
Yes, it's true. But he is looking to do it in a loop.
Something like this may help in that sense, I think.
Code:private void findMax(ArrayList arl) {
int max = 0;
for(int i = 0; i < arl.size(); i++) {
if(Integer.parseInt((String) arl.get(i)) > max) {
max = Integer.parseInt((String) arl.get(i));
}
}
System.out.println(max);
}