File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
http://aspose.com/file-tools
The moose likes Beginning Java and the fly likes arrays? Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Java » Beginning Java
Bookmark "arrays?" Watch "arrays?" New topic
Author

arrays?

Lydia Su
Greenhorn

Joined: Apr 27, 2001
Posts: 13
i've a question involving reading a user inputted value to set the length of an array then showing the largest number from the array. how do i make the program show the largest number after sorting? i keep getting errors on this...
Mindy Wu
Ranch Hand

Joined: Jan 12, 2001
Posts: 121
To get the largest number you can do this
int intLargestNum = array[array.length-1]
if you sort your number ascending order.
Hope this help!
Mindy
William Barnes
Ranch Hand

Joined: Mar 16, 2001
Posts: 984

Are you asking how to find the largest number in the array or how to find the largest array index (aka the size of the array)? Mindy's post answered the size of array question. If you want to find the largest value in the array try something like this:
int largest = 0 ;
for( int i=0; i < ArrayVar.largest; i++) {<br /> if( ArrayVar[ i ] > largest) {
largest = ArrayVar[ i ] ;
}
}
(I didn't compile this so it may need to be fixed a little.)


Please ignore post, I have no idea what I am talking about.
Lydia Su
Greenhorn

Joined: Apr 27, 2001
Posts: 13
i need the largest number in the array...must the array be sorted to get the largest number?
sharad goswami
Ranch Hand

Joined: Jan 18, 2001
Posts: 51
well i don't get it why don't u use noraml sorting matters?


Stop The World I Want To Get Off.
William Barnes
Ranch Hand

Joined: Mar 16, 2001
Posts: 984

No, if you just want the largest number you don't need to sort the array. To get the largest number use something like the code in my previous post.
If you want the largest number to be at the first, or last, element in the array than you need to sort.
deep venu
Ranch Hand

Joined: Jun 09, 2001
Posts: 56
for(i = 0, i < arr.length - 1; i++)<br /> {<br /> for(j=i+1; j = arr.length; j++)<br /> {<br /> if(arr[i] > arr[j])
{
int temp = arr[i];
arr[i] = arr[j];
arr[j] = temp;
}
}
}
This should sort ur array ..then take the last element of sorted array
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: arrays?
 
Similar Threads
pick the largest, second and third number from an array
Putting Ints into an Object array.
Finding Nth Largest element of an array without sorting
Range class
2d arrays help