200 likes | 352 Views
Arrays. Spring 2006 UVA - CS 101 Aaron Bloomfield Edited for WLCS by Paul Bui. Background. Programmers often need the ability to represent a group of values as a list List may be one-dimensional or multidimensional Java provides arrays and the collection classes. c. -. …. value. 0.
E N D
Arrays Spring 2006 UVA - CS 101 Aaron Bloomfield Edited for WLCS by Paul Bui
Background • Programmers often need the ability to represent a group of values as a list • List may be one-dimensional or multidimensional • Java provides arrays and the collection classes
c - … value 0 0 0 0 0 Example • Definitions char[] c; int[] value = new int[10]; • Causes • Array object variable c is un-initialized • Array object variable value references a new ten element list of integers • Each of the integers is default initialized to 0
An array example 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(); 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(); 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(); 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(); 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(); 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(); 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(); 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(); 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(); 8 is displayed Suppose 3 is extracted
ElementType [ ] id; Brackets Name of Type of indicate array list values in variable being list defined Array variable definition styles • Without initialization int [] a; int a[];
Nonnegative integer expression specifying the number of elements in the array ElementType id ElementType [n]; = new [ ] A new array of n elements Array variable definition styles • With initialization
Basic terminology • List is composed of elements • Elements in a list have a common name • Example: a[3] = 5; • The common name is ‘a’ • The list as a whole is referenced through the common name • List elements are of the same type — the base type • Elements of a list are referenced by subscripting (indexing) the common name
Java array features • Subscripts are denoted as expressions within brackets: [ ] • Base (element) type can be any type • Size of array must be specified • Index type is integer and the index range must be 0 ... n-1 • Where n is the number of elements • Just like Strings indexing! • Automatic bounds checking • Ensures any reference to an array element is valid • Data field “.length” specifies the number of elements in the list
Consider • Segment int[] b = new int[100]; b[-1] = 0; b[100] = 0; • Causes • Array variable to reference a new list of 100 integers • Each element is initialized to 0 • Two exceptions to be thrown • -1 is not a valid index – too small • 100 is not a valid index – too large • IndexOutOfBoundsException
vertex vertex vertex p[0] p[0] p[0] p[0] p[0] p[0] p[0] p[1] p[1] p[1] p[1] p[1] p[1] p[1] p[2] p[2] p[2] p[2] p[2] p[2] p[2] null null null p p p p p p p Point: (4, 4) Point: (4, 4) Point: (4, 4) Point: (1, 0) Point: (0, 0) Point: (1, 0) Point: (1, 0) Point: (1, 0) Point: (1, 0) Point: (1, 1) Point: (1, 2) Point: (1, 1) Point: (1, 2) Point: (2, 2) Point: (2, 2) Point: (2, 2) Point: (2, 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]; p[2] = vertex; 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;
id references an array of n elements. id[0] has value exp0, id[1] has value exp1, and so on. Each expiis an expression that evaluates to type ElementType Explicit initialization • Syntax ElementType id exp , exp , ... exp [] = { } ; 0 1 -1 n
Explicit initialization • Example String[] puppy = { “pika”, “mila”, “arlo”, “nikki” }; int[] unit = { 1 }; • Equivalent to String[] puppy = new String[4]; puppy[0] = “pika"; puppy[1] = “mila"; puppy[2] = “arlo"; puppy[3] = “nikki"; int[] unit = new int[1]; unit[0] = 1;
Array members • Member length • Size of the array for (int i = 0; i < puppy.length; ++i) { System.out.println(puppy[i]); }
Review of arrays • Creating an array: int[] foo = new int[10]; • Accessing an array: foo[3] = 7; System.out.print (foo[1]); • Creating an array: String[] bar = new String[10]; • Accessing an array: bar[3] = “qux”; System.out.println (bar[1]);
Array - length = 5 - data = + … 1 2 3 4 5 1 2 3 4 5 How Java represents arrays • Consider int[] a = { 1, 2, 3, 4, 5 }; a
c d b null a 1 0 2 0 3 0 4 0 5 0 More about how Java represents Arrays • Consider int[] a; int[] b = null; int[] c = new int[5]; int[] d = { 1, 2, 3, 4, 5 }; a = c; d = c; int[] a; int[] b = null; int[] c = new int[5]; int[] d = { 1, 2, 3, 4, 5 }; a = c; d = c; -
What do these pictures mean? • Light beer • Dandy lions • Assaulted peanut • Eggplant • Dr. Pepper • Pool table • Tap dancers • Card shark • King of pop • I Pod • Gator aide • Knight mare • Hole milk
Java Array Code Examples • To print the array: public void printArray(int[] data) { for (int i = 0; i < data.length; ++i) { System.out.println(data[i]); } } • Consider int[] score = {6,9,82,11,29,85,11,28, 91}; printArray(score);
Java Array Code Examples • Returns a reversed version of the int array public int[] reverse(int[] data) { int[] clone = data.clone(); for ( int i = 0; i < clone.length; ++i ) { clone[i] = data[data.length-1-i]; } return clone; } • Consider int[] foo = { 1, 2, 3, 4, 5 }; printArray( reverse (foo));