



numWords = console.nextInt();
String [ ] words = new String[numWords];
size of data

 !"#$
"
#!
%&

ArrayList food = new ArrayList( );
food.size(); // returns 0. Its empty
food.add("Apple");
food.add("Banana");
food.size(); // returns 2
System.out.println( food.get(0) ); // Apple
System.out.println( food.get(1) ); // Banana


!'
##$(
ArrayList

List"$
&
")*#
)*#)*# +)*#&
,,$(
ArrayListList
interface

$)*
"$
-.
ArrayList list = new ArrayList(); Java
list.add("apple");
list.add("banana");
list.size() // = 2
list.get(0) // "apple"
list.remove(0) // delete apple
list = [] Python
list.append("apple")
list.append("banana")
len(list) # = 2
list[0] # "apple"
del(list[0]) # delete apple
/+$(
ArrayList list = new ArrayList( );
list.add( "Apple" ); // a string
list.add( LocalDate.now() ); // LocalDate object
list.add( new Double(3.14) ); // another object
// Get something from arraylist
Object obj = list.get(1);
// If you want a String you must use a cast
String fruit = (String) list.get(0);
0
,,+$(
0
,!,+$(
cast
-$
$any$('
ArrayList list = new ArrayList( );
list.add( "apple" ); // String
list.add( new Date() ); // Date object
list.add( new Long(10) ); // Long object
list.add( new Coin(5) ); // Coin
Object obj = list.get(1); // always returns object
&!)*1cast
2!$(#cast
&
2&
ArrayList list = new ArrayList( );
list.add( "apple" ); // String
list.add( something );
...
String x = (String)list.get(0); // cast to String
/!!
// Get a string
String fruit = (String) list.get(0);
// If get(1) not a String, an Exception occurs
String fruit2 = (String) list.get(1);
java.lang.ClassCastException: line 5
0
2!,3!,#cast
3!&
0
$(3!
2
ArrayList<String> fruit =
new ArrayList<String>( );
list.add( "Apple" ); // a string
list.add( "Orange" ); // string
list.add( new Double(3.14) ); // Error
// Compiler will not allow to add a Double
// No cast! Result is automatically String
String s = list.get(1); // No cast!
0
3!43!5
0
43!5type parameter&
0
2$#$
,6,
43!5,3!,
4-5,-,
7
ArrayList<String> fruit =
new ArrayList<String>( );
fruit.add("Apple"); // add at end of list (0)
fruit.add("Orange"); // add at end of list (1)
fruit.add(1,"Banana"); // insert at index 1
fruit.size(); // 3 things in list
fruit.get(1); // "Banana" was inserted
fruit.get(2); // "Orange" was pushed down
fruit.contains("Fig") // false
fruit.remove("Apple") // remove first occurrence
fruit.get(0) // "Banana"
8
9!&
:$
&
,7;,
List<String> fruit = new ArrayList<String>( );
public double sum(List<Double> scores)
// Return a list of Students
public List<Student> getEnrollment(String course)
0
/List!$##
<<,,
0
&)=*
(*) Sometimes we need to know if List is mutable or immutable.
83"
8!
,Program to an interface, not an implementation,
"
$#
&
>3
">!>#

>!>&
8!3"
List<Person> people = ...
people
specication#
&
/425?
%)* @
)2$(* $()*
)#2$(* $()*
2!) * !$(!
2) * .
)* 
) #2$(*$(
)2$(* ,,$(
7)%* 
%!
2A#$3!#-#
B#&&&
7)*C!lot
&
!
3
;
List<String> menu = Restaurant.getMenu( );
for(int k=0; k < menu.size(); k++) {
System.out.println( list.get(k) );
}
0
-
0
-!<
List<String> menu = Restaurant.getMenu( );
for( String menuItem: menu ) {
System.out.println( menuItem );
}
7!
List<String> list = new ArrayList<String>( );
... read some data and add it to list
// create an array large enough to store the data
String [ ] words = new String[ list.size( ) ];
// copy ArrayList to Array
list.toArray( words );
0
/
$!&
0
list.toArray( array )<
3!
List<String> list = Restaurant.getMenu( );
Collections.sort( list ); // sorts the menu
Sort an ArrayList using the java.util.Collections class
Collections.sort( anyList )
anyList must contain objects that are Comparable
String, Double, Long, Int, Date...
any class that has a compareTo method
3


##

%!D
&
?;
Big Java, 7E or Core Java,9F&
2< 
-;&