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

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

Embed Size (px)

DESCRIPTION

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

Citation preview

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

1

Arrays

Spring 2006UVA - CS 101Aaron BloomfieldEdited for WLCS by Paul Bui

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

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

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

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

-c

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

4

An array exampleint[] 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 00 0 00 0 00 00

v[2]v[0] v[1] v[5]v[3] v[4] v[7]v[6] v[9]v[8]

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 01 0 00 0 00 00

v[2]v[0] v[1] v[5]v[3] v[4] v[7]v[6] v[9]v[8]

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 01 0 00 0 50 00

v[2]v[0] v[1] v[5]v[3] v[4] v[7]v[6] v[9]v[8]

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 81 0 00 0 50 00

v[2]v[0] v[1] v[5]v[3] v[4] v[7]v[6] v[9]v[8]

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 81 0 06 0 50 00

v[2]v[0] v[1] v[5]v[3] v[4] v[7]v[6] v[9]v[8]

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 81 0 06 0 50 012

v[2]v[0] v[1] v[5]v[3] v[4] v[7]v[6] v[9]v[8]

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 81 0 06 0 50 012

v[2]v[0] v[1] v[5]v[3] v[4] v[7]v[6] v[9]v[8]

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

v 81 0 06 3 50 012

v[2]v[0] v[1] v[5]v[3] v[4] v[7]v[6] v[9]v[8]

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

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

5

Array variable definition styles Without initialization

Type ofvalues in

list

Name oflist

Bracketsindicate arrayvariable being

defined

ElementType [ ] id;

int [] a;int a[];

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

6

Array variable definition styles With initialization

ElementType[ ] id = new ElementType [n];

Nonnegative integer expression specifying thenumber of elements in the array

A new array of nelements

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

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

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

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

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

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

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

10

ConsiderPoint[] 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 null 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;

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

1111

New 2005 demotivatiors!New 2005 demotivatiors!

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

12

Explicit initialization Syntax

ElementType[] id = { exp0 , exp1 , ... expn-1 };

id references an array of n elements. id[0] hasvalue exp0, id[1] has value exp1, and so on.

Each expi is an expression thatevaluates to type ElementType

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

13

Explicit initialization Example

String[] puppy = { “pika”, “mila”, “arlo”, “nikki” };

int[] unit = { 1 };

Equivalent toString[] puppy = new String[4];puppy[0] = “pika"; puppy[1] = “mila";puppy[2] = “arlo"; puppy[3] = “nikki";

int[] unit = new int[1];unit[0] = 1;

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

14

Array members Member length

Size of the arrayfor (int i = 0; i < puppy.length; ++i) {

System.out.println(puppy[i]);}

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

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

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

16

How Java represents arrays Consider

int[] a = { 1, 2, 3, 4, 5 };

a 1 2 3 4 5+ …

Array

- length = 5- data = 1 2 3 4 5

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

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 2 3 4 5

0 0 0 0 0

a -

b null

c

d

int[] a;int[] b = null;int[] c = new int[5];int[] d = { 1, 2, 3, 4, 5 };a = c;d = c;

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

1818

What do these pictures What do these pictures mean?mean?

Light beerLight beer Dandy lionsDandy lions Assaulted Assaulted

peanutpeanut EggplantEggplant Dr. PepperDr. Pepper Pool tablePool table Tap dancersTap dancers Card sharkCard shark King of popKing of pop I PodI Pod Gator aideGator aide Knight mareKnight mare Hole milkHole milk

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

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

}

Considerint[] score = { 6, 9, 82, 11, 29, 85, 11, 28, 91 };printArray(score);

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

20

Java Array Code Examples Returns a reversed version of the int arraypublic 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;

}

Considerint[] foo = { 1, 2, 3, 4, 5 };printArray( reverse (foo));