DOC PREVIEW
UNC-Chapel Hill COMP 401 - COMP 401 ARRAYS

This preview shows page 1-2-15-16-17-32-33 out of 33 pages.

Save
View full document
View full document
Premium Document
Do you want full access? Go Premium and unlock all 33 pages.
Access to all documents
Download any document
Ad free experience
View full document
Premium Document
Do you want full access? Go Premium and unlock all 33 pages.
Access to all documents
Download any document
Ad free experience
View full document
Premium Document
Do you want full access? Go Premium and unlock all 33 pages.
Access to all documents
Download any document
Ad free experience
View full document
Premium Document
Do you want full access? Go Premium and unlock all 33 pages.
Access to all documents
Download any document
Ad free experience
View full document
Premium Document
Do you want full access? Go Premium and unlock all 33 pages.
Access to all documents
Download any document
Ad free experience
View full document
Premium Document
Do you want full access? Go Premium and unlock all 33 pages.
Access to all documents
Download any document
Ad free experience
View full document
Premium Document
Do you want full access? Go Premium and unlock all 33 pages.
Access to all documents
Download any document
Ad free experience
Premium Document
Do you want full access? Go Premium and unlock all 33 pages.
Access to all documents
Download any document
Ad free experience

Unformatted text preview:

Slide 1TopicsExampleVariable-Size CollectionVariable-Size CollectionVariable-Size CollectionMonolithic Programs Using Variable Sized collectionSpecial TypeSupporting EncapsulationSupporting EncapsulationHistoryImplementing A HistoryUsing a HistoryPrinting a HistoryHistoryDatabaseDatabaseremoveElement(String element)removeElement(String element)Multi-Element WindowMulti-Element WindowindexOf(String element)indexOf(String element)public boolean member(String element)public void clear()public void clear()addElement(“Mary Doe”);APointHistoryConventions for Variable-Sized CollectionConventions for Variable-Sized CollectionAlternative Conventions for Variable-Sized CollectionRead vs. Write MethodsJava Vectors, Array Lists and IteratorsCOMP 401ARRAYSInstructor: Prasun Dewan2TOPICSVariable-sized collectionsEncapsulated arrays3EXAMPLE4VARIABLE-SIZE COLLECTIONfilled partunfilled partcurrent sizemaximum size53James DeanJoe DoeJane Smithsize arrayVARIABLE-SIZE COLLECTIONfilled partunfilled partcurrent sizemaximum size6VARIABLE-SIZE COLLECTIONpublic class <ClassNeedingVariableSizeCollection> { …final static int A_MAX_SIZE = 50;String[] a = new String[A_MAX_SIZE];int aSize = 0;…//process afor (int index = 0; index < aSize; index++) System.out.println(a[index]);…final int B_MAX_SIZE = 50;String[] b = new String[B_MAX_SIZE];int bSize = 0;… //process b …}7MONOLITHIC PROGRAMS USING VARIABLE SIZED COLLECTIONVariable-sized collection takes programmer effort.Max size constant.Current size.Checking of max size.Manipulation of size.Deleting, replacing, searching takes more effort.Main program declares and uses (array-based implementation of) variable-sized collectionCannot reuse variable-size collection implementation.It is ok if program uses array as fixed-sized collection as no programmer effort required to implement it.8SPECIAL TYPEpublic class <ClassNeedingVariableSizeCollection> { ... AVariableSizeCollection a = new AVariableSizeCollection(); ... for (int index = 0; index < a.size; index++) System.out.println(a.contents[index]); … AVariableSizeCollection b = new AVariableSizeCollection(); ...}public class AVariableSizeCollection {public static final int MAX_SIZE = 50;public String[] contents = new String [MAX_SIZE];public int size = 0;}No EncapsulationSize Not Updateda.contents[a.size] = java.io.console.readLine();9SUPPORTING ENCAPSULATIONpublic interface StringHistory {public static final int MAX_SIZE = 50;public void addElement(String element);public void print();}User-specific Implementation-specific10SUPPORTING ENCAPSULATIONpublic interface StringHistory {public void addElement(String element);public String[] getStringArray();public int getSize();}Size is not a logical component of the arrayImplementation-specific (may use Vector, ArrayList or LinkedList)Can change arbitrary elements in array.11HISTORYpublic interface StringHistory {public void addElement(String element);public int size();public String elementAt(int index);}12IMPLEMENTING A HISTORYpublic class AStringHistory implements StringHistory { public final int MAX_SIZE = 50; String[] contents = new String[MAX_SIZE]; int size = 0; public int size() { return size;} public String elementAt (int index) { return contents[index]; } boolean isFull() { return size == MAX_SIZE; } public void addElement(String element) { if (isFull()) System.out.println("Adding item to a full history"); else { contents[size] = element; size++;} } }13USING A HISTORYpublic static void main(String[] args) { StringHistory names = new AStringHistory(); while (true) { String input = System.io.console.readLine(); if (input.length() > 0) if (input.charAt(0) == 'q') break; else if (input.charAt(0) == 'p' ) print(names); else names.addElement(input); }}14PRINTING A HISTORYstatic void print(StringHistory strings) { System.out.println("******************"); for ( int elementNum = 0; elementNum < strings.size(); elementNum++) System.out.println(strings.elementAt(elementNum)); System.out.println("******************");}15HISTORY16DATABASE17DATABASEpublic interface StringDatabase {//from historypublic int size();public void addElement(String element);public String elementAt(int index); //additional methods public void removeElement(String element); public void member(String element); public void clear();}18REMOVEELEMENT(STRING ELEMENT)James DeanJoe DoeJane SmithJohn Smitharraysize4removeElement(“Joe Doe”);John Smith3public void removeElement (String element) { contents[indexOf(element)] = contents[size - 1]; size--;}Elements out of order!19REMOVEELEMENT(STRING ELEMENT)James DeanJoe DoeJane Smitharraysize4removeElement(“Joe Doe”);John Smith1indexpublic void removeElement(String element) { shiftUp(indexOf(element));}20MULTI-ELEMENT WINDOWJames DeanJoe DoeJane Smitharraysize4removeElement(“Joe Doe”);John Smith1indexpublic void removeElement(String element) { shiftUp(indexOf(element));}Jane Smithcontents[index] = contents[index + 1];21John SmithMULTI-ELEMENT WINDOWJames DeanJane SmithJane SmithJohn Smitharraysize4removeElement(“Joe Doe”);2indexpublic void removeElement(String element) { shiftUp(indexOf(element));}Jane Smithvoid shiftUp(int startIndex) { for (int index = startIndex; index + 1 < size; index++) contents[index] = contents[index + 1]; size--;}3John Smith22INDEXOF(STRING ELEMENT)James DeanJoe DoeJane SmitharraysizeindexOf(“James Dean”);0index Jane Smith4John Smith23INDEXOF(STRING ELEMENT)James DeanJoe DoeJane SmitharraysizeindexOf(“Joe Doe”);0index Jane Smith4John Smithpublic int indexOf (String element) { int index; for (index = 0; index < size && !element.equals(contents[index]; index++); return index;124PUBLIC BOOLEAN MEMBER(STRING ELEMENT)James DeanJoe DoeJane Smitharraysizemember(“Joe Doe”);1index Jane Smith4John Smithpublic int indexOf (String element) { int index; for (index = 0; index < size && !element.equals(contents[index]; index++); return index;public boolean member (String element) {}return indexOf (element) < size;25PUBLIC VOID CLEAR()James DeanJoe DoeJane SmitharraysizeJane Smith4John Smithclear();public void clear() { while (size > 0) deleteElement(size -1);}321026PUBLIC VOID CLEAR()James DeanJoe DoeJane SmitharraysizeJane Smith4John Smithclear();public void clear() { size = 0;}027ADDELEMENT(“MARY DOE”);James DeanJoe


View Full Document

UNC-Chapel Hill COMP 401 - COMP 401 ARRAYS

Documents in this Course
Objects

Objects

36 pages

Recursion

Recursion

45 pages

Load more
Download COMP 401 ARRAYS
Our administrator received your request to download this document. We will send you the file to your email shortly.
Loading Unlocking...
Login

Join to view COMP 401 ARRAYS and access 3M+ class-specific study document.

or
We will never post anything without your permission.
Don't have an account?
Sign Up

Join to view COMP 401 ARRAYS 2 2 and access 3M+ class-specific study document.

or

By creating an account you agree to our Privacy Policy and Terms Of Use

Already a member?