Array List

download Array List

of 21

Transcript of Array List

  • Lists and the Collection InterfaceChapter 4

    Chapter 4: Lists and the Collection Interface

  • The List Interface and ArrayList ClassSo 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 valueElements may be accessed in sequence using a loop that increments the subscriptYou cannotIncrease or decrease the lengthAdd an element at a specified position without shifting the other elements to make roomRemove an element at a specified position without shifting other elements to fill in the resulting gap

    Chapter 4: Lists and the Collection Interface

  • The List InterfaceSome of the allowed operations on the List interface (java.util.List) include:Finding a specified targetAdding an element to either end or in the middleRemoving an item from either end or from the middleTraversing the list structure without a subscriptNot all classes implementing the List interface perform the allowed operations with the same degree of efficiency

    Chapter 4: Lists and the Collection Interface

  • The List Interface and ArrayList Class

    Chapter 4: Lists and the Collection Interface

  • The ArrayList ClassSimplest class that implements the List interfaceImprovement 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(); //each element is an Object

    Chapter 4: Lists and the Collection Interface

  • The ArrayList ClassHow to construct and ArrayList? (Java 5.0)ArrayList myList = new ArrayList(); //each element is a String

    Chapter 4: Lists and the Collection Interface

  • The ArrayList Classsizeint s = myList.size(); //returns 4 for the ArrayList below

    Chapter 4: Lists and the Collection Interface

  • The ArrayList ClassAdd at a specified indexmyList.add(2, Doc);Add at the endmyList.add(Dopey);

    Chapter 4: Lists and the Collection Interface

  • The ArrayList ClassIllegal if index > size()myList.add(8, Doc);

    Chapter 4: Lists and the Collection Interface

  • The ArrayList Class (continued) Remove at a certain indexmyList.remove(1);

    Chapter 4: Lists and the Collection Interface

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

    Chapter 4: Lists and the Collection Interface

  • The ArrayList Class (continued)Search usingmyList.indexOf(Sneezy); // returns 2myList.indexOf(Jumpy); // returns -1

    Chapter 4: Lists and the Collection Interface

  • Specification of the ArrayList Class

    Chapter 4: Lists and the Collection Interface

  • Application of ArrayListThe ArrayList gives you additional capability beyond what an array providesFor Java 1.4.2ArrayList stores items of type Object and can thus store an object of any classYou cannot store values of the primitive types directly but must instead use wrapper classes Example??When an object is stored in an ArrayList, the programmer must remember the original type String entry = (String) myList.get(1);

    Chapter 4: Lists and the Collection Interface

  • Application of ArrayListFor Java 5.0 and higher:You cannot store values of the primitive types directly but must instead use wrapper classes. However, autoboxing/autounboxing are in effectArrayList B = new ArrayList();B.add(42); //autoboxingint temp = B.get(0); //autounboxing

    No casting required when retrieving from the ArrayListArrayList C = new ArrayList();C.add(Hello); String entry = myList.get(0);

    Chapter 4: Lists and the Collection Interface

  • Recall PhoneDirectory?Instance variable theDirectory private DirectoryEntry [ ] theDirectory = new DirectoryEntry[capacity];

    ArrayBased addOrChangeEntry methodpublic String addOrChangeEntry(String name, String number) { String oldNumber = null; int index = find(name); if (index > -1) { oldNumber = theDirectory[index].getNumber(); theDirectory[index].setNumber(number); } else { add(name, number); } modified = true; return oldNumber; }

    Chapter 4: Lists and the Collection Interface

  • Using an ArrayList in PhoneDirectoryInstance variable theDirectory private ArrayList theDirectory = new ArrayList();

    addOrChangeEntry method

    public String addOrChangeEntry(String name, String number) { String oldNumber = null; int index = theDirectory.indexOf(new DirectoryEntry(name, ));//assuming DirectoryEntry class has an equals method that compares entries by name if (index != -1) { DirectoryEntry de = theDirectory.get(index); oldNumber = de.getNumber(); de.setNumber(number); } else { theDirectory.add(new DirectoryEntry(name, number)); } modified = true; return oldNumber;}

    Chapter 4: Lists and the Collection Interface

  • Assignment #1 (Due Thursday, Sep 24)Design and implement the Programming Project #2 described at the end of Chapter 1 (page 56), using an ArrayList as the underlying storage.

    Provide UML diagrams for your design.

    Document your code well.

    Place all your file under a directory called YourNameHW1 and zip the directory. Email a single zipped file to [email protected] a hardcopy of your code together with your UML diagram in class on the due date.

    Chapter 4: Lists and the Collection Interface

  • Implementation of an ArrayList ClassKWArrayList: simple implementation of a ArrayList classPhysical size of array indicated by data field capacityNumber of data items indicated by the data field size

    Chapter 4: Lists and the Collection Interface

  • Implementation of an ArrayList Class (continued)

    Chapter 4: Lists and the Collection Interface

  • Performance of KWArrayList and the Vector ClassSet and get methods execute in constant timeInserting or removing elements is linear timeInitial release of Java API contained the Vector class which has similar functionality to the ArrayListBoth contain the same methodsNew applications should use ArrayList rather than Vector

    Chapter 4: Lists and the Collection Interface