Results 1 to 6 of 6
  1. #1
    pytho is offline Member
    Join Date
    Jun 2011
    Location
    Indiana
    Posts
    6
    Rep Power
    0

    Lightbulb Checking to see if Array is sorted

    Hi everyone, I'm new to programming & this site has helped me a lot in the past so I thought I would ask another question. I'm trying to create the method isSorted that takes an array just returns true or false if the array is in descending order. I keep getting this error on line 10 saying "isSorted(int[]) in Sort cannot be applied to (int)" I'm not sure what this means.. my approach is to get the number at the index i and compare it to the number at i + 1. Can anyone explain this to me? I appreciate any help.

    Java Code:
    class Sort {
      
      public static boolean isSorted(int[] x) { 
        
        boolean isSorted = false;
        
        for (int i=0; i < x.length-1; i++) {
          
          if (x[i] < x[i+1]) {
            return Sort.isSorted(x[i]); //want to continue looking through list to see if all i is less than i-1
    
    //i get an error on this line saying "isSorted(int[]) in Sort cannot be applied to (int)" 
            
          } else if (x[i] > x[i+1]) {
            return false;
            
          } else
            isSorted = true;
          
        }
      }
    }

  2. #2
    JosAH's Avatar
    JosAH is offline Moderator
    Join Date
    Sep 2008
    Location
    Voorschoten, the Netherlands
    Posts
    13,778
    Blog Entries
    7
    Rep Power
    21

    Default

    The value x[ i ] is just a single int but your method takes an entire array of ints as a parameter; the compiler saw that and complained ...

    kind regards,

    Jos
    cenosillicaphobia: the fear for an empty beer glass

  3. #3
    pytho is offline Member
    Join Date
    Jun 2011
    Location
    Indiana
    Posts
    6
    Rep Power
    0

    Default

    Ahh I see. So is there not a way to use recursion with this problem?
    I am familiar with only scheme and know that in a list it's possible to recur by doing something to the car (first item in list) then recuring on the cdr (the rest of the list 1 by 1).

  4. #4
    JosAH's Avatar
    JosAH is offline Moderator
    Join Date
    Sep 2008
    Location
    Voorschoten, the Netherlands
    Posts
    13,778
    Blog Entries
    7
    Rep Power
    21

    Default

    Quote Originally Posted by pytho View Post
    Ahh I see. So is there not a way to use recursion with this problem?
    I am familiar with only scheme and know that in a list it's possible to recur by doing something to the car (first item in list) then recuring on the cdr (the rest of the list 1 by 1).
    Java isn't a functional language and a (primitive) array isn't a general list, i.e. it uses indexes and no pointer arithmetic (as in C). You can recur over an array but you have to pass the index from where to recur to your method as well; all in all it isn't worth all the trouble and go for an iterative approach instead.

    kind regards,

    Jos
    cenosillicaphobia: the fear for an empty beer glass

  5. #5
    Junky's Avatar
    Junky is offline Grand Poobah
    Join Date
    Jan 2011
    Location
    Dystopia
    Posts
    3,800
    Rep Power
    7

    Default

    Gazing into my crystal ball I see a missing return statement error in your future.

  6. #6
    pytho is offline Member
    Join Date
    Jun 2011
    Location
    Indiana
    Posts
    6
    Rep Power
    0

    Default

    Thanks Jos! I don't know why I was trying to use recursion inside of a for loop. The iterative way was not as hard to figure out as I thought it was.

    Junky..

Similar Threads

  1. Checking if an ArrayList is sorted
    By MonkeyGrad in forum New To Java
    Replies: 4
    Last Post: 04-13-2011, 07:27 AM
  2. Replies: 8
    Last Post: 01-15-2011, 10:39 PM
  3. Replies: 7
    Last Post: 04-28-2008, 05:28 AM
  4. Replies: 8
    Last Post: 04-22-2008, 04:21 AM
  5. Replies: 0
    Last Post: 04-14-2008, 09:39 PM

Tags for this Thread

Posting Permissions

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