1 The copy constructor in the BankAccounts class. Two alternatives here: /** copy constructor */ public BankAccounts(BankAccounts L){ theAccounts = L.theAccounts.clone();

Slides:



Advertisements
Similar presentations
1 ADS2 Lecture 2 : Review of arrays and ArrayLists Some definitions: Data type - a set of values and methods to define an object. In java a (non-primitive)
Advertisements

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.
Copyright © 2007 Pearson Education, Inc. Publishing as Pearson Addison-Wesley Starting Out with Java From Control Structures through Data Structures by.
CSC 205 – Java Programming II Lecture 25 March 8, 2002.
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.
CS 106 Introduction to Computer Science I 04 / 30 / 2007 Last lecture :( Instructor: Michael Eckmann.
An Array-Based Implementation of the ADT List public class ListArrayBased implements ListInterface { private static final int MAX_LIST = 50; private Object.
CSE 143 Lecture 22: Advanced List Implementation (ADTs; interfaces; abstract classes; inner classes; generics; iterators)
CS 106 Introduction to Computer Science I 04 / 27 / 2007 Instructor: Michael Eckmann.
Lists Chapter 4. 2 Chapter Contents Specifications for the ADT List Redefining the Specifications Using the ADT List Using a List Is Like Using a Vending.
CS 106 Introduction to Computer Science I 05 / 03 / 2010 Instructor: Michael Eckmann.
For use of Cleveland State's IST410 Students only 1 Vectors and Collections.
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.
1 Dynamic Arrays  Why Dynamic Arrays?  A Dynamic Array Implementation  The Vector Class  Program Example  Array Versus Vector.
24-Jun-15 Introduction to Collections. 2 Collections A collection is a structured group of objects Java 1.2 introduced the Collections Framework Collections.
CHAPTER 6 Stacks Array Implementation. 2 Stacks A stack is a linear collection whose elements are added and removed from one end The last element to be.
Unit 291 Java Collections Framework: Interfaces Introduction to the Java Collections Framework (JCF) The Comparator Interface Revisited The Collection.
Vectors. Vectors and arrays A Vector is like an array of Object s Differences between arrays and Vector s: –Arrays have special syntax; Vector s don’t.
Lists in Java Part of the Collections Framework. Kinds of Collections Collection --a group of objects, called elements –Set-- An unordered collection.
CS 106 Introduction to Computer Science I 04 / 30 / 2010 Instructor: Michael Eckmann.
Copyright © Texas Education Agency, Advanced Computer Programming Data Structures: Collections.
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.
CS 106 Introduction to Computer Science I 04 / 25 / 2007 Instructor: Michael Eckmann.
04/29/ Introduction to Vectors?... A vector is a dynamic array. - It can be expanded and shrunk as required - A Component of a vector can be accessed.
ArrayList, Multidimensional Arrays
Lists Ellen Walker CPSC 201 Data Structures Hiram College.
IMPLEMENTING ARRAYLIST – Part 2 COMP 103. RECAP  Abstract Classes – overview, details in 2 nd year  Implementing the ArrayList: size(), get(), set()
Data Design and Implementation. Definitions of Java TYPES Atomic or primitive type A data type whose elements are single, non-decomposable data items.
Lab 7 Queue ADT. OVERVIEW The queue is one example of a constrained linear data structure. The elements in a queue are ordered from least recently added.
Arrays Construct array: new double[10] Store in variable of type double[] double[] data = new double[10];
CS 106 Introduction to Computer Science I 04 / 25 / 2008 Instructor: Michael Eckmann.
Lists Chapter 4. 2 Chapter Contents Specifications for the ADT List Redefining the Specifications Using the ADT List Java Class Library: The Interface.
Chapter overview This chapter focuses on Array declaration and use Bounds checking and capacity Arrays storing object references Variable length parameter.
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.
(c) University of Washington15-1 CSC 143 Java List Implementation via Arrays Reading: 13.
ArrayList By Neil Butcher. What is the difference between an ArrayList and an Array? An ArrayList is in many ways similar to an array, but has a few subtle.
CSE 143 Lecture 24 Advanced collection classes (ADTs; abstract classes; inner classes; generics; iterators) read 11.1, 9.6, , slides.
(c) University of Washington16-1 CSC 143 Java Lists via Links Reading: Ch. 23.
Lists Chapter 4. 2 Chapter Contents Specifications for the ADT List Redefining the Specifications Using the ADT List Using a List Is Like Using a Vending.
Click to edit Master title style Click to edit Master text styles Second level Third level Fourth level Fifth level 1 ArrayLists Section 9.9.
IMPLEMENTING ARRAYLIST COMP 103. RECAP  Comparator and Comparable  Brief look at Exceptions TODAY  Abstract Classes - but note that the details are.
Object-Oriented Programming (Java) Review Unit 1 Class Design Basic Console I/O StringTokenizer Exception UML class diagram.
CS1020 Data Structures and Algorithms I Lecture Note #6 Vector and ArrayList.
Implementing ArrayList Part T2 Lecture 6 School of Engineering and Computer Science, Victoria University of Wellington  Thomas Kuehne, Marcus Frean,
19-Mar-16 Collections and ArrayLists.. 2 Collections Why use Collections. Collections and Object-Orientation. ArrayLists. Special Features. Creating ArrayLists.
Quiz: Design a Product Class Create a definition for a class called Product, which keeps track of the following information: –Name of the product –Weight.
 2016, Marcus Biel, ArrayList Marcus Biel, Software Craftsman
1 CS162: Introduction to Computer Science II Abstract Data Types.
An Array-Based Implementation of the ADT List
Lists Chapter 4.
COP 3503 FALL 2012 Shayan Javed Lecture 8
Implementing ArrayList Part 1
Introduction to OO Program Design
TCSS 143, Autumn 2004 Lecture Notes
Programming in Java Lecture 11: ArrayList
Copyright ©2012 by Pearson Education, Inc. All rights reserved
(Java Collections Framework) AbstractSequentialList
ArrayList Collections.
L2. Necessary Java Programming Techniques
The Vector Class An object of class Vector is similar to an array in that it stores multiple values However, a vector only stores objects does not have.
ArrayLists 22-Feb-19.
Collections Framework
Introduction to Collections
Programming II (CS300) Chapter 02: Using Objects Java ArrayList Class
ArrayLists.
ArrayLists 27-Apr-19.
Copyright ©2012 by Pearson Education, Inc. All rights reserved
Presentation transcript:

1 The copy constructor in the BankAccounts class. Two alternatives here: /** copy constructor */ public BankAccounts(BankAccounts L){ theAccounts = L.theAccounts.clone(); size=L.size; } /** alternative copy constructor */ public BankAccounts(BankAccounts L){ theAccounts = L.theAccounts; size = L.size; } or Will examine their effects using TestBankAccounts programme: ADS2 Lecture 2 (a) (b) Handout for Lecture 2. SLIDE 10

2 public class TestBankAccounts { public static void main(String[] args) { // Test BankAccount Class BankAccount bankAccount1=new BankAccount("1456","Michael Smith",300); BankAccount bankAccount2=new BankAccount("1457","Jane Edwards",1000); BankAccount bankAccount3=new BankAccount("1458","Sam Forge",2000); BankAccounts myAccounts1 = new BankAccounts(3); myAccounts1.setAccount(0,bankAccount1); myAccounts1.setAccount(1,bankAccount2); myAccounts1.setAccount(2,bankAccount3); BankAccounts myAccounts2=new BankAccounts(myAccounts1); myAccounts1.withdraw(0,250.0); myAccounts1.withdraw(1,250.0); myAccounts1.withdraw(2,250.0); * (ignore asterisks for now) ADS2 Lecture 2 SLIDE 11

3 System.out.println("The final bank accounts are: "); System.out.println("myAccounts1: "); System.out.println(myAccounts1); System.out.println("myAccounts2: "); System.out.println(myAccounts2); } Exercise: What is the output if we use the first copy constructor on slide 10? What is the output if we use the second copy constructor on slide 10? ** ADS2 Lecture 2 SLIDE 12

5 As a data structure, ArrayList is a generic storage device think of as an extendible array implemented by arrays Will see later arraylist ADT – don’t assume implemented using array. ArrayList is class in standard Java – need import java.util.ArrayList ; For an array, size is fixed at declaration: theAccounts = new BankAccount[3]; Initialises an array of size 3 of BankAccounts objects For an ArrayList specify type of entry and initial size at declaration: ArrayList list = new ArrayList (20); If capacity exceeds 20, new array is built. Note base type must be reference type (not primitive type). If you want to use a primitive type, must use wrapper class (e.g. Integer for int). ADS2 Lecture 2 SLIDE 15

6 Some ArrayList methods /** Create ArrayList of specified Base-Type and initialCapacity */ public ArrayList (int initialCapacity) /** Create ArrayList of specified Base-Type and init capacity 10 */ public ArrayList () /** Return element at specified index */ public Base-Type get(int index) /** Set the element at specified index to newElement and return * the value previously at that position; (often used as if void)*/ public Base-Type set(int index, Base-Type newElement) /** Add newElement at position index, moves elements to make room, * and increments size;increases capacity if necessary */ public void add(int index, Base-Type newElement) /** Add newElement at position size and increments size; * (increases capacity if necessary) */ public void add(Base-Type newElement) From JP2 ADS2 Lecture 2 SLIDE 16

7 /** Remove and return element at specified index; move elements * down to close the gap */ public Base-Type remove(int index) /** return the number of elements in the ArrayList*/ public int size() /** return true if the ArrayList is empty, and false otherwise*/ public boolean isEmpty() /** make the ArrayList empty */ public void clear() /** trim the capacity of the ArrayList to its current size */ public void trimToSize() /** Return the index of the first occurrence of o in the ArrayList, or -1 if this list does not contain the element. */ public int indexOf(Object o) /** Remove the first occurrence of o from the ArrayList, if it is present; returns true if it was removed, false otherwise */ public boolean remove(Object o) ADS2 Lecture 2 SLIDE 17

8 Example for you: replace code in Bank Accounts example between * and ** with code in which: ArrayList myAccounts of size 3 is initialised to take data of type BankAccount bankAccount1.. bankAccount3 are added to the list Set element at index 0 to bankAccount4 = (“1455”,”Edwin Moore”,600) Remove entry with index 2 Contents of myAccounts printed to standard output. What is output? ADS2 Lecture 2 SLIDE 19