Unit02A - Lists Organizing Data Linearly ArrayList Lists 2A...

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

View Full Document Right Arrow Icon
1 1 Lists Organizing Data Linearly ArrayList 2A 2 Lists A list is a linear sequence of data. List interface in Java int size() boolean add(E element) void add(int index, E element) E remove(int index) E get(int index) E set(int index, E element) 3 ArrayList Implements the List interface Acts like an array but The size of the ArrayList instance increases and decreases as necessary based on the number of elements in the data structure. Each element has an index that starts with 0, just like arrays, but we do not use subscript notation. ArrayList myList = new ArrayList(); ... myList.add(3, someObject); Object someObject = myList.get(1); 4 ArrayList (pre Java 5) ArrayList myList = new ArrayList(); myList.add("Steelers"); myList.add("Pirates"); myList.add("Penguins"); String a = (String)myList.get(0); String b = (String)myList.get(1); String c = (String)myList.get(2); ArrayList holds references to Object add method implicitly widens String type to Object typecasting is needed
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 12/19/2009 for the course CS 121 taught by Professor Reid-miller during the Spring '09 term at Carnegie Mellon.

Page1 / 3

Unit02A - Lists Organizing Data Linearly ArrayList Lists 2A...

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