Presentation is loading. Please wait.

Presentation is loading. Please wait.

1 Arrays Spring 2006 UVA - CS 101 Aaron Bloomfield Edited for WLCS by Paul Bui.

Similar presentations


Presentation on theme: "1 Arrays Spring 2006 UVA - CS 101 Aaron Bloomfield Edited for WLCS by Paul Bui."— Presentation transcript:

1 1 Arrays Spring 2006 UVA - CS 101 Aaron Bloomfield Edited for WLCS by Paul Bui

2 2 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

3 3 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 value 0 0000 -c …

4 4 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(); 8 is displayed 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(); Suppose 3 is extracted 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();

5 5 Array variable definition styles  Without initialization Type of values in list Name of list Brackets indicate array variable being defined ElementType [ ] id; int [] a; int a[];

6 6 Array variable definition styles  With initialization ElementType [] id = new ElementType [n]; Nonnegative integer expression specifying the number of elements in the array A new array of n elements

7 7 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

8 8 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

9 9 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

10 10 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 p[0]p[1]p[2] null Point: (0, 0) p p[0]p[1] Point: (1, 1)Point: (2, 2) p[2] Point: (1, 0) p p[0]p[1] Point: (1, 1)Point: (2, 2) p[2] Point: (1, 0) p p[0]p[1] Point: (1, 2)Point: (2, 2) p[2] Point: (1, 0) p p[0]p[1] Point: (1, 2)Point: (2, 2) p[2] vertex Point: (4, 4) Point: (1, 0) p p[0]p[1] Point: (2, 2) p[2] vertex Point: (4, 4) Point: (1, 0) p p[0]p[1]p[2] vertex Point: (4, 4) 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;

11 11 New 2005 demotivatiors!

12 12 Explicit initialization  Syntax ElementType [] id = { exp 0, 1,... exp n }; id references an array of n elements. id[0] has value exp 0, id[1] has value exp 1, and so on. Each exp i is an expression that evaluates to type ElementType

13 13 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;

14 14 Array members  Member length Size of the array for (int i = 0; i < puppy.length; ++i) { System.out.println(puppy[i]); }

15 15 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]);

16 16 How Java represents arrays  Consider int[] a = { 1, 2, 3, 4, 5 }; a 1 2345 + … Array - length = 5 - data = 12345

17 17 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; 1 23450 0000 a - b null cd int[] a; int[] b = null; int[] c = new int[5]; int[] d = { 1, 2, 3, 4, 5 }; a = c; d = c;

18 18 What do these pictures mean? Light beer Light beer Dandy lions Dandy lions Assaulted peanut Assaulted peanut Eggplant Eggplant Dr. Pepper Dr. Pepper Pool table Pool table Tap dancers Tap dancers Card shark Card shark King of pop King of pop I Pod I Pod Gator aide Gator aide Knight mare Knight mare Hole milk Hole milk

19 19 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);

20 20 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));


Download ppt "1 Arrays Spring 2006 UVA - CS 101 Aaron Bloomfield Edited for WLCS by Paul Bui."

Similar presentations


Ads by Google