week10topics - Computing Sciences

Download Report

Transcript week10topics - Computing Sciences

Week 10
Introduction to Computer Science
and Object-Oriented Programming
COMP 111
George Basham
Week 10 Topics
10.1.1 Wrappers and Auto-boxing
10.1.2 The Enhanced for Loop
10.1.3 Simple Array Algorithms
10.1.4 Two-dimensional Arrays
10.1.5 Copying Arrays
10.1.1 Wrappers and Auto-boxing
• Because numbers are not objects in Java,
you cannot directly enter them in array
lists
• For example, this will not compile:
• ArrayList<double> data = new
ArrayList<double>(); // No
• To store sequences of numbers in an array
list, you must turn them into wrapper
classes
10.1.1 Wrappers and Auto-boxing Cont.
Primitive Type Wrapper Class
byte
Byte
boolean
Boolean
char
Character
double
Double
float
Float
int
Integer
long
Long
short
Short
10.1.1 Wrappers and Auto-boxing Cont.
• Beginning with Java 5.0, if you assign a primitive
type to a wrapper class, the conversion is
automatic (called auto-boxing):
• Double d = 29.95;
• Conversely, beginning with Java 5.0, wrapper
classes are automatically converted to primitive
types (called auto-unboxing):
• double x = d;
10.1.1 Wrappers and Auto-boxing Cont.
• This will work to store simple number
types in an array list:
• ArrayList<Double> data = new
ArrayList<Double>(); // Yes
• data.add(29.95);
• double x = data.get(0);
10.1.2 The Enhanced for Loop
• Java version 5.0 introduces a very
convenient shortcut for a common loop
type, when you need to iterate through an
sequence of elements
• The new loop construct is know as the “for
each” loop
• The “for each” loop has a very specific
purpose, traversing the elements of a
collection from the beginning to the end
10.1.2 The Enhanced for Loop
Cont.
double[] data = new double[100];
… assume that array is then populated
double sum = 0;
for (double e : data)
{
sum = sum + e;
}
10.1.2 The Enhanced for Loop
Cont.
ArrayList<BankAccount> accounts = new
ArrayList<BankAccount>();
… assume that array list is then
populated
double sum = 0;
for (BankAccount a : accounts)
{
sum = sum + a.getBalance();
}
10.1.3 Simple Array Algorithms
• Counting Matches
• Finding a Value (linear search)
• Finding the Maximum or Minimum
10.1.3 Simple Array Algorithms Cont.
• Counting Matches
double atLeast = 5000.00;
int matches = 0;
for (BankAccount a : accounts)
{
if (a.getBalance() >= atLeast)
matches++;
}
System.out.println(matches);
10.1.3 Simple Array Algorithms Cont.
• Finding a Value (linear search)
int accountNumber = 1001;
for (BankAccount a : accounts)
{
if (a.getAccountNumber() ==
accountNumber) {
System.out.println
(a.getBalance()); break; }
}
10.1.3 Simple Array Algorithms Cont.
• Finding the Maximum or Minimum
BankAccount max = accounts.get(0);
for (int i = 1; i < acocunts.size(); i++)
{
BankAccount a = account.get(i);
if (a.getBalance() > max.getBalance())
max = a;
}
System.out.println(max.getAccountNumber());
10.1.4 Two-Dimensional Arrays
• int[][] table = new int[2][3];
• The array identified by table will have 2 rows
(the row is the FIRST subscript) and 3
columns (the column is the SECOND
subscript).
[0][0]
[0][1]
[0][2]
[1][0]
[1][1]
[1][2]
10.1.4 Two-Dimensional Arrays Cont.
• table[0][0] = 22;
• table[0][1] = 1301;
•. . .
• table[1][2] = 43
// Traversing the 2-D array
for (int i = 0; i < 2; ++i) // rows
for (int j = 0; j < 3; ++j) // cols
System.out.println(table[i][j];
10.1.5 Copying Arrays
• Array variables work just like object
variables, they hold a reference to the
actual array
• If you copy the reference, you get another
reference to the same array:
double[] data = new double[10];
… // fill array
double[] prices = data;
10.1.5 Copying Arrays Cont.
• If you want to make a true copy of an
array, call the clone method
• Note that you need to cast the return value
of the clone method from the type Object
to the array type:
double[] prices = (double[])
data.clone();
10.1.5 Copying Arrays Cont.
• Use the static System.arraycopy method to copy
elements from one array to another
• System.arraycopy(from, fromStart, to, toStart,
count)
• To add a new element at position i into data:
System.arraycopy(data, i, data, i+1,
data.length –i -1);
data[i] = x;
10.1.5 Copying Arrays Cont.
• To grow an array that has run out of
space:
double[] newData = new double[2
* data.length);
System.arraycopy(data, 0,
newData, 0, data.length);
data = newData;
Reference: Big Java 2nd Edition by Cay
Horstmann
10.1.1 Wrappers and Auto-boxing
(section 8.3 in Big Java)
10.1.2 The Enhanced for Loop
(section 8.4 in Big Java)
10.1.3 Simple Array Algorithms
(section 8.5 in Big Java)
10.1.4 Two-dimensional Arrays
(section 8.6 in Big Java)
10.1.5 Copying Arrays (section 8.7 in
Big Java)