0% found this document useful (0 votes)
19 views73 pages

Lecture#3

Uploaded by

anonimolankaan
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PPT, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
19 views73 pages

Lecture#3

Uploaded by

anonimolankaan
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PPT, PDF, TXT or read online on Scribd
You are on page 1/ 73

 PART A: (a few slides from Chapter7)

◦ Intro to Java collections framework API


◦ Intro to class ArrayList
◦ Intro to the static methods in class Arrays

 PART B: (a few sample codes within Lecture Materials)


◦ Sample class applications

 PART C: (a few slides from Chapter16)


◦ ArrayList, List, Iterator, LinkedList
◦ The use of static methods in class Collections

© Copyright 1992-2015 by Pearson


Education, Inc. All Rights Reserved.
 Java collections framework – overview:
https://docs.oracle.com/javase/8/docs/technotes/guides/collection
s/overview.html
 Java API provides several predefined data structures, called
collections, used to store groups of related objects in memory.
◦ Each provides efficient methods that organize, store and retrieve your
data without requiring knowledge of how the data is being stored.
◦ Reduce application-development time.
 Arrays do not automatically change their size at execution time
to accommodate additional elements.
 ArrayList<T> (package java.util) can dynamically
change its size to accommodate more elements.
◦ T is a placeholder for the type of element stored in the collection.
 Classes with this kind of placeholder that can be used with any
type are called generic classes.
© Copyright 1992-2015 by Pearson
Education, Inc. All Rights Reserved.
© Copyright 1992-2015 by Pearson
Education, Inc. All Rights Reserved.
 Figure 7.24 demonstrates some common ArrayList
capabilities.
 An ArrayList’s capacity indicates how many items

it can hold without growing.


 When the ArrayList grows, it must create a larger

internal array and copy each element to the new array.


◦ This is a time-consuming operation. It would be inefficient for
the ArrayList to grow each time an element is added.
◦ An ArrayList grows only when an element is added and the
number of elements is equal to the capacity—i.e., there is no
space for the new element.

© Copyright 1992-2015 by Pearson


Education, Inc. All Rights Reserved.
 Method add adds elements to the ArrayList.
◦ One-argument version appends its argument to the end of the
ArrayList.
◦ Two-argument version inserts a new element at the specified
position.
◦ Collection indices start at zero.
 Method size returns the number of elements in the
ArrayList.
 Method get obtains the element at a specified index.
 Method remove deletes an element with a specific value.
◦ An overloaded version of the method removes the element at the
specified index.
 Method contains determines if an item is in the
ArrayList.

© Copyright 1992-2015 by Pearson


Education, Inc. All Rights Reserved.
© Copyright 1992-2015 by Pearson
Education, Inc. All Rights Reserved.
© Copyright 1992-2015 by Pearson
Education, Inc. All Rights Reserved.
© Copyright 1992-2015 by Pearson
Education, Inc. All Rights Reserved.
Java SE 7—Diamond (<>) Notation for Creating an Object
of a Generic Class
Consider line 10 of Fig. 7.24:
 ArrayList<String> items = new ArrayList<String>();
Notice that ArrayList<String> appears in the variable
declaration and in the class instance creation expression. Java
SE 7 introduced the diamond (<>) notation to simplify
statements like this. Using <> in a class instance creation
expression for an object of a generic class tells the compiler to
determine what belongs in the angle brackets.

© Copyright 1992-2015 by Pearson


Education, Inc. All Rights Reserved.
 In Java SE 7 and higher, the preceding statement can be
written as:
 ArrayList<String> items = new ArrayList<>();

 When the compiler encounters the diamond (<>) in the


class instance creation expression, it uses the declaration of
variable items to determine the ArrayList’s element type
(String)—this is known as inferring the element type.

© Copyright 1992-2015 by Pearson


Education, Inc. All Rights Reserved.
 Arrays class
◦ Provides static methods for common array manipulations.
 Methods include
◦ sort for sorting an array (ascending order by default)
◦ binarySearch for searching a sorted array
◦ equals for comparing arrays
◦ fill for placing values into an array.
 Methods are overloaded for primitive-type arrays and
for arrays of objects.
 System class static arraycopy method
◦ Copies contents of one array into another.

© Copyright 1992-2015 by Pearson


Education, Inc. All Rights Reserved.
© Copyright 1992-2015 by Pearson
Education, Inc. All Rights Reserved.
© Copyright 1992-2015 by Pearson
Education, Inc. All Rights Reserved.
© Copyright 1992-2015 by Pearson
Education, Inc. All Rights Reserved.
© Copyright 1992-2015 by Pearson
Education, Inc. All Rights Reserved.
© Copyright 1992-2015 by Pearson
Education, Inc. All Rights Reserved.
© Copyright 1992-2015 by Pearson
Education, Inc. All Rights Reserved.
 Let’s now switch back to ‘Lecture Materials’ on Bb and
dissect the following sample codes:
◦ Student
◦ SoccerPlayer
◦ Box

© Copyright 1992-2015 by Pearson


Education, Inc. All Rights Reserved.
Java How to Program, 10/e

© Copyright 1992-2015 by Pearson Education, Inc. All


Rights Reserved.
© Copyright 1992-2015 by Pearson
Education, Inc. All Rights Reserved.
 Java collections framework
◦ Contains prebuilt generic data structures
 After reading Chapter 17, Java SE 8 Lambdas and
Streams, you’ll be able to reimplement many of
Chapter 16’s examples in a more concise and elegant
manner, and in a way that makes them easier to
parallelize to improve performance on today’s multi-
core systems.

© Copyright 1992-2015 by Pearson


Education, Inc. All Rights Reserved.
 A collection is a data structure—actually, an object—
that can hold references to other objects.
◦ Usually, collections contain references to objects of any type
that has the is-a relationship with the type stored in the
collection.
 Figure 16.1 lists some of the collections framework
interfaces.
 Package java.util.

© Copyright 1992-2015 by Pearson


Education, Inc. All Rights Reserved.
© Copyright 1992-2015 by Pearson
Education, Inc. All Rights Reserved.
 Each primitive type has a corresponding type-wrapper
class (in package java.lang).
◦ Boolean, Byte, Character, Double, Float,
Integer, Long and Short.
 Each type-wrapper class enables you to manipulate
primitive-type values as objects.
 Collections cannot manipulate variables of primitive

types.
◦ They can manipulate objects of the type-wrapper classes,
because every class ultimately derives from Object.

© Copyright 1992-2015 by Pearson


Education, Inc. All Rights Reserved.
 Each of the numeric type-wrapper classes—Byte,
Short, Integer, Long, Float and Double—
extends class Number.
 The type-wrapper classes are final classes, so you

cannot extend them.


 Primitive types do not have methods, so the methods

related to a primitive type are located in the


corresponding type-wrapper class.

© Copyright 1992-2015 by Pearson


Education, Inc. All Rights Reserved.
© Copyright 1992-2015 by Pearson
Education, Inc. All Rights Reserved.
 A boxing conversion converts a value of a primitive type to an
object of the corresponding type-wrapper class.
 An unboxing conversion converts an object of a type-wrapper
class to a value of the corresponding primitive type.
 These conversions are performed automatically—called
autoboxing and auto-unboxing.
 Example:
◦ // create integerArray
Integer[] integerArray = new Integer[5];
// assign Integer 10 to integerArray[ 0 ]
integerArray[0] = 10;
// get int value of Integer
int value = integerArray[0];

© Copyright 1992-2015 by Pearson


Education, Inc. All Rights Reserved.
 Interface Collection contains bulk operations for adding,
clearing and comparing objects in a collection.
 A Collection can be converted to an array.
 Interface Collection provides a method that returns an
Iterator object, which allows a program to walk through the
collection and remove elements from the collection during the
iteration.
 Class Collections provides static methods that search,
sort and perform other operations on collections.

© Copyright 1992-2015 by Pearson


Education, Inc. All Rights Reserved.
© Copyright 1992-2015 by Pearson
Education, Inc. All Rights Reserved.
© Copyright 1992-2015 by Pearson
Education, Inc. All Rights Reserved.
 A List (sometimes called a sequence) is an ordered
Collection that can contain duplicate elements.
 List indices are zero based.
 In addition to the methods inherited from Collection,
List provides methods for manipulating elements via their
indices, manipulating a specified range of elements,
searching for elements and obtaining a ListIterator to
access the elements.
 Interface List is implemented by several classes, including
ArrayList, LinkedList and Vector.
 Autoboxing occurs when you add primitive-type values to
objects of these classes, because they store only references
to objects.

© Copyright 1992-2015 by Pearson


Education, Inc. All Rights Reserved.
 Class ArrayList and Vector are resizable-array implementations
of List.
 Inserting an element between existing elements of an ArrayList or
Vector is an inefficient operation.
 A LinkedList enables efficient insertion (or removal) of elements in
the middle of a collection, but is much less efficient than an
ArrayList for jumping to a specific element in the collection.
 We discuss the architecture of linked lists in Chapter 21.
 The primary difference between ArrayList and Vector is that
operations on Vectors are synchronized by default, whereas those on
ArrayLists are not.
 Unsynchronized collections provide better performance than
synchronized ones.
 For this reason, ArrayList is typically preferred over Vector in
programs that do not share a collection among threads.

© Copyright 1992-2015 by Pearson


Education, Inc. All Rights Reserved.
© Copyright 1992-2015 by Pearson
Education, Inc. All Rights Reserved.
© Copyright 1992-2015 by Pearson
Education, Inc. All Rights Reserved.
 List method add adds an item to the end of a list.
 List method size retursn the number of elements.
 List method get retrieves an individual element’s value from the
specified index.
 Collection method iterator gets an Iterator for a
Collection.
 Iterator- method hasNext determines whether there are more
elements to iterate through.
◦ Returns true if another element exists and false otherwise.
 Iterator method next obtains a reference to the next element.
 Collection method contains determine whether a
Collection contains a specified element.
 Iterator method remove removes the current element from a
Collection.

© Copyright 1992-2015 by Pearson


Education, Inc. All Rights Reserved.
© Copyright 1992-2015 by Pearson
Education, Inc. All Rights Reserved.
© Copyright 1992-2015 by Pearson
Education, Inc. All Rights Reserved.
© Copyright 1992-2015 by Pearson
Education, Inc. All Rights Reserved.
© Copyright 1992-2015 by Pearson
Education, Inc. All Rights Reserved.
© Copyright 1992-2015 by Pearson
Education, Inc. All Rights Reserved.
 Type Inference with the <> Notation
◦ Lines 14 and 21 specify the type stored in the ArrayList
(that is, String) on the left and right sides of the
initialization statements.
◦ Java SE 7 introduced type inferencing with the <> notation
—known as the diamond notation—in statements that
declare and create generic type variables and objects. For
example, line 14 can be written as:

List<String> list = new ArrayList<>();

◦ Java uses the type in angle brackets on the left of the


declaration (that is, String) as the type stored in the
ArrayList created on the right side of the declaration.

© Copyright 1992-2015 by Pearson


Education, Inc. All Rights Reserved.
 List method addAll appends all elements of a collection to
the end of a List.
 List method listIterator gets A List’s bidirectional
iterator.
 String method toUpperCase gets an uppercase version of a
String.
 List-Iterator method set replaces the current element to
which the iterator refers with the specified object.
 String method toLowerCase returns a lowercase version of
a String.
 List method subList obtaina a portion of a List.
◦ This is a so-called range-view method, which enables the program to
view a portion of the list.

© Copyright 1992-2015 by Pearson


Education, Inc. All Rights Reserved.
 List method clear remove the elements of a List.
 List method size returns the number of items in the

List.
 ListIterator method hasPrevious determines

whether there are more elements while traversing the


list backward.
 ListIterator method previous gets the

previous element from the list.

© Copyright 1992-2015 by Pearson


Education, Inc. All Rights Reserved.
 Class Arrays provides static method asList to view an
array as a List collection.
◦ A List view allows you to manipulate the array as if it were a list.
◦ This is useful for adding the elements in an array to a collection and for
sorting array elements.
 Any modifications made through the List view change the
array, and any modifications made to the array change the List
view.
 The only operation permitted on the view returned by asList is
set, which changes the value of the view and the backing array.
◦ Any other attempts to change the view result in an
UnsupportedOperationException.
 List method toArray gets an array from a List collection.

© Copyright 1992-2015 by Pearson


Education, Inc. All Rights Reserved.
© Copyright 1992-2015 by Pearson
Education, Inc. All Rights Reserved.
© Copyright 1992-2015 by Pearson
Education, Inc. All Rights Reserved.
© Copyright 1992-2015 by Pearson
Education, Inc. All Rights Reserved.
© Copyright 1992-2015 by Pearson
Education, Inc. All Rights Reserved.
© Copyright 1992-2015 by Pearson
Education, Inc. All Rights Reserved.
© Copyright 1992-2015 by Pearson
Education, Inc. All Rights Reserved.
© Copyright 1992-2015 by Pearson
Education, Inc. All Rights Reserved.
 LinkedList method addLast adds an element to
the end of a List.
 LinkedList method add also adds an element to the

end of a List.
 LinkedList method addFirst adds an element to

the beginning of a List.

© Copyright 1992-2015 by Pearson


Education, Inc. All Rights Reserved.
© Copyright 1992-2015 by Pearson
Education, Inc. All Rights Reserved.
 Class Collections provides several high-
performance algorithms for manipulating collection
elements.
 The algorithms (Fig. 16.5) are implemented as

static methods.

© Copyright 1992-2015 by Pearson


Education, Inc. All Rights Reserved.
© Copyright 1992-2015 by Pearson
Education, Inc. All Rights Reserved.
© Copyright 1992-2015 by Pearson
Education, Inc. All Rights Reserved.
 Method sort sorts the elements of a List
◦ The elements must implement the Comparable interface.
◦ The order is determined by the natural order of the elements’
type as implemented by a compareTo method.
◦ Method compareTo is declared in interface Comparable
and is sometimes called the natural comparison method.
◦ The sort call may specify as a second argument a
Comparator object that determines an alternative ordering
of the elements.

© Copyright 1992-2015 by Pearson


Education, Inc. All Rights Reserved.
© Copyright 1992-2015 by Pearson
Education, Inc. All Rights Reserved.
 The Comparator interface is used for sorting a
Collection’s elements in a different order.
 The static Collections method

reverseOrder returns a Comparator object that


orders the collection’s elements in reverse order.

© Copyright 1992-2015 by Pearson


Education, Inc. All Rights Reserved.
© Copyright 1992-2015 by Pearson
Education, Inc. All Rights Reserved.
© Copyright 1992-2015 by Pearson
Education, Inc. All Rights Reserved.
 Figure 16.8 creates a custom Comparator class, named
TimeComparator, that implements interface
Comparator to compare two Time2 objects.
 Class Time2, declared in Fig. 8.5, represents times with
hours, minutes and seconds.
 Class TimeComparator implements interface
Comparator, a generic type that takes one type argument.
 A class that implements Comparator must declare a
compare method that receives two arguments and returns
a negative integer if the first argument is less than the
second, 0 if the arguments are equal or a positive integer if
the first argument is greater than the second.

© Copyright 1992-2015 by Pearson


Education, Inc. All Rights Reserved.
© Copyright 1992-2015 by Pearson
Education, Inc. All Rights Reserved.
© Copyright 1992-2015 by Pearson
Education, Inc. All Rights Reserved.
© Copyright 1992-2015 by Pearson
Education, Inc. All Rights Reserved.
 Collections method reverse reverses the order of the
elements in a List
 Method fill overwrites elements in a List with a
specified value.
 Method copy takes two arguments—a destination List
and a source List.
◦ Each source List element is copied to the destination List.
◦ The destination List must be at least as long as the source List;
otherwise, an IndexOutOfBoundsException occurs.
◦ If the destination List is longer, the elements not overwritten are
unchanged.
 Methods min and max each operate on any Collection.
◦ Method min returns the smallest element in a Collection, and
method max returns the largest element in a Collection.

© Copyright 1992-2015 by Pearson


Education, Inc. All Rights Reserved.
© Copyright 1992-2015 by Pearson
Education, Inc. All Rights Reserved.
© Copyright 1992-2015 by Pearson
Education, Inc. All Rights Reserved.
© Copyright 1992-2015 by Pearson
Education, Inc. All Rights Reserved.
 static Collections method binarySearch
locates an object in a List.
◦ If the object is found, its index is returned.
◦ If the object is not found, binarySearch returns a negative
value.
◦ Method binarySearch determines this negative value by
first calculating the insertion point and making its sign
negative.
◦ Then, binarySearch subtracts 1 from the insertion point to
obtain the return value, which guarantees that method
binarySearch returns positive numbers (>= 0) if and only
if the object is found.

© Copyright 1992-2015 by Pearson


Education, Inc. All Rights Reserved.
© Copyright 1992-2015 by Pearson
Education, Inc. All Rights Reserved.
© Copyright 1992-2015 by Pearson
Education, Inc. All Rights Reserved.
© Copyright 1992-2015 by Pearson
Education, Inc. All Rights Reserved.

You might also like