简体   繁体   中英

Sorting numbers in ascending order in arraylist java

import java.util.ArrayList;
import java.util.Random;

public class Final
{
    public static void main(String[]arg)
    {
        ArrayList<Integer>randomNumber2=new ArrayList<Integer>();
        Random rand=new Random();
        for(int i=0;i<10;i++)
        {
            randomNumber2.add(new Integer(rand.nextInt(50)));
        }
}

I have created this random generated arraylist that generates numbers 1-50. I want to sort the numbers that were generated in ascending order. Without using a Collection.sort, how would I do that using ArrayList?

Without just copying and pasting code let's think about the answer here. You have 10 values and you want to add them to an array in ascending order. If you were to actually do that in your head how would you go about organizing it?

One way (simple, I can explain it here, and I bet you would be able to implement it) is to assign the first number to the beginning of the Arraylist. Then, when the second number is generated, check it against the first value in the Arraylist, if the newly generated number is lower than the number your checking it against, then move that first number back 1 slot in the Arraylist and plug in the new number in the newly empty spot.

If you check a newly generated number against, say 2 numbers in the Arraylist and this new number is between the value of the first and the second number, then when you check the new number against the first it will check out and continue along the code. Then, when you check the new number against the second number in the Arraylist and find out that it's lower then you move that number back 1 slot in the ArrayList and plug in the new number into slot 2.

If you check a newly generated number against, say 3 numbers so far (this is the fourth iteration now) and it gets to the fourth value in the arraylist that's empty, then you assign the newly generated number to that fourth slot as it should be the new max value.

This doesn't make as much sense as I want it to, but I'm guessing this is for a lab/PA/hw/something like that; so I hope you can take what I wrote, understand it, and write your own code for it. It's not the most efficient way to do this - but it's an easy-to-understand method that just about anyone can code and is great for a simple classroom assignment.

Cheers!

Here is the code that collections.sort use internally that you can also use if you any kind od reservation against collections class. You can make this utility method in your class

public static <T extends Comparable<? super T>> void sort(List<T> list) {
   Object[] a = list.toArray();
    Arrays.sort(a);
    ListIterator<T> i = list.listIterator();
   for (int j=0; j<a.length; j++) {
   i.next();
   i.set((T)a[j]);
}

There are many, many sorting algorithms , but as it doesn't sound like performance is a concern and this might be homework...

So try Sleep Sort !

import java.util.ArrayList;
import java.util.Random;
import java.util.Timer;
import java.util.TimerTask;

public class Final {

    private static int count = 10;

    public static void main(String[] arg)
    {
        ArrayList<Integer> randomNumber2 = new ArrayList<Integer>();
        Random rand = new Random();

        for (int i = 0; i < count; i++) {
            randomNumber2.add(new Integer(rand.nextInt(50)));
        }

        Timer timer = new Timer();
        for(final Integer i : randomNumber2) {
            timer.schedule(new TimerTask()
            {
                public void run()
                {
                    System.out.println(i);
                    --count;
                }
            }, i * 50L);
        }
        while(count > 0){ Thread.yield();}
        timer.cancel();
    }
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM