Welcome to the Java Programming Forums


The professional, friendly Java community. 21,500 members and growing!


The Java Programming Forums are a community of Java programmers from all around the World. Our members have a wide range of skills and they all have one thing in common: A passion to learn and code Java. We invite beginner Java programmers right through to Java professionals to post here and share your knowledge. Become a part of the community, help others, expand your knowledge of Java and enjoy talking with like minded people. Registration is quick and best of all free. We look forward to meeting you.


>> REGISTER NOW TO START POSTING


Members have full access to the forums. Advertisements are removed for registered users.

Results 1 to 2 of 2

Thread: Help with Comparable interface.

  1. #1
    Junior Member
    Join Date
    Oct 2011
    Posts
    1
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Help with Comparable interface.

    Hi,

    I have written a program to implement five sorting methods. This program will sort an array obtained from user input and return the sorted array along with the number of comparisons and total time of execution of each method. When I try to compile, I receive errors stating "int cannot be dereferenced."

    The following is the code for which I am receiving the error.

    public static <numbers extends Comparable> int mergeSort (int numbers[], int min, int max)
       {    
    		  int temp[];
            int index1, left, right;
     
            //find the length and the midpoint of the list 
            int size = max - min + 1;
            int pivot = (min + max) / 2;
     
            temp = (int[])(new Comparable[size]);
     
            //sort left half of list
            mergeSort(numbers, min, pivot);
     
            //sort right half of list
            mergeSort(numbers, pivot + 1, max);
     
            //copy sorted data into workspace
            for (index1 = 0; index1 < size; index1++)
               temp[index1] = numbers[min + index1];
     
            //merge the two sorted lists
            left = 0;
            right = pivot - min + 1;
    		  int comparisons = 0;
     
            for (index1 = 0; index1 < size; index1++)
            {
    		  	  comparisons++;
     
               if (right <= max - min)
                  if (left <= pivot - min)
                      if (temp[left].compareTo(temp[right]) > 0)
                         numbers[index1 + min] = temp[right++];
                      else
                         numbers[index1 + min] = temp[left++];
                  else
                     numbers[index1 + min] = temp[right++];
               else
                  numbers[index1 + min] = temp[left++];
            } 
     
    		  return comparisons;        
       }
     
     
       //-----------------------------------------------------------------
       //  Sorts the specified array of objects using a bubble sort
       //  algorithm.
       //-----------------------------------------------------------------
       public static <numbers extends Comparable> int bubbleSort (int numbers[])
       {
          int position, scan;
          int temp;
    		int comparisons = 0;
     
          for (position = numbers.length - 1; position >= 0; position--)
          {
             for (scan = 0; scan <= position - 1; scan++)
             {
    				comparisons++;
     
                if (numbers[scan].compareTo(numbers[scan+1]) > 0)
                {
                    // Swap the values
                    temp = numbers[scan];
                    numbers[scan] = numbers[scan + 1];
                    numbers[scan + 1] = temp;
                }
             }
          }
     
    		return comparisons;
       }
     
       //-----------------------------------------------------------------
       //  Sorts the specified array of integers using the selection
       //  sort algorithm.
       //-----------------------------------------------------------------
       public static <numbers extends Comparable> int selectionSort (int numbers[])
       {
          int min;
          int temp;
    		int comparisons = 0;
     
          for (int index = 0; index < numbers.length-1; index++)
          {
             min = index;
    			comparisons++;
     
             for (int scan = index+1; scan < numbers.length; scan++)
                if (numbers[scan].compareTo(numbers[min])<0)
                   min = scan;
     
             // Swap the values
             temp = numbers[min];
             numbers[min] = numbers[index];
             numbers[index] = temp;
          }
     
    		return comparisons;
       }
     
       //-----------------------------------------------------------------
       //  Sorts the specified array of objects using an insertion
       //  sort algorithm.
       //-----------------------------------------------------------------
       public static <numbers extends Comparable> int insertionSort (int numbers[])
       {
    		int comparisons = 0;
     
          for (int index = 1; index < numbers.length; index++)
          {
             int key = numbers[index];
             int position = index;
    			comparisons++;
     
             // Shift larger values to the right
             while (position > 0 && numbers[position-1].compareTo(key) > 0)
             {
                numbers[position] = numbers[position-1];
                position--;
             }
     
             numbers[position] = key;
          }
     
    		return comparisons;
       }

    I am not sure what I'm doing wrong. I am very new to java and this is the first time I am using the Comparable interface and don't believe I am doing it right. Any help is greatly appreciated. Thank you.


  2. #2
    Crazy Cat Lady KevinWorkman's Avatar
    Join Date
    Oct 2010
    Location
    Washington, DC
    Posts
    5,370
    My Mood
    Hungover
    Thanks
    144
    Thanked 626 Times in 537 Posts

    Default Re: Help with Comparable interface.

    What line is giving you the error? If you want help, you should provide an SSCCE (that means you should boil the problem down to as few lines as possible) that demonstrates the problem.

    Hint: int is a primitive, not an Object, so you can't do .anything() on it.
    Useful links: How to Ask Questions the Smart Way | Use Code Tags | Java Tutorials
    Static Void Games - Play indie games, learn from game tutorials and source code, upload your own games!

Similar Threads

  1. When to use Comparator and Comparable Interface in java
    By diyaots in forum Java Theory & Questions
    Replies: 7
    Last Post: October 26th, 2012, 09:35 AM
  2. Priority Queue using comparable
    By jkalm in forum Collections and Generics
    Replies: 6
    Last Post: December 5th, 2010, 09:02 PM
  3. Making item sortable (implementing Comparable)
    By Asido in forum What's Wrong With My Code?
    Replies: 3
    Last Post: August 19th, 2010, 12:44 PM
  4. [SOLVED] Problem with Ordering an Arraylist of Comparable Objects.
    By Faz in forum Collections and Generics
    Replies: 8
    Last Post: June 16th, 2010, 06:36 PM
  5. Comparable Interface
    By yelrubk in forum What's Wrong With My Code?
    Replies: 1
    Last Post: April 28th, 2010, 09:08 AM