Jump to content

View more

Image of the Day

雑魚は多めにして、爽快感重視にしつつ・・・(´・ω・`)
早いとこ、ベースを作って、完成にもっていかないとね。
タイトルもまだ迷ってるだよなぁ。 
#indiedev  #indiegame #screenshotsaturday https://t.co/IwVbswGrhe
IOTD | Top Screenshots

The latest, straight to your Inbox.

Subscribe to GameDev.net Direct to receive the latest updates and exclusive content.


Sign up now

Sorting Java.

4: Adsense

Old topic!

Guest, the last post of this topic is over 60 days old and at this point you may not reply in this topic. If you wish to continue this conversation start a new topic.


  • You cannot reply to this topic
3 replies to this topic

#1 logitech1   Members   

121
Like
0Likes
Like

Posted 04 June 2014 - 01:21 AM

Question guys, need some help about sorting using arraylist, i don't know why it doesn't sort at all.. >_< sorry for the post.

 

public class Driver2 {
    
    static Random r = new Random();

    public static void main(String[]args) {
    
        ArrayList<Integer> list = new ArrayList<Integer>();
        
        for(int i = 0 ; i < 3 ; i++) {
            list.add(rollDice());
        }
        
        for(Integer g : list)
        System.out.println(g);
        
        for(int index = 0 ; index < list.size() ; index++) {
            for(int i = 1 ; i < list.size() - 1 ; i++) {
                
                if(list.get(index) < list.get(i)) {
                    int temp = list.get(index);
                    list.remove(index);
                    list.add(index , list.get(i));
                    list.remove(i);
                    list.add(i, temp);
                }
            }
        }
        
        for(Integer g : list)
            System.out.println(g);
        
    }
    
    public static int rollDice() {
        
        int temp;
        temp = (1 + r.nextInt(6));
        
        return temp;
    }
}



#2 BitMaster   Members   

8647
Like
0Likes
Like

Posted 04 June 2014 - 02:31 AM

First, what does "it doesn't sort at all" even mean? Is there no change? Is there change but it's not the expected result? Sample output might help.

Then, what algorithm are you trying to implement? I don't recognize it (possibly because of the horrible code formatting) and the quadratic runtime does not look like something often-used. Is that a well-known algorithm or something you think 'should work'. If the latter, have you tried to test it on paper if it should work at all.
I would also think it's highly unlikely you really want to use ArrayList.add() and ArrayList.remove() while sorting. It is unnecessary (in general you want to swap elements) and will cause hell with indices.
Also note, if implementing the sorting algorithm is not the purpose of this, there are sorting functions in java.util.Arrays.

#3 Ashaman73   Members   

13714
Like
0Likes
Like

Posted 04 June 2014 - 02:33 AM

If this is not homework, then use the standard sort of java (eg Collections.sort).

 

If this is homework, and your task is to write bubblesort,then take a closer look here:

        for(int index = 0 ; index < list.size() ; index++) {
            for(int i = 1 ; i < list.size() - 1 ; i++) {


Edited by Ashaman73, 04 June 2014 - 02:34 AM.

Ashaman

 

Gnoblins: Website - Facebook - Twitter - Youtube - Steam Greenlit - IndieDB - Gamedev Log


#4 Wooh   Members   

1088
Like
0Likes
Like

Posted 04 June 2014 - 02:47 AM

The swap code also doesn't work. Take a look at these lines:
list.remove(index);
list.add(index , list.get(i));
Remove will shift all elements after the remove element so that they are stored at an index that is one smaller. If i > index then list.get(i) will not give you the same number as it did before you called remove.




Old topic!

Guest, the last post of this topic is over 60 days old and at this point you may not reply in this topic. If you wish to continue this conversation start a new topic.