Fundamentals of Java: AP Computer Science Essentials, 4th Edition

Slides:



Advertisements
Similar presentations
Lists and the Collection Interface Chapter 4. Chapter Objectives To become familiar with the List interface To understand how to write an array-based.
Advertisements

Chapter 14 Introduction to Collections
CSC 205 – Java Programming II Lecture 25 March 8, 2002.
Chapter 10 Introduction to Arrays
Slides prepared by Rose Williams, Binghamton University Chapter 14 Generics and the ArrayList Class.
1 Java intro Part 3. 2 Arrays in Java Store fixed number of values of a given type Arrays are objects –have attributes –must be constructed Array declaration:
Using ArrayList. Lecture Objectives To understand the foundations behind the ArrayList class Explore some of the methods of the ArrayList class.
Lists and the Collection Interface Chapter 4. Chapter 4: Lists and the Collection Interface2 Chapter Objectives To become familiar with the List interface.
Slides prepared by Rose Williams, Binghamton University Chapter 14 Generics and the ArrayList Class.
Fall 2007CS 2251 Lists and the Collection Interface Chapter 4.
Chapter 9 Introduction to Arrays
Chapter 10 Strings, Searches, Sorts, and Modifications Midterm Review By Ben Razon AP Computer Science Period 3.
CSC 142 J(part 1) 1 CSC 142 The ArrayList class [Reading: chapter 10]
Chapter 11 Arrays Continued
5-Aug-2002cse Arrays © 2002 University of Washington1 Arrays CSE 142, Summer 2002 Computer Programming 1
Arrays and ArrayLists in Java L. Kedigh. Array Characteristics List of values. A list of values where every member is of the same type. Each member in.
Object-Oriented Program Development Using Java: A Class-Centered Approach, Enhanced Edition.
Computer Science 209 Software Development Java Collections.
ArrayList Class An ArrayList is an object that contains a sequence of elements that are ordered by position. An ArrayList is an object that contains a.
Dynamic Data Structures and Generics Chapter 10. Outline Vectors Linked Data Structures Introduction to Generics.
Aug 9, CMSC 202 ArrayList. Aug 9, What’s an Array List ArrayList is  a class in the standard Java libraries that can hold any type of object.
AP Computer Science edition Review 1 ArrayListsWhile loopsString MethodsMethodsErrors
Chapter 5 Array-Based Structures © 2006 Pearson Education Inc., Upper Saddle River, NJ. All rights reserved.
The ArrayList Data Structure Standard Arrays at High Speed! More Safety, More Efficient, and Less Overhead!
List Interface and Linked List Mrs. Furman March 25, 2010.
Chapter 5 Array-Based Structures © 2006 Pearson Education Inc., Upper Saddle River, NJ. All rights reserved.
COMPUTER PROGRAMMING 2 ArrayLists. Objective/Essential Standard Essential Standard 3.00Apply Advanced Properties of Arrays Essential Indicator 3.02 Apply.
The ArrayList Data Structure Standard Arrays at High Speed!
Chapter 9 Introduction to Arrays Fundamentals of Java.
1 Sections 5.1 – 5.2 Digital Image Processing Fundamentals of Java: AP Computer Science Essentials, 4th Edition Lambert / Osborne.
Lists and the Collection Interface Chapter 4. Chapter 4: Lists and the Collection Interface2 Chapter Objectives To become familiar with the List interface.
1 Sections 3.1 – 3.2a Basic Syntax and Semantics Fundamentals of Java: AP Computer Science Essentials, 4th Edition Lambert / Osborne.
CMSC 202 ArrayList Aug 9, 2007.
Sections 10.1 – 10.4 Introduction to Arrays
Sixth Lecture ArrayList Abstract Class and Interface
Sections Inheritance and Abstract Classes
Fundamentals of Java: AP Computer Science Essentials, 4th Edition
(like an array on steroids)
Array, Strings and Vectors
Software Development Java Collections
Intro to Collections.
ARRAYLIST AND VECTOR.
Array Array is a variable which holds multiple values (elements) of similar data types. All the values are having their own index with an array. Index.
Section 3.2c Strings and Method Signatures
Welcome to CSE 143!.
Array List Pepper.
CS 106A, Lecture 19 ArrayLists
Java for Beginners University Greenwich Computing At School DASCO
The ArrayList Class An ArrayList is a complex data structure that allows you to add or remove objects from a list and it changes size automatically. The.
Can store many of the same kind of data together
ArrayLists.
Words exercise Write code to read a file and display its words in reverse order. A solution that uses an array: String[] allWords = new String[1000]; int.
Arrays We often want to organize objects or primitive data in a way that makes them easy to access and change. An array is simple but powerful way to.
Arrays versus ArrayList
© A+ Computer Science - Arrays and Lists © A+ Computer Science -
CMSC 202 ArrayList Aug 9, 2007.
Welcome to CSE 143!.
Can store many of the same kind of data together
Dynamic Data Structures and Generics
Topics Covered: Arrays, 1-D & 2-D Passing & Returning Arrays
Object Oriented Programming in java
CMSC 202 ArrayList Aug 9, 2007.
Grouped Data Arrays, and Array Lists.
Lecture 13: ArrayLists AP Computer Science Principles
ArrayLists 22-Feb-19.
slides created by Alyssa Harding
Review: libraries and packages
List Interface ArrayList class implements the List Interface
Arrays and ArrayLists.
Arrays.
Presentation transcript:

Fundamentals of Java: AP Computer Science Essentials, 4th Edition Section 14.3 Using Lists Fundamentals of Java: AP Computer Science Essentials, 4th Edition Lambert / Osborne

Using Lists Lists and arrays are both: 2 2 Objects that contain a sequence of elements ordered by an integer index position. Elements can be both accessed and replaced. 2 2

Using Lists (continued) Lists are unlike arrays: Programmer must use methods rather than the subscript operator [ ] to manipulate list items. Lists track both logical and physical size. When first created, a list’s logical size is 0. The list updates its logical size as elements are added to or removed from the list. The index positions for access in a list range from 0 to its logical size minus 1. 3 3

Using Lists (continued) The List Interface: Includes a large number of methods: Boolean isEmpty(), int size(), etc. The methods get, set, and the first add and remove methods are index-based. Access an index position in the list to perform a task. Methods in the List interface show an element type named E or Object to indicate that the element type is generally an object. 4 4

Some methods of the List interface

Using Lists (continued) The List Interface (cont): Type variable: when a name such as E is used to specify a type in an interface. Type parameter: when a name such as String replaces the type variable during instantiation. Declaring and Instantiating a List: A list is an object and must be instantiated. Don’t use an interface name for a type name. Physical length is not specified. 6 6

Declaring and instantiating an ArrayList General syntax for declaring a list object and instantiating it as a list object: List<element-type> variable-name = new list-class-name<element-type> import java.util.*; // required to access List // creating an ArrayList of String objects List<String> agenda; // declaration agenda = new ArrayList<String>(); // instatiation // declaration and instantiation together List<String> agenda = new ArrayList<String>();

Using Lists (continued) Using List Methods: The programmer manipulates a list by sending it messages. Methods include: examining logical size; testing for emptiness; inserting, removing, examining, replacing and searching for elements, etc. 8 8

Calling List methods (example) List<String> list = new ArrayList<String>(); String s; list.add(“hello”); // add “hello” to the end list.add(0,”hi”); // add “hi” as first element list.set(1,”bye”); // replace element 1 s = list.get(0); // get element 0 s = list.remove(1); // remove element 1

Using Lists (continued) Lists and Primitive Types: Unlike arrays, lists can contain only objects, not values of primitive type, such as int or double. Primitive Types and Wrapper Classes: Wrapper class: used to store primitive data types in a list. A class that contains a value of primitive type. Use the appropriate wrapper class name as the element type parameter. 10 10

Using Lists (continued) Iterators: Iterators are objects that allow the programmer to visit all of the elements in a collection. The simplest iterator type supports the methods in the java.util.Iterator interface. The programmer opens an iterator object by running the iterator method. The iterator’s current position pointer is placed before the first object, if the collection isn’t empty. 11 11

Using Lists (continued) Iterators (cont): The programmer then tests to determine if there are elements to visit. Sends the hasNext() message. The message returns the element to the caller and advances the iterator’s current position to just beyond the first element. Repeat steps to visit all of the elements in a collection. 12 12

Using Lists (continued) Iterators (cont): When the compiler sees an enhanced for loop with a collection, it generates code that opens an iterator on the collection and carries out the loop process. Because a for loop is easier to use, programmers don’t use an explicit iterator unless a collection’s elements need to be removed. 13 13

Using Lists (continued) Confusing Arrays and Lists: Lists are accessed using methods; arrays are accessed using subscripts. Should I Use Arrays or Lists?: Lists are more powerful and easier to use. Lists include methods for tasks such as insertions, removals, and searches. Lists track logical size and grow or shrink automatically. 14 14

Using Lists (continued) Should I Use Arrays or Lists? (cont): Arrays could be used interchangeably with lists in some situations: You know the exact number of data elements to be inserted in advance, and they are known at startup, and never removed. The operations on the sequence are more specialized that those provided by a list, or consist of simple traversals. 15 15

Using Lists (continued) Linked Lists: LinkedList class can be used wherever ArrayList is used. Array lists and linked lists are logically the same, but differ in run-time characteristics. Array lists run in constant time. Linked lists run in linear time. LinkedList class includes methods for accesses, insertions, and removals. 16 16