Beruflich Dokumente
Kultur Dokumente
Chapter 8
Background
Java provides arrays and the collection classes We will consider arrays first
Basic terminology
Where n is the number of elements: either a number or character Ensures any reference to an array element is valid
Public data field length specifies the number of elements in the list (not a length function like with strings) Array is an object
Without initialization
ElementType [ ] id ;
Brackets Name of Type of list values in indicate array variable being list defined
With initialization
Nonnegative integer expression specifying the number of elements in the array
Example
Definitions
char[] c; int[] value = new int[5];
Causes
Array object variable c is un-initialized Array object variable v references a new nfive element list of integers
c value
0 0 0 0 0
Consider
int[] v = new int[10]; int i = 7; int j = 2; int k = 4; v[0] = 1; v[i] = 5; v[j] = v[i] + 3; v[j+1] = v[i] + v[0]; v[v[j]] = 12; System.out.println(v[2]); v[k] = stdin.nextInt();
v 0
v[0]
0
v[1]
0
v[2]
0
v[3]
0
v[4]
0
v[5]
0
v[6]
0
v[7]
0
v[8]
0
v[9]
Consider
int[] v = new int[10]; int i = 7; int j = 2; int k = 4; v[0] = 1; v[i] = 5; v[j] = v[i] + 3; v[j+1] = v[i] + v[0]; v[v[j]] = 12; System.out.println(v[2]); v[k] = stdin.nextInt();
v 1
v[0]
0
v[1]
0
v[2]
0
v[3]
0
v[4]
0
v[5]
0
v[6]
0
v[7]
0
v[8]
0
v[9]
Consider
int[] v = new int[10]; int i = 7; int j = 2; int k = 4; v[0] = 1; v[i] = 5; v[j] = v[i] + 3; v[j+1] = v[i] + v[0]; v[v[j]] = 12; System.out.println(v[2]); v[k] = stdin.nextInt();
v 1
v[0]
0
v[1]
0
v[2]
0
v[3]
0
v[4]
0
v[5]
0
v[6]
5
v[7]
0
v[8]
0
v[9]
Consider
int[] v = new int[10]; int i = 7; int j = 2; int k = 4; v[0] = 1; v[i] = 5; v[j] = v[i] + 3; v[j+1] = v[i] + v[0]; v[v[j]] = 12; System.out.println(v[2]); v[k] = stdin.nextInt();
v 1
v[0]
0
v[1]
8
v[2]
0
v[3]
0
v[4]
0
v[5]
0
v[6]
5
v[7]
0
v[8]
0
v[9]
Consider
int[] v = new int[10]; int i = 7; int j = 2; int k = 4; v[0] = 1; v[i] = 5; v[j] = v[i] + 3; v[j+1] = v[i] + v[0]; v[v[j]] = 12; System.out.println(v[2]); v[k] = stdin.nextInt();
v 1
v[0]
0
v[1]
8
v[2]
6
v[3]
0
v[4]
0
v[5]
0
v[6]
5
v[7]
0
v[8]
0
v[9]
Consider
int[] v = new int[10]; int i = 7; int j = 2; int k = 4; v[0] = 1; v[i] = 5; v[j] = v[i] + 3; v[j+1] = v[i] + v[0]; v[v[j]] = 12; System.out.println(v[2]); v[k] = stdin.nextInt();
v 1
v[0]
0
v[1]
8
v[2]
6
v[3]
0
v[4]
0
v[5]
0
v[6]
5
v[7]
12
v[8]
0
v[9]
Consider
int[] v = new int[10]; int i = 7; int j = 2; int k = 4; v[0] = 1; v[i] = 5; v[j] = v[i] + 3; v[j+1] = v[i] + v[0]; v[v[j]] = 12; System.out.println(v[2]); v[k] = stdin.nextInt();
v 1
v[0]
8 is displayed
0
v[1]
8
v[2]
6
v[3]
0
v[4]
0
v[5]
0
v[6]
5
v[7]
12
v[8]
0
v[9]
Consider
int[] v = new int[10]; int i = 7; int j = 2; int k = 4; v[0] = 1; v[i] = 5; v[j] = v[i] + 3; v[j+1] = v[i] + v[0]; v[v[j]] = 12; System.out.println(v[2]); v[k] = stdin.nextInt();
v 1
v[0]
Suppose 3 is extracted
0
v[1]
8
v[2]
6
v[3]
3
v[4]
0
v[5]
0
v[6]
5
v[7]
12
v[8]
0
v[9]
Consider
Segment
int[] b = new int[100]; b[-1] = 0; b[100] = 0;
Causes
IndexOutOfBoundsException
Consider
Point[] p = new Point[3]; p[0] = new Point(0, 0); p[1] = new Point(1, 1); p[2] = new Point(2, 2); p[0].setX(1); p[1].setY(p[2].getY()); Point vertex = new Point(4,4); p[1] = p[0]; p[2] = vertex;
Consider
Point[] p = new Point[3]; p[0] = new Point(0, 0); p[1] = new Point(1, 1); p[2] = new Point(2, 2); p[0].setX(1); p[1].setY(p[2].getY()); Point vertex = new Point(4,4); p[1] = p[0]; p[2] = vertex;
p[0] p[1] p[2]
Point: (0, 0)
Point: (1, 1)
Point: (2, 2)
Consider
Point[] p = new Point[3]; p[0] = new Point(0, 0); p[1] = new Point(1, 1); p[2] = new Point(2, 2); p[0].setX(1); p[1].setY(p[2].getY()); Point vertex = new Point(4,4); p[1] = p[0]; p[2] = vertex;
p[0] p[1] p[2]
Point: (1, 0)
Point: (1, 1)
Point: (2, 2)
Consider
Point[] p = new Point[3]; p[0] = new Point(0, 0); p[1] = new Point(1, 1); p[2] = new Point(2, 2); p[0].setX(1); p[1].setY(p[2].getY()); Point vertex = new Point(4,4); p[1] = p[0]; p[2] = vertex;
p[0] p[1] p[2]
Point: (1, 0)
Point: (1, 2)
Point: (2, 2)
Consider
Point[] p = new Point[3]; p[0] = new Point(0, 0); p[1] = new Point(1, 1); p[2] = new Point(2, 2); p[0].setX(1); p[1].setY(p[2].getY()); Point vertex = new Point(4,4); p[1] = p[0]; vertex p[2] = vertex;
p[0] p[1] p[2]
Point: (4, 4)
Point: (1, 0)
Point: (1, 2)
Point: (2, 2)
Consider
Point[] p = new Point[3]; p[0] = new Point(0, 0); p[1] = new Point(1, 1); p[2] = new Point(2, 2); p[0].setX(1); p[1].setY(p[2].getY()); Point vertex = new Point(4,4); p[1] = p[0]; vertex p[2] = vertex;
p[0] p[1] p[2]
Point: (4, 4)
Point: (1, 0)
Point: (2, 2)
Consider
Point[] p = new Point[3]; p[0] = new Point(0, 0); p[1] = new Point(1, 1); p[2] = new Point(2, 2); p[0].setX(1); p[1].setY(p[2].getY()); Point vertex = new Point(4,4); p[1] = p[0]; vertex p[2] = vertex;
p[0] p[1] p[2]
Point: (4, 4)
Point: (1, 0)
Explicit initialization
Syntax
id references an array of n elements. id[0] has value exp0 , id[1] has value exp1, and so on.
Explicit initialization
Example
String[] puppy = { "nilla, darby, "galen", "panther" }; int[] unit = { 1 };
Equivalent to
String[] puppy = new String[4]; puppy[0] = "nilla"; puppy[1] = darby"; puppy[2] = "galen"; puppy[4] = "panther"; int[] unit = new int[1]; unit[0] = 1;
Array members
Member length
Array members
Member clone()
Array members
Member clone()
Point: (0, 0)
Point: (1, 1)
v
v[0] v[1]
Array members
Member clone()
Point: (0, 0)
Point: (1, 1)
v
v[0] v[1]
Example
Point[] w = new Point[u.length]; for (int i = 0; i < u.length; ++i) { w[i] = u[i].clone(); }
Point: (0, 0)
w[0] w[1] w[2]
Point: (2, 1)
Point: (2, 2)
Point: (0, 0)
Point: (2, 1)
Point: (2, 2)
if (i != data.length) { System.out.println(key + " is the " + i + "-th element"); } else { System.out.println(key + " is not in the list"); }
data key i
4 5 -
if (i != data.length) { System.out.println(key + " is the " + i + "-th element"); } else { System.out.println(key + " is not in the list"); }
data key i
4 5 0
if (i != data.length) { System.out.println(key + " is the " + i + "-th element"); } else { System.out.println(key + " is not in the list"); }
data key i
4 5 0
if (i != data.length) { System.out.println(key + " is the " + i + "-th element"); } else { System.out.println(key + " is not in the list"); }
data key i
4 5 0
if (i != data.length) { System.out.println(key + " is the " + i + "-th element"); } else { System.out.println(key + " is not in the list"); }
data key i
4 5 1
if (i != data.length) { System.out.println(key + " is the " + i + "-th element"); } else { System.out.println(key + " is not in the list"); }
data key i
4 5 1
if (i != data.length) { System.out.println(key + " is the " + i + "-th element"); } else { System.out.println(key + " is not in the list"); }
data key i
4 5 1
if (i != data.length) { System.out.println(key + " is the " + i + "-th element"); } else { System.out.println(key + " is not in the list"); }
data key i
4 5 2
if (i != data.length) { System.out.println(key + " is the " + i + "-th element"); } else { System.out.println(key + " is not in the list"); }
data key i
4 5 2
if (i != data.length) { System.out.println(key + " is the " + i + "-th element"); } else { System.out.println(key + " is not in the list"); }
data key i
4 5 2
if (i != data.length) { System.out.println(key + " is the " + i + "-th element"); } else { System.out.println(key + " is not in the list"); }
data key i
4 5 2
if (i != data.length) { System.out.println(key + " is the " + i + "-th element"); } else { System.out.println(key + " is not in the list"); }
data key i
4 5 2
if (i != data.length) { System.out.println(key + " is the " + i + "-th element"); } else { System.out.println(key + " is not in the list"); }
Segment
int minimumSoFar = sample[0]; for (int i = 1; i < sample.length; ++i) { if (sample[i] < minimumSoFar) { minimumSoFar = sample[i]; } }
Consider
int[] score = { 6, 9, 82, 11, 29, 85, 11, 28, 91 }; int i1 = sequentialSearch(score, 11); int i2 = sequentialSearch(score, 30);
return -1; }
data
82
11
29
85
11
29
91
Consider
int[] score = { 6, 9, 82, 11, 29, 85, 11, 28, 91 }; int i1 = sequentialSearch(score, 11); int i2 = sequentialSearch(score, 30);
Consider
int[] score = { 6, 9, 82, 11, 29, 85, 11, 28, 91 }; putList(score);
for (int i = 0; (stdin.hasNextInt()) && i < MAX_LIST_SIZE; ++i) { buffer[i] = stdin.nextInt(); ++listSize; } int[] data = new int[listSize]; for (int i = 0; i < listSize; ++i) { data[i] = buffer[i]; }
return data;
ArrayTools.java outline
public class ArrayTools { // class constant private static final int MAX_LIST_SIZE = 1000;
// sequentialSearch(): examine unsorted list for key public static int binarySearch(int[] data, int key) { ...
// valueOf(): produces a string representation public static void putList(int[] data) { ... // getList(): extract and return up to MAX_LIST_SIZE values public static int[] getList() throws IOException { ... // reverse(): reverses the order of the element values public static void reverse(int[] list) { ... // binarySearch(): examine sorted list for a key public static int binarySearch(char[] data, char key) { ...
Demo.java
import java.util.*; public class Demo { // main(): application entry point public static void main(String[] args) { System.out.println(""); System.out.println("Enter list of integers:"); int[] number = ArrayTools.getList(); System.out.println(""); System.out.println("Your list"); ArrayTools.putList(number); ArrayTools.reverse(number); System.out.println(""); System.out.println("Your list in reverse"); ArrayTools.putList(number); System.out.println(); } }
Sorting
Problem
Arranging elements so that they are ordered according to some desired scheme
Major tasks
Selection sorting
Algorithm basis
On iteration i, a selection sorting method Finds the element containing the ith smallest value of its list v and exchanges that element with v[i]
Example iteration 0
Swaps smallest element with v[0] This results in smallest element being in the correct place for a sorted result
0 1 2 3 4 5 6 7 8 9
'E'
'W'
'Q'
'R'
'T'
'Y'
'U'
'I'
'O'
'P'
Selection sorting
Algorithm basis
On iteration i, a selection sorting method Finds the element containing the ith smallest value of its list v and exchanges that element with v[i]
Example iteration 0
Swaps smallest element with v[0] This results in smallest element being in the correct place for a sorted result
0 1 2 3 4 5 6 7 8 9
'E'
'W'
'Q'
'R'
'T'
'Y'
'U'
'I'
'O'
'P'
Selection sorting
Algorithm basis
On iteration i, a selection sorting method Finds the element containing the ith smallest value of its list v and exchanges that element with v[i]
Example iteration 1
Swaps second smallest element with v[1] This results in second smallest element being in the correct place for a sorted result
0 1 2 3 4 5 6 7 8 9
'E'
'W'
'Q'
'R'
'T'
'Y'
'U'
'I'
'O'
'P'
Selection sorting
Algorithm basis
On iteration i, a selection sorting method Finds the element containing the ith smallest value of its list v and exchanges that element with v[i]
Example iteration 1
Swaps second smallest element with v[1] This results in second smallest element being in the correct place for a sorted result
0 1 2 3 4 5 6 7 8 9
'E'
'I'
'Q'
'R'
'T'
'Y'
'U'
'W'
'O'
'P'
In a linear (sequential) search, if the number of entries in the array is N, there will be N comparisons for an unsuccessful search.
For a successful search, there will be a minimum of 1 and a maximum of N comparisons. On average, there will be N/2 comparisons.
If the values in an array are arranged in ascending or descending order, the array is sorted. For a binary search to be applied, the array must be sorted. The idea behind the binary search is to divide the array elements in half each time, until the particular element is located.
If we are searching for the value 77, we first find the middle position of the array.
We know the array is sorted. Since 77 is larger than 38, the value must be in the right half of the array.
We next search the middle position of the right half of the array, which is index position [6], and locate the value.
Using the binary search, in the worst case, the number of comparisons is the number of times you can divide the array in half. The maximum number of comparisons K is derived by solving the equation N = 2K
log2N = K
// loop to find the searchValue while (low <= high && number[mid] != searchValue) { System.out.println(mid); // for our info if (number[mid] < searchValue) { low = mid + 1; }
Iteration i
// guess the location of the ith smallest element int guess = i; for (int j = i+1; j < v.length; ++j) { if (v[j] < v[guess]) // is guess ok? // update guess with index of smaller element guess = j; } // guess is now correct, swap elements v[guess] and v[0]
Multidimensional arrays
Matrices Graphical animation Economic forecast models Map representation Time studies of population change Microprocessor design
Example
Segment
int[][] m = new int[3][]; m[0] = new int[4]; m[1] = new int[4]; m[2] = new int[4];
Produces
m[0] m[1] m[2]
m[2][0] m[2][1] m[2][2] m[2][3]
Example
Segment
for (int r = 0; r < m.length; ++r) { for (int c = 0; c < m[r].length; ++c) { System.out.print("Enter a value: "); m[r][c] = stdin.nextInt(); } }
Example
Segment
String[][] s[0] = new s[1] = new s[2] = new s[3] = new s = new String[4][]; String[2]; String[2]; String[4]; String[3];
s[1] s[2] s[3]
Produces
s[0]
null
null null
null null
null
null
null
s[1][0] s[1][1]
Example
Segment
int c[][] = {{1, 2}, {3, 4}, {5, 6}, {7, 8, 9}};
Produces
c[0] c[1] c[2] c[3]
5 1 2 3 4
c[2][0] c[2][1]
c[0][0] c[0][1]
c[1][0] c[1][1]
Matrices
A two-dimensional array is sometimes known as a matrix because it resembles that mathematical concept A matrix a with m rows and n columns is represented mathematically in the following manner
a1 1 a 1 2 a 1 n a2 1 a 2 2 a 2 n am 1 a m 2 a m n
Matrix addition
Matrix addition
public static double[][] add(double[][] a, double[][]b) {