P. 1
Array List

Array List

|Views: 2|Likes:
Published by ggardu

More info:

Published by: ggardu on Feb 14, 2012
Copyright:Attribution Non-commercial

Availability:

Read on Scribd mobile: iPhone, iPad and Android.
download as PPT, PDF, TXT or read online from Scribd
See more
See less

02/14/2012

pdf

text

original

Lists and the Collection Interface

Chapter 2

The List Interface and ArrayList Class
‡ So far, all we have is an array for storing a collection of elements. ‡ An array is an indexed structure: can select its elements in arbitrary order using a subscript value ‡ Elements may be accessed in sequence using a loop that increments the subscript ‡ You cannot ‡ Increase or decrease the length ‡ Add an element at a specified position without shifting the other elements to make room ‡ Remove an element at a specified position without shifting other elements to fill in the resulting gap
2

List) include: ‡ Finding a specified target ‡ Adding an element to either end or in the middle ‡ Removing an item from either end or from the middle ‡ Traversing the list structure without a subscript ‡ Not all classes implementing the List interface perform the allowed operations with the same degree of efficiency 3 .The List Interface ‡ Some of the allowed operations on the List interface (java.util.

The List Interface and ArrayList Class 4 .

//each element is an Object 5 .The ArrayList Class ‡ Simplest class that implements the List interface ‡ Improvement over an array object ‡ expandable! (ans shrinkable!) ‡ Mostly used when a programmer wants to add new elements to the end of a list but still needs the capability to access the elements stored in the list in arbitrary order ‡ How to construct and ArrayList? ArrayList myList = new ArrayList().

The ArrayList Class ‡ How to construct and ArrayList? (Java 5. //each element is a String 6 .0) ArrayList<String> myList = new ArrayList<String>().

//returns 4 for the ArrayList below 7 .The ArrayList Class ‡ size ‡ int s = myList.size().

³Doc´). 8 .add(³Dopey´). ‡ Add at the end myList.add(2.The ArrayList Class ‡ Add at a specified index myList.

³Doc´). 9 .add(8.The ArrayList Class ‡ Illegal if index > size() myList.

10 .remove(1).The ArrayList Class (continued) ‡ Remove at a certain index myList.

³Sneezy´).get(i). ³Sneezy´).set(2.set(6. //illegal if index > size()-1 11 .The ArrayList Class (continued) ‡ cannot index like an array myList[i] ! //illegal ‡ use myList. myList. myList.

The ArrayList Class (continued) ‡ Search using myList. // returns -1 12 . // returns 2 myList.indexOf(³Jumpy´).indexOf(³Sneezy´).

Specification of the ArrayList Class 13 .

4.get(1).2 ‡ ArrayList stores items of type Object and can thus store an object of any class ‡ You cannot store values of the primitive types directly but must instead use wrapper classes Example?? ‡ When an object is stored in an ArrayList. 14 .Application of ArrayList ‡ The ArrayList gives you additional capability beyond what an array provides ‡ For Java 1. the programmer must remember the original type String entry = (String) myList.

String entry = myList.0 and higher: ‡ You cannot store values of the primitive types directly but must instead use wrapper classes. 15 . //autounboxing ‡ No casting required when retrieving from the ArrayList ArrayList<String> C = new ArrayList<String>(). //autoboxing int temp = B.add(³Hello´).add(42). C.get(0).get(0). B. However. autoboxing/autounboxing are in effect ArrayList <Integer> B = new ArrayList<Integer>().Application of ArrayList ‡ For Java 5.

Recall PhoneDirectory? ‡ Instance variable theDirectory private DirectoryEntry [ ] theDirectory = new DirectoryEntry[capacity]. theDirectory[index]. if (index > -1) { oldNumber = theDirectory[index]. } else { add(name.getNumber(). } modified = true. String number) { String oldNumber = null. return oldNumber.setNumber(number). ‡ ArrayBased addOrChangeEntry method public String addOrChangeEntry(String name. } 16 . int index = find(name). number).

³´)). addOrChangeEntry method public String addOrChangeEntry(String name. } else { theDirectory.getNumber().indexOf(new DirectoryEntry(name. de. String number) { String oldNumber = null. } modified = true. int index = theDirectory. //assuming DirectoryEntry class has an equals method that compares entries by name if (index != -1) { DirectoryEntry de = theDirectory. oldNumber = de. number)).setNumber(number).get(index). return oldNumber.add(new DirectoryEntry(name.Using an ArrayList in PhoneDirectory ‡ Instance variable theDirectory private ArrayList<DirectoryEntry> theDirectory = new ArrayList<DirectoryEntry>(). } 17 ‡ .

Implementation of an ArrayList Class ‡ KWArrayList: simple implementation of a ArrayList class ‡ Physical size of array indicated by data field capacity ‡ Number of data items indicated by the data field size 18 .

Implementation of an ArrayList Class (continued) 19 .

Performance of KWArrayList and the Vector Class ‡ Set and get methods execute in constant time ‡ Inserting or removing elements is linear time ‡ Initial release of Java API contained the Vector class which has similar functionality to the ArrayList ‡ Both contain the same methods ‡ New applications should use ArrayList rather than Vector 20 .

You're Reading a Free Preview

Download
scribd
/*********** DO NOT ALTER ANYTHING BELOW THIS LINE ! ************/ var s_code=s.t();if(s_code)document.write(s_code)//-->