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: Using a Generic Insertion Sort method

  1. #1
    Junior Member
    Join Date
    Mar 2010
    Posts
    11
    Thanks
    3
    Thanked 0 Times in 0 Posts

    Default Using a Generic Insertion Sort method

    Hello,
    I am having a real hard to trying to figure out how to use generics

    I have the following insertion sort method defined
      public static <T extends Comparable<? super T>> void insertionSort (T[] data)
      {
         for (int index = 1; index < data.length; index++)
         {
            T key = data[index];
            int position = index;
     
            /** Shift larger values to the right */
            while (position > 0 && data[position-1].compareTo(key) > 0)
            {
               data[position] = data[position-1];
               position--;
            }
     
            data[position] = key;
         }
      }

    I am trying to call it in the following manner where class Card is previously defined


    private static List< Card > list; // declare List that will store Cards
     
    public static void main( String args[] )
       {
          DeckOfCards cards = new DeckOfCards();
          cards.printCards();
          InsertionSort.insertionSort(list);
     
       } // end main

    However, I get the following error:
    The method insertionSort(T[]) in the type InsertionSort is not applicable for the arguments (List<Card>)


    Any help


  2. #2
    Super Moderator helloworld922's Avatar
    Join Date
    Jun 2009
    Posts
    2,896
    Thanks
    23
    Thanked 619 Times in 561 Posts
    Blog Entries
    18

    Default Re: Using a Generic Insertion Sort method

    An Array T[] is not the same as a List<T>. Change your insertionSort method to take a parameter List<T>:

    public static <T extends Comparable<? super T>> void insertionSort (List<T> data)
    {
       // you'll need to fix your insertionSort method to sort a list rather than an array
    }

Similar Threads

  1. Insertion Sort
    By Kimimaru in forum Algorithms & Recursion
    Replies: 2
    Last Post: December 6th, 2010, 06:26 AM
  2. bubble sort and selection sort on strings
    By Sir Saula in forum What's Wrong With My Code?
    Replies: 5
    Last Post: July 3rd, 2010, 10:44 AM
  3. Pseudo code of insertion sort linked list
    By sungirl in forum Algorithms & Recursion
    Replies: 1
    Last Post: May 23rd, 2010, 10:25 AM
  4. Generic method using an int[] argument
    By dubois.ford in forum Collections and Generics
    Replies: 2
    Last Post: March 18th, 2010, 08:18 AM
  5. Replies: 6
    Last Post: October 20th, 2009, 07:33 AM