Question:
Java Array Question Help?
skullz3200
2009-03-08 16:36:20 UTC
In java, suppose you make end with an array (an integer array of course) that looks like:

{4 ,5 ,6, 2 ,9, 5, 3, 5} (or something similar).

Is there a way to organize that array from greatest integer to the least so that it looks like

{9, 6, 5, 5, 5, 4, 3, 2}

Thanks guys! :D
Five answers:
nitaoe
2009-03-08 17:02:35 UTC
Say you have your array,



int[] numbers;

int[] new = new int[numbers.length];

Arrays.sort(numbers);

for(int i=0;i
new[i] = numbers[numbers.length-i-1];
bryanchen
2009-03-08 17:34:01 UTC
If your array of integers is as follows:



int[] a = { 10, 5, 3, 8. 5};



Then you can sort the array by doing:



Arrays.sort(a);



Remember that because Arrays is in the Java util package, you'll need to import it at the top of the .java file:



import java.util.Arrays;



There shouldn't be any faster sorting mechanism for sorting an array of integers because the in-built sort() method uses quicksort.
2016-10-15 10:16:44 UTC
Re:... the minimum and optimum numbers. you go with 2 conserving (storage) factors. (Variables.) One to hold the min value and one to hold the max value. while your application starts off, initialize the min value with something like 9999999. Then, by using fact this technique reads each and all of the ten numbers, take a 2nd and examine that variety to the present variety interior the min variable and that variety to the present variety interior the max variable. If this modern variety study is below the present min value, then THAT variety turns into the recent min variable. flow it in there. this is going to the 1st time via. remember all of us started it out with 9999999, so of direction it's going to be decrease. If this modern variety study is super than the present max value, then THAT variety turns into the recent min variable. flow it in there. this is going to the 1st time via. this is going to start up as 0. this is it. while all ten numbers are appeared at and in comparison in this form, your min variable will incorporate the backside variety entered, and your max variable will incorporate the main important value entered.
Blackcompe
2009-03-08 16:45:03 UTC
sort() will organize the element in ascending order, you need to invoke the sort method that takes an object array and a comparator.
Pat M
2009-03-08 16:43:00 UTC
Use the Arrays.sort() method.


This content was originally posted on Y! Answers, a Q&A website that shut down in 2021.
Loading...