BSCS Semester 6 Mcs 3 Course Instructor: Ms. Iram
BSCS Semester 6 Mcs 3 Course Instructor: Ms. Iram
Paradigm
Advance OO Programming
BSCS Semester 6
MCS 3
Course Instructor: Ms. Iram
java.util.Random
Benchmarks uses the java.util.Random class
a more controlled way to generate random
the seed is different
numbers.
Random generator1 = new Random();
Random generator2 = new Random(seed);
each time
long seed;
0k<n
0x<1
java.util.Arrays
Provides static methods for dealing with
arrays.
Methods:
int pos = Arrays.binarySearch (arr, target);
Arrays.sort (arr);
Arrays.fill (arr, value); // fills arr with a given value
String str = Arrays.toString(arr);
Arrays.asList(arr);
Returns a representation of
arr as a fixed-length list
13-3
java.util.Collections
Provides static methods for dealing with
ArrayLists and other Java collections.
Methods:
int pos = Collections.binarySearch (list, target);
Collections.sort (list);
Collections.shuffle (list);
13-4
Collection, Iterator
interface
Iterator
interface
Collection
Collections
Collection, Iterator
Lists, ListIterator
List
ArrayList
LinkedList
Stack
Queue, PriorityQueue
Sets
Set
TreeSet
HashSet
Maps
Map
TreeMap
HashMap
Overview
19-7
Collection<E>
Methods
boolean isEmpty ()
int size ()
boolean contains (Object obj)
boolean add (E obj)
boolean remove (E obj)
Iterator<E> iterator ()
// ... other methods
interface
Iterator
interface
Collection
Supplies an
iterator for this
collection
19-8
Iterator<E>
Methods
interface
Iterator
boolean hasNext ()
E next ()
void remove ()
interface
Collection
Whats next is
determined by a
particular collection
19-9
Lists, ListIterator
A list represents a collection in which all
elements are numbered by indices:
a0, a1, ..., an-1
java.util:
List interface
ArrayList
LinkedList
List<E>
Methods
interface
Iterator
interface
Collection
interface
ListIterator
interface
List
These methods
are familiar from
ArrayList, which
implements List
Returns a ListIterator
that starts iterations at
index i
19-11
ListIterator<E>
Methods
interface
Iterator
interface
Collection
interface
ListIterator
interface
List
interface
List
interface
Iterator
ArrayList
interface
ListIterator
ArrayList
LinkedList
a1
a2
... an-1
19-13
interface
List
interface
Iterator
LinkedList
interface
ListIterator
ArrayList
LinkedList
a1
a2
...
an-1
LinkedList
(contd)
interface
Iterator
interface
ListIterator
interface
List
ArrayList
LinkedList
19-15
ArrayList
vs. LinkedList
19-16
java.util.ArrayList<E>
Implements a list using an array.
Can only hold objects (of a specified type),
not elements of primitive data types.
"Hat"
"Bat"
...
12-19
ArrayList
Generics
12-20
ArrayList<E> Constructors
Java docs use the letter E as
the type parameter for elements
in generic collections
ArrayList<E> ( )
Creates an empty
ArrayList<E> of
default capacity (ten)
12-21
ArrayList<E> Methods
(a Subset)
int size()
boolean isEmpty ()
boolean add (E obj)
returns true
E set(int i, E obj)
E get(int i)
E remove(int i)
boolean contains(E obj)
int indexOf(E obj)
i must be from 0 to
size() -1
use equals to
compare objects
12-22
ArrayList Example
ArrayList<String> names = new ArrayList<String>( );
names.add(Abdullah");
names.add(Ali");
names.add(0, "Aysha");
System.out.println(names);
Output
ArrayLists toString
method returns a string of
all the elements, separated
by commas, within [ ].
12-23
ArrayList<E> Details
Automatically increases (doubles) the capacity
when the list runs out of space (allocates a
bigger array and copies all the values into it).
12-24
ArrayList<E> Autoboxing
If you need to put ints or doubles into a list,
use a standard Java array or convert them
into Integer or Double objects
12-25
ArrayList<E> Autoboxing
Example
ArrayList<Integer> counts = new
ArrayList<Integer>( );
counts.add(17);
...
int count = counts.get(0);
Autoboxing: compiled as
counts.add(new Integer(17));
Autounboxing: count
gets the value 17
12-26
ArrayList Pitfalls
// Remove all occurences
// of "like" from words:
int i = 0;
for (int i = 0;
i < scores.length; i++)
{
int s = scores[i];
sum += s;
}
19-31