lecture8 - PIC 20A Lecture 8: Arrays What is an array? a...

Info iconThis preview shows pages 1–9. Sign up to view the full content.

View Full Document Right Arrow Icon
PIC 20A Lecture 8: Arrays
Background image of page 1

Info iconThis preview has intentionally blurred sections. Sign up to view the full version.

View Full DocumentRight Arrow Icon
What is an array? • a data structure that consists of a sequence of related data items of the same type. • a “static” entity (ie remains the same size once it is created) • an object so must be allocated with new
Background image of page 2
What is the type of an array? • the element type, followed by [] • you can declare an array of any type eg double[] int[] boolean[] String[] Rectangle[]
Background image of page 3

Info iconThis preview has intentionally blurred sections. Sign up to view the full version.

View Full DocumentRight Arrow Icon
Ways to declare an array: 1. // declare array first type[] name; // then create an array of “zeroes” name = new type[length]; 2. type[] name = new type[length];
Background image of page 4
Ways to declare an array: 3. type[] name1, name2, . .. , nameN; name1 = new type [length1]; //etc… nameN = new type [lengthN]; 4. type name1[] = new type[length1], \\etc. . nameN[] = new type[lengthN];
Background image of page 5

Info iconThis preview has intentionally blurred sections. Sign up to view the full version.

View Full DocumentRight Arrow Icon
Ways to declare an array: 5. type[] arrayname = {val1, val2, . .., valN}; The expression {val1, val2, . .., valN} is called an initializer list
Background image of page 6
Array facts • an array reference may be reassigned to a new array of a different size eg int[] nums = new int[10]; nums = new int[5];
Background image of page 7

Info iconThis preview has intentionally blurred sections. Sign up to view the full version.

View Full DocumentRight Arrow Icon
Array facts • first element in any array is at index 0 i th element has index ( i-1 ) • last element is at index ( length-1 ) • array index must be an int or an int expression
Background image of page 8
Image of page 9
This is the end of the preview. Sign up to access the rest of the document.

Page1 / 25

lecture8 - PIC 20A Lecture 8: Arrays What is an array? a...

This preview shows document pages 1 - 9. Sign up to view the full document.

View Full Document Right Arrow Icon
Ask a homework question - tutors are online