Unformatted text preview:

COMP 401 ARRAYS Instructor Prasun Dewan TOPICS Variable sized collections Encapsulated arrays 2 EXAMPLE 3 VARIABLE SIZE COLLECTION unfilled part filled part maximum size current size 4 VARIABLE SIZE COLLECTION size 3 array James Dean Joe Doe filled part Jane Smith current size maximum size unfilled part 5 VARIABLE SIZE COLLECTION public class ClassNeedingVariableSizeCollection final static int A MAX SIZE 50 String a new String A MAX SIZE int aSize 0 process a for 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 6 MONOLITHIC 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 7 SPECIAL TYPE public class ClassNeedingVariableSizeCollection AVariableSizeCollection a new AVariableSizeCollection Size Not Updated for int index 0 index a size index System out println a contents index a contents a size java io console readLine AVariableSizeCollection b new AVariableSizeCollection public class AVariableSizeCollection No Encapsulation public static final int MAX SIZE 50 public String contents new String MAX SIZE public int size 0 8 SUPPORTING ENCAPSULATION public interface StringHistory public static final int MAX SIZE 50 public void addElement String element public void print User specific Implementation specific 9 SUPPORTING ENCAPSULATION public interface StringHistory public void addElement String element public String getStringArray public int getSize Size is not a logical component of the array Can change arbitrary elements in array Implementation specific may use Vector ArrayList or LinkedList 10 HISTORY public interface StringHistory public void addElement String element public int size public String elementAt int index 11 IMPLEMENTING A HISTORY public 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 12 USING A HISTORY public 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 13 PRINTING A HISTORY static void print StringHistory strings System out println for int elementNum 0 elementNum strings size elementNum System out println strings elementAt elementNum System out println 14 HISTORY 15 DATABASE 16 DATABASE public interface StringDatabase from history public 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 17 REMOVEELEMENT STRING ELEMENT removeElement Joe Doe size array 4 3 James Dean Joe Doe Jane Smith public void removeElement String element contents indexOf element contents size 1 size John Smith Elements out of order 18 REMOVEELEMENT STRING ELEMENT removeElement Joe Doe size array 4 James Dean Joe Doe index Jane Smith 1 John Smith public void removeElement String element shiftUp indexOf element 19 MULTI ELEMENT WINDOW removeElement Joe Doe size array 4 James Dean Joe Doe index Jane Smith 1 John Smith public void removeElement String element shiftUp indexOf element contents index contents index 1 20 MULTI ELEMENT WINDOW removeElement Joe Doe size array 4 3 James Dean Jane Smith index Jane Smith 2 John Smith public void removeElement String element shiftUp indexOf element void shiftUp int startIndex for int index startIndex index 1 size index contents index contents index 1 size 21 INDEXOF STRING ELEMENT indexOf James Dean size array 4 James Dean Joe Doe index Jane Smith 0 John Smith 22 INDEXOF STRING ELEMENT indexOf Joe Doe size array 4 James Dean Joe Doe index Jane Smith 0 1 John Smith public int indexOf String element int index for index 0 index size element equals contents index index return index 23 PUBLIC BOOLEAN MEMBER STRING ELEMENT member Joe Doe size array 4 James Dean Joe Doe index Jane Smith 1 John Smith public 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 24 PUBLIC VOID CLEAR clear size array 4 0 1 2 3 James Dean Joe Doe Jane Smith public void clear while size 0 deleteElement size 1 John Smith 25 PUBLIC VOID CLEAR clear size array 4 0 James Dean Joe Doe public void clear size 0 Jane Smith John Smith 26 ADDELEMENT MARY DOE addElement Mary Doe size array 4 5 James Dean Joe Doe Jane Smith John Smith Mary Doe public void addElement String element if isFull System out println Adding item to a full history else contents size element size Code the same as in AStringHistory 27 APOINTHISTORY Variable sized Collection History Elements accessed by ObjectEditor Conventions for exporting elements 28 CONVENTIONS FOR VARIABLE SIZED COLLECTION Write Method public interface PointHistory public void addElement int x int y public Point elementAt int index public int size Read Methods Arbitrary Type 29 CONVENTIONS FOR VARIABLE SIZED COLLECTION Write method optional public interface I public void addElement T t public T elementAt int index public int size public Any setElementAt T t int index public Any removeElementAt int index public Any removeElementAt T t Read methods Arbitrary Type Unconstrained Type void or T in practice Convention based on Vector 30 ALTERNATIVE CONVENTIONS FOR VARIABLE SIZED COLLECTION Write method optional public interface I public void add T t public T get int index public int size public Any set int index T 2 public Any remove int index public Any remove T t Read methods Arbitrary Type Convention based on ArrayList ObjectEditor understands both 31 READ VS WRITE METHODS Read Methods Used to get components of object Getter methods size addElement Write Methods Used to change components of object Setter methods addElement


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