ARRAYLIST Collections of Data. ArrayLists Array lists can grow and shrink as needed ArrayList is a generic class (similar to C++ template) ArrayList has.

Slides:



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

Review Generics and the ArrayList Class
Lists and the Collection Interface Chapter 4. Chapter Objectives To become familiar with the List interface To understand how to write an array-based.
Week 10 Introduction to Computer Science and Object-Oriented Programming COMP 111 George Basham.
Chapter 7: Arrays and Array Lists. To become familiar with using arrays and array lists To learn about wrapper classes, auto-boxing and the generalized.
Big Java by Cay Horstmann Copyright © 2008 by John Wiley & Sons. All rights reserved. Chapter Seven: Arrays and Array Lists.
Chapter 7 Arrays and Array Lists. Chapter Goals To become familiar with using arrays and array lists To learn about wrapper classes, auto-boxing and the.
Chapter 7 – Arrays.
Computer Science A 10: 20/3. Array Array: Sequence of values of the same type Construct array: new double[10] Store in variable of type double[] double[]
Chapter 7  Arrays and Array Lists 1 Chapter 7 Arrays and Array Lists.
For use of Cleveland State's IST410 Students only 1 Vectors and Collections.
Using final We use the notion of constant data to represent data that cannot be changed. public class Test { static final int someInt = 10; static final.
Using ArrayList. Lecture Objectives To understand the foundations behind the ArrayList class Explore some of the methods of the ArrayList class.
Datalogi A 8: 27/10. Array Array: Sequence of values of the same type Construct array: new double[10] Store in variable of type double[] double[] data.
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.
Chapter 14 Generics and the ArrayList Class Copyright © 2010 Pearson Addison-Wesley. All rights reserved.
Fall 2007CS 2251 Lists and the Collection Interface Chapter 4.
Static Class Members Wrapper Classes Autoboxing Unboxing.
Comparing Objects in Java. The == operator When you define an object, for instance Person p = new Person("John", 23); we talk about p as if its value.
ARRAYS AND ARRAYLISTS Chapter 7. Array  Sequence of values of the same type  Primitive types  Objects  Create an Array  double[] values = new double[10]
Java Programming Week 6: Array and ArrayList Chapter 7.
For each primitive type there is a wrapper class for storing values of that type: Double d = new Double(29.95); Wrapper Classes Wrapper objects can be.
Week 9-10 – Arrays and Array Lists Big Java by Cay Horstmann Copyright © 2009 by John Wiley & Sons. All rights reserved.
Chapter 7 Objects and Memory. Structure of memory The fundamental unit of memory is called a bit, either 0 or 1. In most modern architectures, the smallest.
P Object type and wrapper classes p Object methods p Generic classes p Interfaces and iterators Generic Programming Data Structures and Other Objects Using.
Week 10 Introduction to Computer Science and Object-Oriented Programming COMP 111 George Basham.
AP CS Workshop ArrayList It is very common for applications to require us to store a large amount of data. Array lists store large amounts of data.
GENERIC COLLECTIONS. Type-Wrapper Classes  Each primitive type has a corresponding type- wrapper class (in package java.lang).  These classes are called.
CSC 142 J(part 1) 1 CSC 142 The ArrayList class [Reading: chapter 10]
Big Java Chapter 7-8 (ArrayList / Class Design) 2009.OOP.r1.
Lists Ellen Walker CPSC 201 Data Structures Hiram College.
Chapter 11 Arrays Continued
5-Aug-2002cse Arrays © 2002 University of Washington1 Arrays CSE 142, Summer 2002 Computer Programming 1
© 2007 Lawrenceville Press Slide 1 Chapter 10 Arrays  Can store many of the same kind of data together  Allows a collection of related values to be stored.
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 18 Java Collections Framework
CompSci Arrays  Aggregate data type  Deal with items of same type  Lists of words  Numbers  Analogies  Mailboxes in post office  CD racks.
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.
Fall 2006Slides adapted from Java Concepts companion slides1 Arrays and Array Lists Advanced Programming ICOM 4015 Lecture 7 Reading: Java Concepts Chapter.
CIS3023: Programming Fundamentals for CIS Majors II Summer 2010 Ganesh Viswanathan Generics and Collections Course Lecture Slides 19 th July 2010 “Never.
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.
Arrays…JavaCPython have fixed lengthyes*yesno are initialized to default values yesno? track their own lengthyesnoyes trying to access “out of bounds”
CSC 1051 M.A. Papalaskari, Villanova University Everyday objects: Strings and Wrappers CSC 1051 – Data Structures and Algorithms I Dr. Mary-Angela Papalaskari.
Review and Java Questions 13 June Classes, objects, attributes, methods In Java, by convention a class name begins with Uppercase: Coffee, String.
Java Methods A & AB Object-Oriented Programming and Data Structures Maria Litvin ● Gary Litvin Copyright © 2006 by Maria Litvin, Gary Litvin, and Skylight.
©SoftMoore ConsultingSlide 1 Generics “Generics constitute the most significant change in the Java programming language since the 1.0 release.” – Cay Horstmann.
Arrays Adapted from materials created by Dr. Donald Bell, Cal Poly 2000 (updated February 2004)
Arrays and ArrayLists Topic 6. One Dimensional Arrays Homogeneous – all of the same type Contiguous – all elements are stored sequentially in memory For.
CSE 1201 Object Oriented Programming ArrayList 1.
Arrays. Array: Sequence of values of the same type Construct array: Store in variable of type double[ ] new double[10] double[] data = new double[10];
Week 9 Introduction to Computer Science and Object-Oriented Programming COMP 111 George Basham.
Wrapper Classes Use wrapper objects in Collections when you can’t use primitive types Primitive TypeWrapper Class byteByte shortShort intInteger longLong.
Week 9 Introduction to Computer Science and Object-Oriented Programming COMP 111 George Basham.
Coming up ArrayList ArrayList vs Array – Declaration – Insertion – Access – Removal Wrapper classes Iterator object.
Lecture 20: Wrapper Classes and More Loops
Principles of Computer Science I
Fundamentals of Java: AP Computer Science Essentials, 4th Edition
ARRAYLIST AND VECTOR.
Introduction to Computer Science and Object-Oriented Programming
Can store many of the same kind of data together
ArrayLists.
Can store many of the same kind of data together
Object Oriented Programming in java
ArrayLists 22-Feb-19.
Can store many of the same kind of data together
Presentation transcript:

ARRAYLIST Collections of Data

ArrayLists Array lists can grow and shrink as needed ArrayList is a generic class (similar to C++ template) ArrayList has variety of methods for common tasks, for example: ArrayList accounts = new ArrayList (); // insert using add method accounts.add(new BankAccount(1001)); accounts.add(new BankAccount(2010)); // retrieve using get method BankAccount anAccount = accounts.get(1); // find out how many items in list int count = accounts.size(); // 0 to size-1 are valid BankAccount account2 = new BankAccount(3000); // update the list accounts.set(1, account2); // overwrites accounts.add(0, new BankAccount(1200)); // index < size // remove an item accounts.remove(1); // access all items in order for (int i=0; i<accounts.size(); ++i) System.out.println(accounts.get(i).getBalance());

ArrayList (continued) With an unparameterized ArrayList, any type of object may be stored, but the object must be cast when retrieved: ArrayList accounts = new ArrayList(); accounts.add(new BankAccount(1500)); BankAccount acct = (BankAccount) accounts.get(0); Exception will occur if cast is incorrect. Much better practice to use generic class (warning in Eclipse if use unparameterized)

Wrappers and Auto-boxing Not in C++ (C++ evolved from C, Java designed from scratch as OO) Numbers are not objects, can’t place directly in ArrayLists All primitive types have “wrapper” classes Wrapper contains one value of corresponding primitive type Also have useful static methods (mostly conversions) PrimitiveWrapper byteByte booleanBoolean charCharacter doubleDouble floatFloat intInteger longLong shortShort

Wrappers and Auto-boxing (continued) Starting with Java 5.0, conversion between primitive types and wrapper classes is automatic, called auto-boxing (auto-wrapping would be more consistent!) Examples: Double d = 29.95; // Double d = new Double(29.95); double x = d; // auto-unbox, same as double x = d.doubleValue(); Double e = d + 1; // auto-unbox, add 1, auto-box ArrayList data = new ArrayList (); data.add(32.14); double x = data.get(0);

Efficiency Note Even though you don’t have to write the code, if you have a long sequence of primitive types (numbers or chars), use an array rather than an ArrayList for efficiency

Enhanced/Iterated for loop Convenient new syntax for iterating an array or ArrayList: double[] data = {... }; double sum = 0; for (double e : data) { sum = sum + e; } With ArrayLists: ArrayList accounts;... add, create sum for (BankAccount a : accounts) { sum += a.getBalance(); }

2D Arrays Similar to C++, but must always allocate memory final int ROWS = 3; final int COLS = 3; String[][] board = new String[ROWS][COLS];

More on Arrays etc. First choice will normally be ArrayList, unless storing large collection of primitives OR need 2D Use “for each” loop pattern when processing all elements To make a copy, use the clone method*: double [] data = new double[10];... double [] prices = (double []) data.clone(); Or use System.arraycopy if not copying the entire array (see book or API for details) Clone has some flaws (must call super.clone, issues depending on whether new is called, issues with CloneNotSupportedException). Probably OK to use for arrays, but copy constructor often preferred for other object. Details are beyond our scope… but read the following: recommended-in-java

Vector or ArrayList? The API for these is similar, but there are some differences in implementation. If you’re curious, check out: 5/java-se/vector-or-arraylist-which-is- better.html?null