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 3 of 3

Thread: Generic method using an int[] argument

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

    Default Generic method using an int[] argument

    Hello,
    I have the following generic function declared.
    public static <T extends Comparable<? super T>> void selectionSort (T[] data)

    However eclipse will not compile saying:
    The method selectionSort(T[]) in the type writeInts is not applicable for the arguments (int[])


    Here is the call from main and the rest of the code

    	public static void main(String[] args) throws IOException {
    		// TODO Auto-generated method stub
    		boolean found = false;
    		final int DEFAULT_SIZE = 1000;
    		int[] rowValues = new int[DEFAULT_SIZE];
     
    		writeInts myWriteInts = new writeInts();
     
    		found = myWriteInts.selectionSort(rowValues);
    ;
     
    	}
     
    public static <T extends Comparable<? super T>> void selectionSort (T[] data)
    	    {
    	       int min;
    	       T temp;
     
    	       for (int index = 0; index < data.length-1; index++)
    	       {
    	          min = index;
    	          for (int scan = index+1; scan < data.length; scan++)
    	             if (data[scan].compareTo(data[min])<0)
    	                min = scan;
     
    	          /** Swap the values */
    	          temp = data[min];
    	          data[min] = data[index];
    	          data[index] = temp;
    	       }
    	    }


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

    Default Re: Generic method using an int[] argument

    int is a primitive data type, and as such Java's generics can't handle them. Instead, try using the Integer wrapper class.

  3. The Following User Says Thank You to helloworld922 For This Useful Post:

    dubois.ford (March 18th, 2010)

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

    Default Re: Generic method using an int[] argument

    Thanks that solved it.

Similar Threads

  1. [SOLVED] generic arguments, binary tree
    By vendetta in forum Collections and Generics
    Replies: 0
    Last Post: February 26th, 2010, 07:40 AM
  2. [SOLVED] Command Line Argument Help
    By EmSaint in forum Loops & Control Statements
    Replies: 2
    Last Post: January 28th, 2010, 10:55 AM
  3. generic class
    By fireatmuself in forum Collections and Generics
    Replies: 4
    Last Post: November 17th, 2009, 06:06 AM
  4. he operator / is undefined for the argument type(s) String, int
    By mtbr00x in forum File I/O & Other I/O Streams
    Replies: 2
    Last Post: September 8th, 2009, 08:34 PM
  5. Generic programming example
    By neo_2010 in forum Java Programming Tutorials
    Replies: 3
    Last Post: July 8th, 2009, 11:38 AM