Sep
25
|
One solution would be to loop through the array maintaining the largest value found in a variable. Another solution would be to first sort the array, then the largest value would be the last element.
int[] values = { 2, 67, 15, 3, 567 }; // first approach int largest = values[0]; for (int i=1; i<values.length; i++) { smallest = Math.max(smallest, values[i]); } // second approach java.util.Arrays.sort(values); largest = values[lalues.length-1];
Array ( ) One Response to “How do I find the largest value in an array?”
Leave a Reply
You must be logged in to post a comment.
October 15th, 2009 at 11:46 pm
Collection col = new ArrayList();
int[] values = { 2, 67, 15, 3, 567 };
for(int i=1; i<values.length; i++) ){
col.add(values[i]);
}
java.util.Collections.max(col);
java.util.Collections.min(col);