Arrays 1 Background o o o Programmer often
Arrays 1
Background o o o Programmer often need the ability to represent a group of values as a list n List may be one-dimensional or multidimensional Java provides arrays and the collection classes n The Vector class is an example of a collection class Consider arrays first 2
Basic terminology o List is composed of elements o Elements in a list have a common name o The list as a whole is referenced through the common name o List elements are of the same type — the base type o Elements of a list are referenced by subscripting (indexing) the common name 3
Java array features o Subscripts are denoted as expressions within brackets: [ ] o Base (element) type can be any type o o o Size of array can be specified at run time n This is different that pure C! (for the most part, at least) Index type is integer and the index range must be 0. . . n-1 n Where n is the number of elements Automatic bounds checking n Ensures any reference to an array element is valid Data field length specifies the number of elements in the list Array is an object n Has features common to all other objects 4
Array variable definition styles o Without initialization Element. Type [ ] id; Brackets Name of Type of list values in indicate array variable being list defined int [] a; int a[]; 5
Array variable definition styles o With initialization Nonnegative integer expression specifying the number of elements in the array Element. Type [ ] id = new Element. Type [n]; Reference to a new array of n elements 6
Example o o Definitions char[] c; int[] value = new int[10]; Causes n Array object variable c is un-initialized n Array object variable v references a new ten element list of integers o Each of the integers is default initialized to 0 c value 0 0 … 0 7
2004 IOCCC winners o o 2004 winners: n 2004 anonymous Rendering of a stroked font n 2004 arachnid Curses maze displayer/navigator with only line-of-sight visibility n 2004 burley A Poker game n 2004 gavare A ray tracer n 2004 gavin Mini-OS n 2004 hibachi A CGI capable HTTP server n 2004 hoyle Curses based polynomial graphing with auto-scale n 2004 jdalbec Conway's look'n'say sequence split into elements n 2004 kopczynski OCR of 8, 9, 10 and 11 n 2004 newbern Renders arbitary bitmapped fonts n 2004 omoikane A CRC inserter n 2004 schnitzi Editor animation n 2004 sds Space/tab/linefeed steganography n 2004 vik 1 X Windows car racing game n 2004 vik 2 Calculates prime numbers using only CPP At http: //www 1. us. ioccc. org/years. html#2004 8
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. next. Int(); 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. next. Int(); 10
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. next. Int(); 11
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. next. Int(); 12
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. next. Int(); 13
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. next. Int(); 14
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. next. Int(); 15
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. next. Int(); 8 is displayed 16
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. next. Int(); Suppose 3 is extracted 17
Consider o o Segment int[] b = new int[100]; b[-1] = 0; b[100] = 0; Causes n Array variable to reference a new list of 100 integers o Each element is initialized to 0 n Two exceptions to be thrown o -1 is not a valid index – too small o 100 is not a valid index – too large n Index. Out. Of. Bounds. Exception 18
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]. set. X(1); p[1]. set. Y(p[2]. get. Y()); Point vertex = new Point(4, 4); p[1] = p[0]; p[2] = vertex; 19
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]. set. X(1); p[1]. set. Y(p[2]. get. Y()); Point vertex = new Point(4, 4); p[1] = p[0]; p[2] = vertex; 20
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]. set. X(1); p[1]. set. Y(p[2]. get. Y()); Point vertex = new Point(4, 4); p[1] = p[0]; p[2] = vertex; 21
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]. set. X(1); p[1]. set. Y(p[2]. get. Y()); Point vertex = new Point(4, 4); p[1] = p[0]; p[2] = vertex; 22
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]. set. X(1); p[1]. set. Y(p[2]. get. Y()); Point vertex = new Point(4, 4); p[1] = p[0]; p[2] = vertex; 23
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]. set. X(1); p[1]. set. Y(p[2]. get. Y()); Point vertex = new Point(4, 4); p[1] = p[0]; p[2] = vertex; 24
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]. set. X(1); p[1]. set. Y(p[2]. get. Y()); Point vertex = new Point(4, 4); p[1] = p[0]; p[2] = vertex; 25
Explicit initialization o Syntax id references an array of n elements. id[0] has value exp 0, id[1] has value exp 1, and so on. Element. Type[] id = { exp 0 , exp 1 , . . . expn-1 } ; Each expi is an expression that evaluates to type Element. Type 26
Explicit initialization o o Example String[] puppy = { “lucia" }; int[] unit = { 1 }; “pika“, “arlo“, “shadow", Equivalent to String[] puppy = new String[4]; puppy[0] = “pika"; puppy[1] = “arlo"; puppy[2] = “shadow"; puppy[4] = “lucia"; int[] unit = new int[1]; unit[0] = 1; 27
Empty set: an example 28
Array members o Member length n Size of the array for (int i = 0; i < puppy. length; ++i) { System. out. println(puppy[i]); } 29
Array members o Member clone() n Produces a shallow copy Point[] u = { new Point(0, 0), new Point(1, 1)}; Point[] v = u. clone(); v[1] = new Point(4, 30); 30
Array members o Member clone() n Produces a shallow copy Point[] u = { new Point(0, 0), new Point(1, 1)}; Point[] v = u. clone(); v[1] = new Point(4, 30); 31
Array members o Member clone() n Produces a shallow copy Point[] u = { new Point(0, 0), new Point(1, 1)}; Point[] v = u. clone(); v[1] = new Point(4, 30); 32
Array members o Member clone() n Produces a shallow copy Point[] u = { new Point(0, 0), new Point(1, 1)}; Point[] v = u. clone(); v[1] = new Point(4, 30); 33
Making a deep copy o Example Point[] w = new Point[u. length]; for (int i = 0; i < u. length; ++i) { w[i] = (Point) u[i]. clone(); } 34
Making a deep copy 35
Searching for a value System. out. println("Enter search value (number): "); int key = stdin. next. Int(); int i; for (i = 0; i < data. length; ++i) { if (key == data[i]) { break; } } if (i != data. length) { System. out. println(key + " is the " + I + "-th element"); } else { System. out. println(key + " is not in the list"); 37 }
Searching for a value System. out. println("Enter search value (number): "); int key = stdin. next. Int(); int i; for (i = 0; i < data. length; ++i) { if (key == data[i]) { break; } } if (i != data. length) { System. out. println(key + " is the " + I + "-th element"); } else { System. out. println(key + " is not in the list"); 38 }
Searching for a value System. out. println("Enter search value (number): "); int key = stdin. next. Int(); int i; for (i = 0; i < data. length; ++i) { if (key == data[i]) { break; } } if (i != data. length) { System. out. println(key + " is the " + I + "-th element"); } else { System. out. println(key + " is not in the list"); 39 }
Searching for a value System. out. println("Enter search value (number): "); int key = stdin. next. Int(); int i; for (i = 0; i < data. length; ++i) { if (key == data[i]) { break; } } if (i != data. length) { System. out. println(key + " is the " + I + "-th element"); } else { System. out. println(key + " is not in the list"); 40 }
Searching for a value System. out. println("Enter search value (number): "); int key = stdin. next. Int(); int i; for (i = 0; i < data. length; ++i) { if (key == data[i]) { break; } } if (i != data. length) { System. out. println(key + " is the " + I + "-th element"); } else { System. out. println(key + " is not in the list"); 41 }
Searching for a value System. out. println("Enter search value (number): "); int key = stdin. next. Int(); int i; for (i = 0; i < data. length; ++i) { if (key == data[i]) { break; } } if (i != data. length) { System. out. println(key + " is the " + I + "-th element"); } else { System. out. println(key + " is not in the list"); 42 }
Searching for a value System. out. println("Enter search value (number): "); int key = stdin. next. Int(); int i; for (i = 0; i < data. length; ++i) { if (key == data[i]) { break; } } if (i != data. length) { System. out. println(key + " is the " + I + "-th element"); } else { System. out. println(key + " is not in the list"); 43 }
Searching for a value System. out. println("Enter search value (number): "); int key = stdin. next. Int(); int i; for (i = 0; i < data. length; ++i) { if (key == data[i]) { break; } } if (i != data. length) { System. out. println(key + " is the " + I + "-th element"); } else { System. out. println(key + " is not in the list"); 44 }
Searching for a value System. out. println("Enter search value (number): "); int key = stdin. next. Int(); int i; for (i = 0; i < data. length; ++i) { if (key == data[i]) { break; } } if (i != data. length) { System. out. println(key + " is the " + I + "-th element"); } else { System. out. println(key + " is not in the list"); 45 }
Searching for a value System. out. println("Enter search value (number): "); int key = stdin. next. Int(); int i; for (i = 0; i < data. length; ++i) { if (key == data[i]) { break; } } if (i != data. length) { System. out. println(key + " is the " + I + "-th element"); } else { System. out. println(key + " is not in the list"); 46 }
Searching for a value System. out. println("Enter search value (number): "); int key = stdin. next. Int(); int i; for (i = 0; i < data. length; ++i) { if (key == data[i]) { break; } } if (i != data. length) { System. out. println(key + " is the " + I + "-th element"); } else { System. out. println(key + " is not in the list"); 47 }
Searching for a value System. out. println("Enter search value (number): "); int key = stdin. next. Int(); int i; for (i = 0; i < data. length; ++i) { if (key == data[i]) { break; } } if (i != data. length) { System. out. println(key + " is the " + I + "-th element"); } else { System. out. println(key + " is not in the list"); 48 }
Searching for a value System. out. println("Enter search value (number): "); int key = stdin. next. Int(); int i; for (i = 0; i < data. length; ++i) { if (key == data[i]) { break; } } if (i != data. length) { System. out. println(key + " is the " + I + "-th element"); } else { System. out. println(key + " is not in the list"); 49 }
Searching for the minimum value o Segment int minimum. So. Far = sample[0]; for (int i = 1; i < sample. length; ++i) { if (sample[i] < minimum. So. Far) { minimum. So. Far = sample[i]; } } 50
Array. Tools. java method sequential. Search() public static int sequential. Search(int[] data, int key) { for (int i = 0; i < data. length; ++i) { if (data[i] == key) { return i; } } return -1; } o Consider int[] score = { 6, 9, 82, 11, 29, 85, 11, 28, 91 }; int i 1 = sequential. Search(score, 11); 51 int i 2 = sequential. Search(score, 30);
Array. Tools. java method put. List() public static void put. List(int[] data) { for (int i = 0; i < data. length; ++i) { System. out. println(data[i]); } } o Consider int[] score = { 6, 9, 82, 11, 29, 85, 11, 28, 91 }; put. List(score); 52
public static int[] get. List() { Scanner stdin = new Scanner (System. in); int[] buffer = new int[MAX_LIST_SIZE]; int list. Size = 0; for (int i = 0; i < MAX_LIST_SIZE; ++i) { String v = stdin. next. Line(); if (v != null) { int number = Integer. parse. Int(v); buffer[i] = number; ++list. Size; } else { break; } Array. Tools. java } method get. List() int[] data = new int[list. Size]; for (int i = 0; i < list. Size; ++i) { data[i] = buffer[i]; } return data; } 53
Array. Tools. java – outline o In java. util public class Array. Tools { // class constant private static final int MAX_LIST_SIZE = 1000; // sequential. Search(): examine unsorted list for key public static int binary. Search(int[] data, int key) {. . . // value. Of(): produces a string representation public static void put. List(int[] data) {. . . // get. List(): extract and return up to MAX_LIST_SIZE values public static int[] get. List() throws IOException {. . . // reverse(): reverses the order of the element values public static void reverse(int[] list) {. . . // binary. Search(): examine sorted list for a key public static int binary. Search(char[] data, char key) {54. . . }
Demo. java import java. io. *; public class Demo { // main(): application entry point public static void main(String[] args) throws IOException { System. out. println(""); System. out. println("Enter list of integers: "); int[] numbers = Array. Tools. get. List(); System. out. println(""); System. out. println("Your list"); Array. Tools. put. List(numbers); Array. Tools. reverse(numbers); System. out. println(""); System. out. println("Your list in reverse"); Array. Tools. put. List(numbers); System. out. println(); } } 55
56
Microsoft and patents o … 57
Sorting o o Problem n Arranging elements so that they are ordered according to some desired scheme o Standard is non-decreasing order n Why don't we say increasing order? Major tasks n Comparisons of elements n Updates or element movement 58
Selection sorting o o Algorithm basis n On iteration i, a selection sorting method o Finds the element containing the ith smallest value of its list v and exchanges that element with v[i] Example – iteration 0 n Swaps smallest element with v[0] n This results in smallest element being in the correct place for a sorted result 59
Selection sorting o o Algorithm basis n On iteration i, a selection sorting method o Finds the element containing the ith smallest value of its list v and exchanges that element with v[i] Example – iteration 0 n Swaps smallest element with v[0] n This results in smallest element being in the correct place for a sorted result 60
Selection sorting o o Algorithm basis n On iteration i, a selection sorting method o Finds the element containing the ith smallest value of its list v and exchanges that element with v[i] Example – iteration 1 n Swaps second smallest element with v[1] n This results in second smallest element being in the correct place for a sorted result 61
Selection sorting o o Algorithm basis n On iteration i, a selection sorting method o Finds the element containing the ith smallest value of its list v and exchanges that element with v[i] Example – iteration 1 n Swaps second smallest element with v[1] n This results in second smallest element being in the correct place for a sorted result 62
Array. Tools. java selection sorting public static void selection. Sort(char[] v) { for (int i = 0; i < v. length-1; ++i) { // find the location of the ith smallest element int spot = i; for (int j = i+1; j < v. length; ++j) { if (v[j] < v[spot]) { // is current location ok? // update spot to index of smaller element spot = j; } } // spot is now correct, so swap elements char rmbr = v[i]; v[i] = v[spot]; v[spot] = rmbr; } } 63
Iteration i // find the location of the ith smallest element int spot = i; for (int j = i+1; j < v. length; ++j) { if (v[j] < v[spot]) // is spot ok? // update spot with index of smaller element spot = j; } // spot is now correct, swap elements v[spot] and v[0] 64
Multidimensional arrays o o Many problems require information be organized as a twodimensional or multidimensional list Examples n Matrices n Graphical animation n Economic forecast models n Map representation n Time studies of population change n Microprocessor design 65
Example o o Segment int[][] m = new int[3][]; m[0] = new int[4]; m[1] = new int[4]; m[2] = new int[4]; Produces When an array is created, each value is initialized! 66
Example o o Alternative int[][] m = new int[3][4]; Produces 67
Multidimensional array visualization o o A multi-dimensional array declaration (either one): int[][] m = new int[3][4]; Produces 0 0 0 or 0 0 0 68
Example o 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. next. Int(); } } 70
Example o o Segment String[][] s[0] = new s[1] = new s[2] = new s[3] = new Produces s = new String[4][]; String[2]; String[4]; String[3]; 71
Multidimensional array visualization o o Segment String[][] s[0] = new s[1] = new s[2] = new s[3] = new Produces s = new String[4][]; String[2]; String[4]; String[3]; 0 0 0 0 0 or 0 0 0 o Called a “ragged” array 0 72
Example o o Segment int c[][] = {{1, 2}, {3, 4}, {5, 6}, {7, 8, 9}}; Produces 73
Matrices o o 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 74
Matrix addition o Definition C = A + B n cij = aij + bij n cij is sum of the elements in the same row and column of A and B 75
Matrix addition public static double[][] add(double[][] a, double[][] b) { // determine number of rows in solution int m = a. length; // determine number of columns in solution int n = a[0]. length; // create the array to hold the sum double[][] c = new double[m][n]; // compute the matrix sum row by row for (int i = 0; i < m; ++i) { // produce the current row for (int j = 0; j < n; ++j) { c[i][j] = a[i][j] + b[i][j]; } } return c; } 76
Today’s dose of demotivators 77
- Slides: 75