Results 1 to 2 of 2
  1. #1
    cris9288 is offline Member
    Join Date
    Dec 2011
    Rep Power

    Default trying to understand generic Syntax

    Hell, I am trying to understand how to use parameterized types and I keep getting confused as to what my AnyType variable refers to. I have an example that I have been working with. In this example, Shape implements comparable and Rectangle implements Shape.

    Java Code:
    import java.util.ArrayList;
    import java.util.List;
    public class GenericInsertionSort {
        private static class DefaultComparator<AnyType extends 
                Comparable<? super AnyType>> implements Comparator<AnyType>{
            public int compare(AnyType lhs, AnyType rhs) {
                return lhs.compareTo(rhs);
        public static <AnyType> void insertionSort(List<AnyType> myList, 
                Comparator<? super AnyType> cmp) {
            for (int p = 1; p < myList.size(); p++){
                AnyType temp = myList.get(p);
                int j = p;
                for (; j > 0 &&, myList.get(j-1)) < 0; j--)
                    myList.set(j, myList.get(j - 1));
                myList.set(j, temp);
        public static void main(String[] args) {
            ArrayList<Integer> myInts = new ArrayList<Integer>();
            List<Rectangle> shapeList = new ArrayList<Rectangle>();
            shapeList.add(new Rectangle(10, 12));
            shapeList.add(new Rectangle(11, 15));
            insertionSort(shapeList, new DefaultComparator<Shape>());
    In this example, what is AnyType (in my insertionSort method) referring to? Is it shape or rectangle? I know that my comparator is of type Shape, and myList is of type Rectangle, I just don't understand how the method call is resolved. Thanks!

  2. #2
    DarrylBurke's Avatar
    DarrylBurke is offline Forum Police
    Join Date
    Sep 2008
    Madgaon, Goa, India
    Rep Power

    Default Re: trying to understand generic Syntax

    If you're forever cleaning cobwebs, it's time to get rid of the spiders.

Similar Threads

  1. ArrayLists- generic?
    By katiebear128 in forum New To Java
    Replies: 3
    Last Post: 10-14-2011, 02:43 AM
  2. Java Generic Plz Help
    By amtrbz in forum New To Java
    Replies: 1
    Last Post: 01-29-2011, 11:47 PM
  3. generic code
    By mac in forum New To Java
    Replies: 4
    Last Post: 05-27-2010, 05:06 PM
  4. Java syntax I don't understand - assignment
    By poet in forum New To Java
    Replies: 2
    Last Post: 09-19-2009, 06:47 AM
  5. A generic interface example
    By Java Tip in forum java.lang
    Replies: 0
    Last Post: 04-17-2008, 07:42 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