Download presentation
Presentation is loading. Please wait.
Published by형기 오 Modified over 5 years ago
1
List Interface ArrayList class implements the List Interface import java.util.List; Lists are generic (java 5.0)—Elements are declared to be of a specific object type when a list or collection variable is declared. <E>is a the object type—tells compiler to check for that element type when a collection is used List<E> list = new ArrayList<E>(); List<String> list = new ArrayList<String>(); Margaret Winans, 2014 AP TIP
2
List<Integer> list = new ArrayList<Integer>();
The variable list is declared to be of type List<Integer> (the interface) but is instantiated as type ArrayList<Integer> (the implementation)—preferred byAPCS This has the advantage of making the code applicable to any List. For example, a single change: List<Integer> list = new LinkedList<Integer>(); The AP Quick Reference Guide (given to you for the AP exam) only gives methods of the List interface Margaret Winans, 2014 AP TIP
3
The ArrayList Class Contains a sequence of elements ordered by position Unlike an array in that: It uses methods rather than [] to manipulate elements. It tracks both logical and physical size. The logical size is 0 when created & automatically adjusts as needed. The positions available for access range from 0 to the logical size minus 1. ArrayList can be printed without a loop Margaret Winans, 2014 AP TIP
4
AP Quick Reference Guide: List Interface
The List interface methods below are all inherited by the ArrayList This is the actual screen shot from the QRG Margaret Winans, 2014 AP TIP
5
The ArrayList Class E = obj type not length boolean add(obj) Appends obj to end of the list; returns true Margaret Winans, 2014 AP TIP
6
Adding/Changing values using an ArrayList
//create a list of Integer objects List<Integer> list = new ArrayList<Integer>(); //add int values to the list for (int i = 1; i <= 100; i++) list.add(i); // Increment each int in the list for (int i = 0; i < 100; i++) list.set(i, list.get(i) + 1); Margaret Winans, 2014 AP TIP
Similar presentations
© 2025 SlidePlayer.com. Inc.
All rights reserved.