12Arrays - CSE 114 Computer Science I Arrays French...

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

View Full Document Right Arrow Icon
French Territory of St. Pierre CSE 114 – Computer Science I Arrays
Background image of page 1

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

View Full Document Right Arrow Icon
Arrays An array is an single-named and ordered collection of data values of the same type. Why is this important? We can store multiple data values (primitive types or objects) easily We can add to, change, remove from, and refer to this data dynamically Declaration of 7 double s and 100 int s: double score1, … score7; int grade1, grade2, … grade100; double[] score; score = new double[7]; int[] grade = new int[100]; Reserves memory for this much data
Background image of page 2
double[] score; score = new double[7]; Arrays in Memory 0 1 2 99 grade: 0 1 2 3 4 5 6 score: indexes values int[] grade = new int[100];
Background image of page 3

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

View Full Document Right Arrow Icon
int index; for (index=0; index<7; index++) score[index] = 6.0; Assign values to and access values from array variables using a subscript (index): grade[12] = 79; grade[13] = grade[12] + 10; Array assignments 89 0 12 99 grade: 79 13 6.0 0 1 2 3 4 5 6 score: 6.0 6.0 6.0 6.0 6.0 6.0 NOTE: Array subscripts start with 0!!!! An array of size N has subscripts 0, 1, …, N-1.
Background image of page 4
More about Arrays Array size is static (cannot change after instantiation) BUT array size does not have to be defined with a constant: int[] grade; int numGrades; System.out.println ("How many grades do you have?"); numGrades = keyboard.nextInt(); grade = new int[numGrades]; Initializing arrays: double[] price = {1.25, 3.15, 2.08}; int[] areaCode = new int[10]; int j; for (j=0; j<10; j++) areaCode[j]=631; What’s wrong with this? for (j=0; j<=10; j++) areaCode[j]=631; ArrayIndexOutOfBoundsException
Background image of page 5

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

View Full Document Right Arrow Icon
Arrays and Objects An array is an object (basically): int[] grade = new int[5]; grade[0] grade[1] grade[2] MEMORY grade etc… public instance variable: length length represents the capacity of an array for (j=0; j<grade.length; j++) grade[j] = 100-j; 100 99 98
Background image of page 6
Subscript out of Range Error Using a subscript larger than length-1 or smaller than 0 causes a run time (not a compiler) error an ArrayIndexOutOfBoundsException is thrown you need to fix the problem and recompile your code Other programming languages, e.g. C and C++, do not even cause a run time error!
Background image of page 7

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

View Full Document Right Arrow Icon
Image of page 8
This is the end of the preview. Sign up to access the rest of the document.

{[ snackBarMessage ]}

Page1 / 22

12Arrays - CSE 114 Computer Science I Arrays French...

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

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