Java Coding 6 - Bilkent University Computer Engineering Department

Download Report

Transcript Java Coding 6 - Bilkent University Computer Engineering Department

Java Coding 6
Collections
David Davenport
Computer Eng. Dept.,
Bilkent University
Ankara - Turkey.
email: [email protected]
IMPORTANT…

Students…
This presentation is designed to be used in class as
part of a guided discovery sequence. It is not selfexplanatory! Please use it only for revision purposes
after having taken the class. Simply flicking through
the slides will teach you nothing. You must be actively
thinking, doing and questioning to learn!

Instructors…
You are free to use this presentation in your classes
and to make any modifications to it that you wish. All
I ask is an email saying where and when it is/was
used. I would also appreciate any suggestions you
may have for improving it.
thank you,
David.
Easy collections…
ARRAYLISTS
Easy Collections

Use Java’s ArrayList class
ArrayList
ArrayList()
boolean add(Object)
void
add(int, Object)
int
size()
Object get(int)
Object remove(int)
Object set(int, Object)
Object clone()
String toString()
boolean equals( Object)
An object of the
ArrayList class
provides a container
which can hold any
number of other
objects… NEAT!
Objects arranged in a
sequence: LIST
[ milk, eggs, bread, honey ]
Example use… (1)
import java.util.ArrayList;
ArrayList list;
list = new ArrayList();
System.out.println( list );
list.add( “David” );
list.add( “Gunes” );
list.add( 0, “Derya” );
System.out.println( list );
list.add( 1, new Date() );
list.add( new Person( “Ayse”, 18) );
System.out.println( list );
Example use… (2)
System.out.println( list.size() );
list.remove( size()-1 );
list.set( 0, new Date( 1, 1, 2001) );
System.out.println( list );
System.out.println( list.get(0) );
Date d = list.get(0);
Object o = list.get(0);
Date d = (Date)
if (list.get(0);
o instanceof Date ) {
Date d = (Date) o;
System.out.println( d.getAge() );
}
ArrayList- with generics

Use Java’s ArrayList class
ArrayList
ArrayList<E>()
boolean add(E)
void
add(int, E)
int
size()
E
get(int)
E
remove(int)
E
set(int, E)
Object clone()
String toString()
boolean equals( Object)
see Java API…
<E> is element type
…allows only that
type to be added
Example use… (3)

Solution: use generics…
ArrayList<Date> list;
list = new ArrayList <Date>();
System.out.println( list );
list.add( new Date() );
list.add( “oops, no go!” );
Date d = list.get(0);
System.out.println( d.getAge() );
ArrayList - & primitive types

Only object-types… solution?

enclose primitive in object!

DIY… “MyInt” class

Java’s Integer (wrapper) class

now with auto-boxing/unboxing!
public class MyInt {
DIY…wrapper
int value;
public MyInt( int i) {
value = i;
}
public int getValue() {
return value;
}
ArrayList<MyInt> list;
list = new
public String toString() {
return value + “”;
ArrayList<MyInt>();}
}
list.add( new MyInt(5) );
list.add( new MyInt(7) );
list.add( new MyInt(3) );
System.out.println( list );
int i = list.get(0).getValue();
package java.lang;
Java…wrapper
public class Integer {
int value;
public Integer( int i) {
value = i;
}
ArrayList<Integer> list;
public int intValue() {
return value;
}
list = new ArrayList<Integer>();
list.add( new Integer(5)
list.add( new Integer(7)
list.add( new Integer(3)
System.out.println( list
);
);
);
);
public String toString() {
return value + “”;
}
}
int i = list.get(0).intValue();
Wrapper classes
int – Integer
char – Character
double – Double
boolean - Boolean
With auto box/unboxing…
ArrayList<Integer> list;
list = new ArrayList<>();
new diamond notation
list.add(5);
auto-boxing
list.add(7);
list.add(3);
System.out.println( list );
auto-unboxing
int i = list.get(0);
for ( Integer j : list)
System.out.println( j * 2 );
new loop syntax
Easy Problem

Read in a set of positive integer values and then
print out a table showing the average, each of the
values and their difference from the average.
Example output…
Umm… must remember
all the values we read in
in order to print the
table.
Could use ArrayList… BUT
integers are not Objects!
(use Integer wrapper class)
Average is 5
Value
Diff
-------------10
5
3
-2
6
1
1
-4
--------------
Not so easy collections…
ARRAYS
Not-so-easy Collections

Arrays




Common data structure
All elements of same type
Are Objects in Java
Basis of ArrayList class!
grades
Name for entire
structure
Each element has unique
successor & predecessor
(except first & last.)
10
3
6
5
1
0
1
2
3
4
Each element identified by an
index (label/subscript)
Array Syntax (1)

Array size cannot
be changed after
creation!
Arrays are Objects, so

declare variable then instantiate
Note use of square brackets!
type[] variableName ;
variableName = new type[ noOfElements ];
grades
int[] grades;
grades = new int[5];
10
3
6
5
1
0
1
2
3
4
Array Syntax (2)

Referring to an individual element
variableName[index]

examples
grades[0]
grades[1]
names[99]
Where index is a
literal, named constant,
variable, or expression.
grades[ i]
grades[ i+1]
names[ FIRST]
grades[0] = 10;
grades[1] = grades[0] + 2;
System.out.println( grades[0]);
names[99] = scan.nextLine();
Processing all elements

e.g. Printing contents of array grades
System.out.println( grades[0] );
System.out.println( grades[1] );
:
for ( int i = 0; i < ___________; i++)
System.out.println( grades[i] );
for ( int i = 0; i < grades.length; i++)
System.out.println( grades[i] );
for each int k in grades array
print k
// alternate for syntax
for ( int k : grades)
System.out.println( k);
Easy Problem using arrays!

Printing table of differences from average
1. read set of values
2. compute average of set of values
3. print table of differences using average & set of values


Steps 2 & 3 are straightforward
For step 1 need to know how many values
 Fixed, e.g. 5
 Ask user
 Use sentinel
- but length of array is fixed!
Easy Problem with Methods!

Identify method signatures from algorithm
1. read set of values
int[] readSetOfValues()
double computeAverage( int[] setOfValues)
2. compute average of set of values
void printTable( double average, int[] setOfValues)
3. print table of differences using average & set of values
Data Requirements:
average – double
setOfValues – int[]
Note: Object-type parameters
can act as outputs too!
Arrays of objects

Array contains only references to objects
Track[] tracks;
tracks = new Track[5];

Still need to create actual objects
tracks[0] = new Track( “David”, 100);
tracks[1] = new Track( “Gunes”, 200);
tracks
0
David
100
1
2
Gunes
200
3
4
tracks[0].getTitle()
tracks[4].getTitle()