Presentation is loading. Please wait.

Presentation is loading. Please wait.

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)

Similar presentations


Presentation on theme: "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)"— Presentation transcript:

1 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) data type is defined using a class Abstract data type (ADT) – data type written using information hiding techniques (i.e. using an interface) Data structure – a collection of data and the relationships between them. A data structure has an implicit implementation. In ADS2 we look at different sorts of basic data structure. E.g. arrays, linked lists, binary trees. Some ADTs representing collections of data (e.g. stacks, queues, maps) are defined and various implementations of these given in terms of the basic data structures. Families of data structures (binary search trees, hash tables) are discussed. ADS2 Lecture 2

2 Definitions contd. Previous definitions are a bit fuzzy Basically an ADT is an abstract entity But a data structure has a fixed implementation (as well as structural properties). 2 In this lecture and lecture 3 we will look at 2 basic data structures: Arrays (revision) Linked lists (completely new!) While we are at it, we will revise the ArrayList ADS2 Lecture 2

3 3 Arrays (Revision). See also JP2 lectures 9-10 An array is a data structure used to process a collection of data that is all the same type (Savitch p. 334) Array is a class. But can access instance variable length directly: myArray.length. Why? Because length is created when the array is created, and declared to be public final int. Arrays are built into the java language. An array is an object. So declared using new operator. Declaring an array of base type: int[] a; a= new int[10]; Is equivalent to: int [ ] a = new int[10]; //1-d array of length 10 2-d array: int[][] b = new int[5][6] //2-d array with 5 rows //and 6 columns ADS2 Lecture 2

4 4 Copying arrays If a is array: And we set b=a If we write b[3]=5; Also sets a[3] to 5. 9488838219638218 a 948883519638218 a b 9488838219638218 a b If we had just wanted to make an exact copy of the array a and assign it to array b, use b=a.clone(); Clone method is built in method of any Java object. This time changing a does not affect b. ADS2 Lecture 2

5 5 Arrays of objects: BankAccount[ ] myAccounts = new BankAccount[100]; Is an array containing 100 objects of type BankAccount (as defined in JP2 lab sheet 3). Remember: array contains references to objects, not objects themselves. Example Read in 3 sets of bank account details and store in an array, myAccounts. Make a withdrawal of £250 from each account and output the new details. Read bank account details into myBankAccount1, myBankAccount2, myBankAccount3 do this: Set ith index to point to a copy of bankAccount thus: myAccounts[i] =new BankAccount(bankAccount) Using appropriate copy constructor. Make withdrawals on array elements. Dont do this: Set ith index to point to bankAccount thus: myAccounts[i]=bankAccount Make withdrawals on bankAccount itself. Not recommended. ADS2 Lecture 2

6 6 public class BankAccount { private String accountNumber; private String accountHolder; private double balance; /**constructor*/ public BankAccount(String an,String ah,double b){ accountNumber=an; accountHolder=ah; balance=b; } /**copy constructor */ public BankAccount(BankAccount mybankAccount){ accountNumber=mybankAccount.getAccountNumber(); accountHolder=mybankAccount.getAccountHolder(); balance=mybankAccount.getBalance(); } Accessors and mutators omitted (simplified) BankAccount class: ADS2 Lecture 2

7 7 (simplified) BankAccount class contd.: (all very familiar…) /** output double d in decminal currency */ //with width 1, 2 decimal places, floating point public static String moneyFormat(double d) { return String.format("£%1.2f", d); } /** make a withdrawal from account */ public void withdraw(double d) { balance -= d; } /** return string representing account details */ public String toString() { return accountNumber + " " + accountHolder + " " + moneyFormat(balance) + "\n"; } ADS2 Lecture 2

8 8 BankAccounts class: //import java.util.Arrays; //come back to this public class BankAccounts { public static final int MAX = 100; int size; BankAccount[] theAccounts; /** no parameter constructor */ public BankAccounts(){ size=MAX; theAccounts = new BankAccount[size]; } /** create BankAccounts with given size */ public BankAccounts(int n){ size=n; theAccounts = new BankAccount[size]; } /** copy constructor*/ // public BankAccounts(BankAccounts L){} // COME BACK TO THIS ADS2 Lecture 2

9 9 BankAccounts class contd: public void setAccount(int index, BankAccount b){ if (index = size){ System.out.println("Error: Illegal or unused index."); System.exit(1); } else theAccounts[index] = new BankAccount(b); } public void withdraw(int index, double d){ if (index = size){ System.out.println("Error: Illegal or unused index."); System.exit(1); } else theAccounts[index].withdraw(d); } public String toString(){ String s=""; for (BankAccount b : theAccounts) s=s+b; return s; } ADS2 Lecture 2

10 10 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)

11 11 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

12 12 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 See board

13 Sorting Arrays 13 Saw in JP2 lecture 10 how to sort Arrays using selection sort and insertion sort Java has built-in sorting algorithm Neither of the above Uses algorithm called Quicksort Quicksort is much faster We will learn about it later in the course Insertion sort and Selection sort both took about ½ n 2 comparisons Quicksort takes around n log 2 n (which is much smaller, when n is large) Need to first understand recursion and algorithm analysis techniques. ADS2 Lecture 2

14 java.util methods for arrays 14 Java provides a number of built-in methods for performing common tasks on arrays. They appear as static methods in java.util. Arrays class. (So must remember to import java.util.Arrays and prefix method calls with Arrays ). Some of the available methods are: equals(A,B) : returns boolean fill(A,x) : stores x into every cell of A sort(A) : sorts A using natural ordering of its elements toString(A) : returns String representation of A E.g. add a new method to BankAccounts class to check to see if two accounts are equal (see board) ADS2 Lecture 2

15 15 ArrayLists (see JP2 lecture 18) As a data structure, ArrayList is a generic storage device think of as an extendible array implemented by arrays Will see later arraylist ADT – dont 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

16 16 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

17 17 Some ArrayList methods (continued) /** 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) From JP2 ADS2 Lecture 2

18 Other features of the ArrayList 18 for each loop can also be used here (and for other Java collections for which an iterator has been declared – see much later on in the course! ) e.g. from Crate example (JP2) public void display(){ for (Crate c : stock) System.out.println( Crate with load + c.getLoad()); } ArrayList class is an example of generic class – actual type(s) must be supplied on instantiation. We will be using generics a lot in ADS2. E.g. our interfaces for ADTs will be generic. ADS2 Lecture 2

19 When to use an ArrayList 19 Use when an array would be suitable, but number of elements varies. Time-consuming aspects of implementations include the shifting of elements up or down to keep occupied cells in array contiguous (e.g. after deletions) Replacing array with larger one when space exceeded There are more efficient implementations of the arraylist ADT, which we will see later. Examples you have seen: Crates (JP2 lecture 18), Student data (lab sheet 9), LabGroups (lab (exam) sheet 10). 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

20 Explanation of BankAccount example (see copy constructors slide 10) ADS2 Lecture 220 1. myAccounts2.theAccounts = myAccounts1.getTheAccounts(); myAccounts1: size = 3 theAccounts = bankAccount1bankAccount2bankAccount3 1456 Michael Smith 300 1457 Jane Edwards 1000 1458 Sam Forge 2000 myAccounts2: size = 3 theAccounts = Withdrawing 250 from myAccounts1.theAccounts[i], 0 i 2 changes bankAccount1, bankAccount2 and bankAccount3. Will clearly affect myAccounts2.theAccounts too.

21 ADS2 Lecture 221 2. myAccounts2.theAccounts = myAccounts1.clone() myAccounts1: size = 3 theAccounts = bankAccount1bankAccount2bankAccount3 1456 Michael Smith 300 1457 Jane Edwards 1000 1458 Sam Forge 2000 myAccounts2: size = 3 theAccounts = Withdrawing 250 from myAccounts1.theAccounts[i], 0 i 2 changes bankAccount1, bankAccount2 and bankAccount3. Will clearly affect myAccounts2.theAccounts too. bankAccount1bankAccount2bankAccount3 1456 Michael Smith 300 1457 Jane Edwards 1000 1458 Sam Forge 2000

22 ADS2 Lecture 222 In both cases output is: The final bank accounts are: myAccounts1: 1456 Michael Smith £50.00 1457 Jane Edwards £750.00 1458 Sam Forge £1750.00 myAccounts2: 1456 Michael Smith £50.00 1457 Jane Edwards £750.00 1458 Sam Forge £1750.00 public BankAccounts(BankAccounts L){ theAccounts = new BankAccount[L.getSize()]; size = L.getSize(); for (int i=0;i { "@context": "http://schema.org", "@type": "ImageObject", "contentUrl": "http://images.slideplayer.com/3/776265/slides/slide_22.jpg", "name": "ADS2 Lecture 222 In both cases output is: The final bank accounts are: myAccounts1: 1456 Michael Smith £50.00 1457 Jane Edwards £750.00 1458 Sam Forge £1750.00 myAccounts2: 1456 Michael Smith £50.00 1457 Jane Edwards £750.00 1458 Sam Forge £1750.00 public BankAccounts(BankAccounts L){ theAccounts = new BankAccount[L.getSize()]; size = L.getSize(); for (int i=0;i


Download ppt "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)"

Similar presentations


Ads by Google