12ArraysIntroLarge

12ArraysIntroLarge - Lecture Set #12: Arrays This lecture...

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

View Full Document Right Arrow Icon
1 CMSC 131 Fall 2007 Jan Plane (adapted from Bonnie Dorr) Lecture Set #12: Arrays This lecture set: c Intro to arrays c Copying arrays and making arrays bigger c Array lengths and out-of-bounds indexing c Passing arrays and array elements to a function c Privacy Leaks c Different levels of copy CMSC 131 Fall 2007 Jan Plane (adapted from Bonnie Dorr) 1 Data Structures and Arrays c Data structures : mechanisms for storing data in a structured way c We have seen simple data structures implemented as classes: c Rational.java c Rational number data stored as numerator / denominator pair c Arrays are a very useful data structure provided by Java and other programming languages c Array: sequence of variables of the same type c homogeneous data structure c size (quantity) fixed when space is allocated c ordered c Individual elements of sequence can be referenced/updated/etc. c Arrays are objects (hence allocated on heap) with a reference on the stack c Like other objects, “instance variables” of array = cells in array are assigned default values (0 / null / etc.) when array created
Background image of page 1

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

View Full DocumentRight Arrow Icon
2 CMSC 131 Fall 2007 Jan Plane (adapted from Bonnie Dorr) 2 Array Indexing c Java provides a special syntax for uniformly accessing cells in an array c Declaration of a: int[] a; c Allocation of space for array named a: a = new int[5]; c This creates five int variables “named”:: a[0],a[1],a[2],a[3],a[4] c To modify contents of cell #2 to 6 and cell #1 to 74: a[2] = 6; a[1] = 74; c To use the contents of cell #2 and cell #1 : System.out.println(“value = “ + (a[1]-a[2])); c This access mechanism to the individual elements is called array indexing c In Java / C / C++, array cells are indexed beginning at 0 and going up to n-1 (n is number of cells) c Beware: start at 0! and end at one less than the size!! CMSC 131 Fall 2007 Jan Plane (adapted from Bonnie Dorr) 3 Square Brackets: [ ] and length c Three uses in Java: c Array variable declaration int[] a; c Array object creation new int[10]; c Array indexing a[0] c array also has a.length holds the amount of space currently allocated for that array
Background image of page 2
3 CMSC 131 Fall 2007 Jan Plane (adapted from Bonnie Dorr) 4 Alternate Declaration Syntax c To maintain consistency with C / C++, following declaration of array variables also possible
Background image of page 3

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

View Full DocumentRight Arrow Icon
Image of page 4
This is the end of the preview. Sign up to access the rest of the document.

Page1 / 11

12ArraysIntroLarge - Lecture Set #12: Arrays This lecture...

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

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