Lecture 9: Lists MIT-AITI Kenya 2005. © 2005 MIT-Africa Internet Technology Initiative In this lecture we will learn…. ArrayList – These are re-sizeable.

Slides:



Advertisements
Similar presentations
Why not just use Arrays? Java ArrayLists.
Advertisements

Introduction to Java 2 Programming Lecture 5 The Collections API.
11 Copyright © 2005, Oracle. All rights reserved. Using Arrays and Collections.
List Implementations That Use Arrays
Lists and the Collection Interface Chapter 4. Chapter Objectives To become familiar with the List interface To understand how to write an array-based.
ArrayLists The lazy mans array. Whats the matter here? int[] list = new int[10]; list[0] = 5; list[2] = hey; list[3] = 15; list[4] = 23;
Data Structures A data structure is a collection of data organized in some fashion that permits access to individual elements stored in the structure This.
Introduction to Linked Lists In your previous programming course, you saw how data is organized and processed sequentially using an array. You probably.
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:
Loops Notes adapted from Dr. Flores. It repeats a set of statements while a condition is true. while (condition) { execute these statements; } “while”
List Implementations That Use Arrays Chapter 5. 2 Chapter Contents Using a Fixed-Size Array to Implement the ADT List An Analogy The Java Implementation.
Lists and the Collection Interface Chapter 4. Chapter 4: Lists and the Collection Interface2 Chapter Objectives To become familiar with the List interface.
Iterators Chapter 7. Chapter Contents What is an Iterator? A Basic Iterator Visits every item in a collection Knows if it has visited all items Doesn’t.
1 Arrays  Arrays are objects that help us organize large amounts of information  Chapter 8 focuses on: array declaration and use passing arrays and array.
Fall 2007CS 2251 Lists and the Collection Interface Chapter 4.
©TheMcGraw-Hill Companies, Inc. Permission required for reproduction or display. Chapter 10 *Arrays with more than one dimension *Java Collections API.
Aalborg Media Lab 28-Jun-15 Software Design Lecture 8 “Arrays”
Arrays, Loops weeks 4-6 (change from syllabus for week 6) Chapter 4.
1 Collections Working with More than One Number or Data Type or Object.
Building Java Programs
Chapter 101 Dynamic Data Structures and Generics Chapter 10.
Chapter 10 2D Arrays Collection Classes. Topics Arrays with more than one dimension Java Collections API ArrayList Map.
Data Structures Data structures permit the storage of related data for use in your program. –Arrays.
Chapter 10Java: an Introduction to Computer Science & Programming - Walter Savitch 1 Chapter 10 l Vectors l Linked Data Structures Dynamic Data Structures.
Chapter 6Java: an Introduction to Computer Science & Programming - Walter Savitch 1 l Array Basics l Arrays in Classes and Methods l Programming with Arrays.
1 ArrayList  Array’s are limited because we need to know the size before we use them.  An ArrayList is an extension of an array that grows and shrinks.
ARRAYLIST.. Hazen High School. Vocabulary to Know ArrayList Generic Class ArrayList Operations ArrayList Methods ArrayList Searching For-Each Wrapper.
Copyright © 2002, Systems and Computer Engineering, Carleton University Patterns.ppt * Object-Oriented Software Development Part 11.
CSC 142 J(part 1) 1 CSC 142 The ArrayList class [Reading: chapter 10]
Lists Ellen Walker CPSC 201 Data Structures Hiram College.
111 © 2002, Cisco Systems, Inc. All rights reserved.
5-Aug-2002cse Arrays © 2002 University of Washington1 Arrays CSE 142, Summer 2002 Computer Programming 1
IMPLEMENTING ARRAYLIST – Part 2 COMP 103. RECAP  Abstract Classes – overview, details in 2 nd year  Implementing the ArrayList: size(), get(), set()
COMP 103 Linked Lists. 2 RECAP-TODAY RECAP  Linked Structures: LinkedNode  Iterating and printing Linked Nodes  Inserting and removing Linked Nodes.
LinkedList Many slides from Horstmann modified by Dr V.
Data structures Abstract data types Java classes for Data structures and ADTs.
1 Chapter 20 Lists, Stacks, Queues Lecture 7 Dr. Musab Zghoul برمجة هيكلية.
Linked List. Background Arrays has certain disadvantages as data storage structures. ▫In an unordered array, searching is slow ▫In an ordered array, insertion.
M180: Data Structures & Algorithms in Java Arrays in Java Arab Open University 1.
Chapter 4 Grouping Objects. Flexible Sized Collections  When writing a program, we often need to be able to group objects into collections  It is typical.
OBJECTS FOR ORGANIZING DATA -- As our programs get more sophisticated, we need assistance organizing large amounts of data. : array declaration and use.
JAVA COLLECTIONS M. TAIMOOR KHAN (ADAPTED FROM SWINBURNE NOTES)
CSE 143 Lecture 2 ArrayList reading: 10.1 slides created by Marty Stepp
CSS446 Spring 2014 Nan Wang.  To understand the implementation of linked lists and array lists  To analyze the efficiency of fundamental operations.
Arrays An array is a data object that can hold multiple objects, all of the same type. We can think of an array as a storage box which has multiple compartments.
©SoftMoore ConsultingSlide 1 Generics “Generics constitute the most significant change in the Java programming language since the 1.0 release.” – Cay Horstmann.
Week 2 - Friday.  What did we talk about last time?  Computing Big Oh.
Data Design and Implementation. Definitions Atomic or primitive type A data type whose elements are single, non-decomposable data items Composite type.
CSE 373: Data Structures and Algorithms Lecture 2: Queues.
UniMAP Sem2-10/11 DKT121: Fundamental of Computer Programming1 Arrays.
COMPUTER PROGRAMMING 2 ArrayLists. Objective/Essential Standard Essential Standard 3.00Apply Advanced Properties of Arrays Essential Indicator 3.02 Apply.
Week111 APCS-A: Java Arrays Continued (related information in Chapter 7 of Lewis & Loftus) November 16, 2005.
Introducing Arrays. Too Many Variables?  Remember, a variable is a data structure that can hold a single value at any given time.  What if I want to.
Array Size Arrays use static allocation of space. That is, when the array is created, we must specify the size of the array, e.g., int[] grades = new int[100];
Topic 13 Iterators. 9-2 Motivation We often want to access every item in a data structure or collection in turn We call this traversing or iterating over.
Arrays (part 2) 1 -Based on slides from Deitel & Associates, Inc. - Revised by T. A. Yang.
1 Linked List. Outline Introduction Insertion Description Deletion Description Basic Node Implementation Conclusion.
CSE 501N Fall ‘09 10: Introduction to Collections and Linked Lists 29 September 2009 Nick Leidenfrost.
CS-2851 Dr. Mark L. Hornick 1 Linked-List collections Structure and Implementation.
Chapter 9 Introduction to Arrays Fundamentals of Java.
Introduction to Java Collection. Java Collections What are they? –A number of pre-packaged implementations of common ‘container’ classes, such as LinkedLists,
 2016, Marcus Biel, ArrayList Marcus Biel, Software Craftsman
Lists and the Collection Interface Chapter 4. Chapter 4: Lists and the Collection Interface2 Chapter Objectives To become familiar with the List interface.
Introduction to Linked Lists
Programming in Java Lecture 11: ArrayList
14.1 The java.util Package.
Review: libraries and packages
Introduction to Java Collection
Presentation transcript:

Lecture 9: Lists MIT-AITI Kenya 2005

© 2005 MIT-Africa Internet Technology Initiative In this lecture we will learn…. ArrayList – These are re-sizeable arrays LinkedList brief overview Differences between Arrays and ArrayLists Casting Iterator method (briefly)

© 2005 MIT-Africa Internet Technology Initiative Review of Arrays Arrays are a simple Data Structure Arrays store a row of values of the same type: Primitive types (int, double, etc) int[] prices = new prices[10]; //This stores 10 different prices// Object types (Students, Cars, etc) Students[] aitiClass = new Students[70] //Each student in the class is a separate object//

© 2005 MIT-Africa Internet Technology Initiative Arrays Review Part 2 Access each value in an Array using the index; int[] primeNums = new int[20]; //first 20 prime numbers// primeNums[0] = 1; primeNums[3] = 5; Remember, Array indices start at 0. What was the main problem with Arrays? Array lengths could not be changed once declared.

© 2005 MIT-Africa Internet Technology Initiative Something better than Arrays? As we noticed in the GradeBook Lab, Arrays can be annoying to use due to their fixed length. We need something with a similar structure to that of Arrays, but which can be resized automatically (no more fixed length issues). We use the ArrayList Class!!!

© 2005 MIT-Africa Internet Technology Initiative ArrayList I In an ArrayList, the elements are stored internally as an Array. Elements in ArrayLists are stored as type Object Thus in order to take an element out of an ArrayList, we will need to cast it into the desired type (we will revisit Casting in future slides) Since ArrayList is a class, it has its own methods too…

© 2005 MIT-Africa Internet Technology Initiative ArrayList II get method is fast – it just retrieves the element at the specified index add method is slow – may have to create a larger array and copy over all the elements. Other methods in ArrayList class include: set method – change an entry size() method – count number of elements To create an ArrayList: ArrayList newList = new ArrayList();

© 2005 MIT-Africa Internet Technology Initiative ArrayList III ArrayLists are not in the core java language, they are stored in a package They must therefore be imported by typing import java.util.*; At the top of your class ArrayList ArrayLists have many functional methods that make it easy to use and flexible

© 2005 MIT-Africa Internet Technology Initiative ArrayLists versus Arrays ArraysArrayLists Stores any one type only Stores only objects (Can store different objects) Fixed lengthFlexible length Has indicesDoes not have indices Faster at retrieving its contents Slower at retrieving its contents

© 2005 MIT-Africa Internet Technology Initiative Linked List Data Structure In a linked list, each link stores an item and is connected to the next link The list holds a reference to the first (and maybe the last) element Linked ListLink 1Link 2Link n Item 1Item 2Item n...

© 2005 MIT-Africa Internet Technology Initiative LinkedList A LinkedList stores its elements internally in a linked list data structure (diagram on previous slide) e.g. LinkedList anotherList = new LinkedList(); add method is fast – it just appends a new link to the end of the list get method is slow – has to walk down the list retrieve the element at the specified index

© 2005 MIT-Africa Internet Technology Initiative iterator method Both the ArrayList and LinkedList classes have an iterator method iterator method returns an object of type Iterator We use the iterator to go sequentially through each of the elements in the list. e.g. for an ArrayList of cars; Iterator c = cars.iterator(); Now c contains all the elements of the cars ArrayList, each of type Iterator.

© 2005 MIT-Africa Internet Technology Initiative Iterator hasNext method returns the boolean true when there are more elements to iterate over next method returns the next element in the iteration What is the return type of the next method?

© 2005 MIT-Africa Internet Technology Initiative Casting I When an ArrayList uses the get method, the return value is of type Object We cast this return value from type Object into the actual type it should be Casting means forcing the type of a value to be changed We cast so as to use an object in a different way Casting can only be done if the object/primitive being cast is compatible

© 2005 MIT-Africa Internet Technology Initiative Casting II To make a cast, you put the target class name in () and place it before the reference to the object you want to cast e.g. double y = 3.14; int z = (int) y; Another example on next slide…

© 2005 MIT-Africa Internet Technology Initiative GradeBook ArrayList example class GradeBook { private ArrayList grades; public void printGrades() { for (int i =0; i<grades.get(i); i++){ Double g = (Double)grades.get(i); System.out.println(g.doubleValue()); }

© 2005 MIT-Africa Internet Technology Initiative ArrayList aList = new ArrayList(); //aList contains several Car objects (remember //the car object from yesterday?) Car whiteVan = aList.get(matatu); Quiz What are the advantages of using an ArrayList? What is wrong with the ArrayList implementation below? Casting

© 2005 MIT-Africa Internet Technology Initiative What have we learned in this lecture? How to manipulate ArrayLists Differences between ArrayLists and Arrays Casting (will be useful in future labs)