Results 1 to 7 of 7
  1. #1
    Metastar's Avatar
    Metastar is offline Member
    Join Date
    Jul 2010
    Posts
    62
    Rep Power
    0

    Question Problem with selection sort

    Hi, I am having trouble making my selection sort work for my program. Everything seems to check out up until my sort. I'm not understanding why I cannot do what I want to do, which is use the swap method I made to swap two objects. Please let me know what you think.

    Also I am trying to sort objects that have various attributes, they look like this:

    Israel Tel Aviv West Asia 4 IS

    I get the error message:

    Exception in thread "main" java.lang.RuntimeException: Uncompilable source code - unexpected type

    required: variable
    found: value
    at project3cquast.CountryArray.swap(CountryArray.java :101)
    at project3cquast.CountryArray.selectionSort(CountryA rray.java:91)
    at project3cquast.Main.main(Main.java:26)
    Java Result: 1

    91 is the swap() call
    101 is the swap method itself

    my code is:
    Java Code:
    public void selectionSort()
        {
            int out, in, min;
    
            for(out=0; out < reg4ArrList.size()-1; out++)
            {
                min = out;
                
                for(in=out+1; in < reg4ArrList.size(); in++)
                {
                    if(reg4ArrList.get(in).getCountryCode().compareToIgnoreCase(reg4ArrList.get(min).getCountryCode()) > 0)
                    {
                        min = in;
                        swap(out, min);
    
                    }
                }
            }
        }
    
        private void swap(int i, int j)
        {
            Country temp = reg4ArrList.get(i);
            reg4ArrList.get(i) = reg4ArrList.get(j);
            reg4ArrList.get(j) = temp;
    
        }
    Last edited by Metastar; 10-21-2010 at 01:26 AM.

  2. #2
    Metastar's Avatar
    Metastar is offline Member
    Join Date
    Jul 2010
    Posts
    62
    Rep Power
    0

    Default

    anyone????

  3. #3
    Metastar's Avatar
    Metastar is offline Member
    Join Date
    Jul 2010
    Posts
    62
    Rep Power
    0

    Default

    All I need to figure out is why my swap method gives me the error I mentioned above. That's my only problem and I cannot figure it out to save my life.

  4. #4
    pbrockway2 is offline Moderator
    Join Date
    Feb 2009
    Location
    New Zealand
    Posts
    4,565
    Rep Power
    12

    Default

    Which line exactly is giving the error? I would guess that its

    Java Code:
    reg4ArrList.get(i) = reg4ArrList.get(j);

    What is the declaration of reg4ArrList? And what is this line supposed to do?

  5. #5
    Metastar's Avatar
    Metastar is offline Member
    Join Date
    Jul 2010
    Posts
    62
    Rep Power
    0

    Default

    reg4ArrList is an arraylist of type country and that line (how I thought it would work) was supposed to assign what was at index j to index i to swap them. I did however figure out my problem. Since I was using an arraylist I had to use the set method to do the assigning and I guess I can't simply code it how I had it. This is the correct swap:

    Java Code:
    private void swap(int i, int j)
        {
            Country temp1 = reg4ArrList.get(i);
            reg4ArrList.set(i, reg4ArrList.get(j));
            reg4ArrList.set(j, temp1);
    
        }
    But thank you for looking at my post and responding :)

  6. #6
    pbrockway2 is offline Moderator
    Join Date
    Feb 2009
    Location
    New Zealand
    Posts
    4,565
    Rep Power
    12

    Default

    I'm glad you got it figured out. Yes you're quite right the documented behaviour of get() is to ... get things. No amount of getting things will cause things to be set.

    Another way of looking at this to realise that, in Java, = always has a variable (or array+index) on the left and an expression on the right.

  7. #7
    Metastar's Avatar
    Metastar is offline Member
    Join Date
    Jul 2010
    Posts
    62
    Rep Power
    0

Similar Threads

  1. Selection Sort recursive java code
    By myst in forum New To Java
    Replies: 3
    Last Post: 07-12-2010, 10:08 AM
  2. selection sort
    By mayhewj7 in forum New To Java
    Replies: 1
    Last Post: 04-29-2009, 12:40 AM
  3. Replies: 3
    Last Post: 01-26-2009, 12:20 AM
  4. write a selection sort without having numerous variable?
    By seandingobat in forum New To Java
    Replies: 6
    Last Post: 10-28-2008, 02:33 PM
  5. Selection sort in Java
    By Java Tip in forum Algorithms
    Replies: 0
    Last Post: 04-15-2008, 07:41 PM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •