Results 1 to 2 of 2
- 02-17-2012, 06:19 PM #1
Member
- Join Date
- Dec 2011
- Posts
- 11
- Rep Power
- 0
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.
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!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>{ @Override 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 && cmp.compare(temp, 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>()); } }
- 02-17-2012, 06:47 PM #2
Re: trying to understand generic Syntax
AnyType is what its name suggests.
Lesson: Generics (The Java™ Tutorials > Learning the Java Language)
dbWhy do they call it rush hour when nothing moves? - Robin Williams
Similar Threads
-
ArrayLists- generic?
By katiebear128 in forum New To JavaReplies: 3Last Post: 10-14-2011, 02:43 AM -
Java Generic Plz Help
By amtrbz in forum New To JavaReplies: 1Last Post: 01-29-2011, 11:47 PM -
generic code
By mac in forum New To JavaReplies: 4Last Post: 05-27-2010, 05:06 PM -
Java syntax I don't understand - assignment
By poet in forum New To JavaReplies: 2Last Post: 09-19-2009, 06:47 AM -
A generic interface example
By Java Tip in forum java.langReplies: 0Last Post: 04-17-2008, 07:42 PM


LinkBack URL
About LinkBacks
Reply With Quote
Bookmarks