Title: ArrayList
1ArrayList
2Limitations of Arrays
- In Java you can allocate space for an array at
runtime
numWords / value set at run-time, e.g. from
input / String words new StringnumWords
But, you cannot change the size of an array after
allocating storage.
What if you don't know the size of data in
advance? Example reading words from a file, but
you don't know how many words the file contains?
3ArrayList
- ArrayList is an alternative for variable size
data - don't specify size in advance ... grows
dynamically - can add, delete, replace objects in ArrayList
- ArrayList holds data of type "Object" (no
primitives)
- ArrayList alist new ArrayList( )
- BufferedReader input new BufferedReader( ... )
- while( input.ready( ) )
- String word input.readLine( ).trim( )
- alist.add( word ) / ArrayList grows itself /
-
- System.out.println("You read " alist.size( )
" words"
4Copying ArrayList to Array
- Use an ArrayList to save data when you don't know
how big the data set is. - After saving all data, copy to an Array for
processing - Method (Arraylist-object).toArray( Array )
- ArrayList alist new ArrayList( )
- BufferedReader input openFile( filename )
- while( input.ready( ) )
- String s input.readLine( ).trim( )
- alist.add( s ) // add input data to ArrayList
-
- // create an Array large enough to store the data
- String words new String alist.size( )
- // copy ArrayList to Array
- alist.toArray( words )
size( ) returns size of ArrayList
5Using ArrayList to Read Strings
- Read lines from a file and sort them all.
- You can't use an array of String, because you
don't know how many lines are in the file.
- ArrayList alist new ArrayList( )
- BufferedReader input openFile( filename )
- while( input.ready( ) )
- String s input.readLine( ).trim( )
- alist.add( s ) // add input data to ArrayList
-
- // copy to an array so we can sort. We know the
size now! - String words new String alist.size( )
- alist.toArray( words )
- // now sort the words
- Arrays.sort( words )
Exact size of data!
6Using ArrayList to Read KUPersons
Read student names IDs from the
URL http//www.cpe.ku.ac.th/jim/classlist.txt
- ArrayList alist new ArrayList( )
- BufferedReader input openURL("http//.../classli
st.txt") - while( input.ready( ) )
- String s input.readLine( ).trim( )
- KUPerson ku parseKUPerson( s ) / Homework 3
/ - alist.add( ku )
-
- // copy to an array so we can sort
- KUPerson class new KUPerson alist.size( )
- alist.toArray( class ) / copy data to class
array / - Arrays.sort( class ) / Homework 4 /
7Useful ArrayList Methods
- int size( ) returns actual size of ArrayList
- add( Object obj ) add an object to ArrayList
- Object get(int index) get object at given index
- Object remove(int i) get object and delete from
ArrayList - clear( ) remove all objects from ArrayList
- set(int index, Object obj) replace object at
index - contains( Object obj ) "true" if obj is in
ArrayList - ensureCapacity(int size) make sure ArrayList can
hold at least - this many elements without reallocation
- ensureCapacity( ) is used to improve efficiency
when you are adding a lot of items to an
Arraylist.
8Merging Data (Union)
- Read words from the input and form the union of
all data. - You must eliminate duplicate words.
- Sort the list of words and output them
ArrayList wordlist new ArrayList(
) BufferedReader input openFile("wordlist.txt")
while( input.ready( ) ) String s
input.readLine( ).trim( ) if ( !
wordlist.contains( s ) ) wordlist.add( s ) //
copy to an array so we can sort String words
new String wordlist.size( )
wordlist.toArray( words ) / copy to array
/ Arrays.sort( class )
9More Information
- Core Java, page 171 - 177.
- Java API documentation.