arraylist5.0

arraylist5.0 - ArrayLists in JDK 5.0 Genericity...

Info iconThis preview shows pages 1–2. Sign up to view the full content.

View Full Document Right Arrow Icon
ArrayLists in JDK 5.0 Genericity (parameterized types) Genericity is a mechanism for clients to specify the types of objects that a class can work with via parameters passed at declaration-time and evaluated at compile-time. Suppose that you have the following code to add two objects to an object of ArrayList. ArrayList list = new ArrayList(); Integer int1 = new Integer(12); String str1 = new String("abc"); list.add(int1); list.add(str2); Later you might have code: for (int i = 0; i < list.size(); i++) { Integer v1 = (Integer) list.get(i); } However, when the object retrieved at i = 1, we do not have an object of Integer and this can cause an execution error, ClassCastException can be thrown. To prevent this from happening, we can specify an "unqualified identifier" (type parameter) in <> to declare the ArrayList object as follows: (an unqualified identifier does not specify a type, it serves as a placeholder for a type.) ArrayList<Integer> list = new ArrayList<Integer>(); Integer int1 = new Integer(12);
Background image of page 1

Info iconThis preview has intentionally blurred sections. Sign up to view the full version.

View Full DocumentRight Arrow Icon
Image of page 2
This is the end of the preview. Sign up to access the rest of the document.

This note was uploaded on 11/09/2010 for the course CSE 71682 taught by Professor Nakamura during the Spring '10 term at ASU.

Page1 / 2

arraylist5.0 - ArrayLists in JDK 5.0 Genericity...

This preview shows document pages 1 - 2. Sign up to view the full document.

View Full Document Right Arrow Icon
Ask a homework question - tutors are online