CS 116 OBJECT ORIENTED PROGRAMMING II LECTURE 6 GEORGE KOUTSOGIANNAKIS Copyright: 2016 Illinois Institute of Technology/George Koutsogiannakis 1.

Slides:



Advertisements
Similar presentations
The ArrayList Class and the enum Keyword
Advertisements

Java POWERED BY: ARVIND DEPARTMENT OF COMPUTER SCIENCE AND ENGINEERING RADHA GOVIND GROUP OF INSTITUTIONS,MEERUT.
OO Programming Objectives for today: Casting Objects Introduction to Vectors The instanceof keyword.
5-May-15 ArrayLists. 2 ArrayList s and arrays A ArrayList is like an array of Object s Differences between arrays and ArrayList s: Arrays have special.
Slides prepared by Rose Williams, Binghamton University Chapter 14 Generics and the ArrayList Class.
For use of Cleveland State's IST410 Students only 1 Vectors and Collections.
©2004 Brooks/Cole Chapter 8 Arrays. Figures ©2004 Brooks/Cole CS 119: Intro to JavaFall 2005 Sometimes we have lists of data values that all need to be.
1 Dynamic Arrays  Why Dynamic Arrays?  A Dynamic Array Implementation  The Vector Class  Program Example  Array Versus Vector.
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.
1 Chapter 2 Introductory Programs. 2 Getting started To create and run a Java program –Create a text file with a.java extension for the source code. For.
Slides prepared by Rose Williams, Binghamton University Chapter 14 Generics and the ArrayList Class.
Chapter 14 Generics and the ArrayList Class Copyright © 2010 Pearson Addison-Wesley. All rights reserved.
Building Java Programs
15-Jul-15 Generics. ArrayList s and arrays A ArrayList is like an array of Object s, but... Arrays use [ ] syntax; ArrayList s use object syntax An ArrayList.
CSE373 Optional Section Java Collections 11/12/2013 Luyi Lu.
Announcements  I will discuss the labtest and the written test #2 common mistakes, solution, etc. in the next class  not today as I am still waiting.
By Nicholas Policelli An Introduction to Java. Basic Program Structure public class ClassName { public static void main(String[] args) { program statements.
CSC 142 J(part 1) 1 CSC 142 The ArrayList class [Reading: chapter 10]
ArrayList, Multidimensional Arrays
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.
Chapter 9 Multidimensional Arrays and the ArrayList Class.
CSE 143 Lecture 4 ArrayList Reading: 10.1 slides created by Marty Stepp
Chapter 14 Generics and the ArrayList Class Slides prepared by Rose Williams, Binghamton University Copyright © 2008 Pearson Addison-Wesley. All rights.
Copyright 2008 by Pearson Education Building Java Programs ArrayList Reading: 10.1.
OBJECTS FOR ORGANIZING DATA -- As our programs get more sophisticated, we need assistance organizing large amounts of data. : array declaration and use.
Chapter overview This chapter focuses on Array declaration and use Bounds checking and capacity Arrays storing object references Variable length parameter.
1 Generics Chapter 21 Liang, Introduction to Java Programming.
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.
Lecture 121 CS110 Lecture 12 Tuesday, March 9, 2004 Announcements –hw5 due Thursday –Spring break next week Agenda –questions –ArrayList –TreeMap.
CS 115 OBJECT ORIENTED PROGRAMMING I LECTURE 9 GEORGE KOUTSOGIANNAKIS Copyright: 2014 Illinois Institute of Technology- George Koutsogiannakis 1.
CS 116 OBJECT ORIENTED PROGRAMMING II LECTURE 12 GEORGE KOUTSOGIANNAKIS Copyright: 2015 Illinois Institute of Technology/ George Koutsogiannakis 1.
CSE 1201 Object Oriented Programming ArrayList 1.
An Introduction to Java – Part 1 Erin Hamalainen CS 265 Sec 001 October 20, 2010.
CS 116 OBJECT ORIENTED PROGRAMMING II LECTURE 5 GEORGE KOUTSOGIANNAKIS Copyright: 2016 Illinois Institute of Technology/George Koutsogiannakis 1.
CS 115 OBJECT ORIENTED PROGRAMMING I LECTURE 12 GEORGE KOUTSOGIANNAKIS 1 Copyright: 2015 Illinois Institute of Technology_ George Koutsogiannakis.
CS1020 Data Structures and Algorithms I Lecture Note #6 Vector and ArrayList.
CS 116: Object Oriented Programming II. Topics Vectors Multidimensional Arrays ArrayList.
The ArrayList Data Structure The Most Important Things to Review.
CS 116 OBJECT ORIENTED PROGRAMMING II LECTURE 4 GEORGE KOUTSOGIANNAKIS Copyright: 2016 Illinois Institute of Technology/George Koutsogiannakis 1.
1 The copy constructor in the BankAccounts class. Two alternatives here: /** copy constructor */ public BankAccounts(BankAccounts L){ theAccounts = L.theAccounts.clone();
CS 115 OBJECT ORIENTED PROGRAMMING I LECTURE 11 GEORGE KOUTSOGIANNAKIS 1 Copyright: 2015 Illinois Institute of Technology_ George Koutsogiannakis.
19-Mar-16 Collections and ArrayLists.. 2 Collections Why use Collections. Collections and Object-Orientation. ArrayLists. Special Features. Creating ArrayLists.
Chapter 9 Introduction to Arrays Fundamentals of Java.
CS202 Java Object Oriented Programming Introduction to Collection Classes Chengyu Sun California State University, Los Angeles.
EKT472: Object Oriented Programming
OBJECT ORIENTED PROGRAMMING II GEORGE KOUTSOGIANNAKIS
Chapter VII: Arrays.
Sixth Lecture ArrayList Abstract Class and Interface
CS 116 OBJECT ORIENTED PROGRAMMING II MIDTERM EXAM INFORMATION
OBJECT ORIENTED PROGRAMMING II LECTURE 15 GEORGE KOUTSOGIANNAKIS
Fundamentals of Java: AP Computer Science Essentials, 4th Edition
OBJECT ORIENTED PROGRAMMING II LECTURE 21_1 GEORGE KOUTSOGIANNAKIS
OBJECT ORIENTED PROGRAMMING II GEORGE KOUTSOGIANNAKIS
OBJECT ORIENTED PROGRAMMING II GEORGE KOUTSOGIANNAKIS
Welcome to CSE 143!.
Array List Pepper.
OBJECT ORIENTED PROGRAMMING II LECTURE 8 GEORGE KOUTSOGIANNAKIS
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.
Welcome to CSE 143!.
Arrays and Collections
Object Oriented Programming in java
OBJECT ORIENTED PROGRAMMING II LECTURE 11_1 GEORGE KOUTSOGIANNAKIS
OBJECT ORIENTED PROGRAMMING I LECTURE 9 GEORGE KOUTSOGIANNAKIS
OBJECT ORIENTED PROGRAMMING II LECTURE 13_2 GEORGE KOUTSOGIANNAKIS
OBJECT ORIENTED PROGRAMMING I LECTURE 11 GEORGE KOUTSOGIANNAKIS
OBJECT ORIENTED PROGRAMMING II LECTURE 20 GEORGE KOUTSOGIANNAKIS
Presentation transcript:

CS 116 OBJECT ORIENTED PROGRAMMING II LECTURE 6 GEORGE KOUTSOGIANNAKIS Copyright: 2016 Illinois Institute of Technology/George Koutsogiannakis 1

Last lecture’s topics Binary Search of an array. Using Arrays as counters. Vectors. Multi-dimentional Arrays 2

This lecture’ s topics ArrayList 3

The ArrayList Class Arrays have a fixed size once they have been instantiated. What if we don't know how many elements we will need? For example, if we are reading values from a file returning search results We could create a very large array, but then we waste space for all unused elements. A better idea is to use an ArrayList, which stores elements of object references and automatically expands its size, as needed. 4

The ArrayList Class The ArrayList class is in the package: java.util All ArrayList elements are object references, so we could have an ArrayList of Auto objects, Book objects, Strings, etc. To store primitive types in an ArrayList, use the wrapper classes (Integer, Double, Character, Boolean, etc.) The ArrayList is a generic class. The ArrayList class has been written so that it can store object references of any type specified by the client. 5

Declaring an ArrayList Use this syntax: ArrayList arrayListName; E is a class name that specifies the type of object references that will be stored in the ArrayList. Example: ArrayList listOfStrings; ArrayList listOfCars; ArrayList listOfInts; 6

ArrayList Constructors The capacity of an ArrayList is the total number of elements allocated to the list. The size of an an ArrayList is the number of elements that are used. Constructor name and argument list ArrayList constructs an ArrayList object of type E with an initial capacity of 10 ArrayList ( int initialCapacity ) constructs an ArrayList object of type E with the specified initial capacity

Instantiating an ArrayList This list has a capacity of 10 Astronaut references (default capacity), but a size of 0. ArrayList listOfAstronauts = new ArrayList ( ); This list has a capacity of 5 Strings, but a size of 0. ArrayList listOfStrings = new ArrayList ( 5 ); 8

ArrayList Methods Return valueMethod name and argument list booleanadd( E element ) appends element to the end of the list voidclear( ) removes all the elements in the list intsize( ) returns the number of elements in the list Eremove( int index ) removes the element at the specified index position

More ArrayList Methods Return valueMethod name and argument list Eget( int index ) returns the element at the specified index position; the element is not removed from the list. Eset( int index, E element ) replaces the element at the specified index position with the specified element voidtrimToSize( ) sets the capacity of the list to its current size

Processing Array Lists Using a standard for loop: ClassName currentObject; for ( int i = 0; i < arrayListName.size( ); i++ ) { currentObject = arrayListName.get( i ); // process currentObject } Example: Auto currentAuto; for ( int i = 0; i < listOfAutos.size( ); i++ ) { currentAuto = listOfAutos.get( i ); // process currentAuto } 11

Processing Array Lists Example of a method definition that returns an ArrayList: public ArrayList myMethod() Example of setting an object into the list: ArrayList list=new ArrayList (); list.set(0, obj); //sets at index 0 the object obj 12

The Enhanced for Loop Simplifies processing of lists The standard form is: for ( ClassName currentObject : arrayListName ) { // process currentObject } This enhanced for loop prints all elements of an ArrayList of Strings named list: for ( String s : list ) { System.out.println( s ); } 13

Study Guide Chapter 9 – Sections