09-arrays

09-arrays - COMP 202 COMP 202 Introduction to Computing 1...

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

View Full Document Right Arrow Icon
COMP 202 – Introduction to Computing 1 COMP 202 - Arrays 1 COMP 202 Programming With Arrays CONTENTS : • Arrays, 2D Arrays, Multidimensional Arrays • The Array List • Variable Length parameter lists • The For-each Statement Thinking Like A Programmer : Designing for 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
COMP 202 – Introduction to Computing 1 COMP 202 - Arrays 2 Part 1 Array Basics
Background image of page 2
COMP 202 – Introduction to Computing 1 COMP 202 - Arrays 3 Arrays • An array is an ordered list of values 0 1 2 3 4 5 6 7 8 9 79 87 94 82 67 98 87 81 74 91 An array of size N is indexed from zero to N-1 marks The entire array has a single name Each value has a numeric index This array holds 10 values that are indexed from 0 to 9
Background image of page 3

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

View Full DocumentRight Arrow Icon
COMP 202 – Introduction to Computing 1 COMP 202 - Arrays 4 Arrays A particular value in an array is referenced using the array name followed by the index in brackets For example, the expression System.out.println(marks[2]); refers to the value 94 (which is the 3rd value in the array) That expression represents a place to store a single integer, and can be used wherever an integer variable can For example, it can be assigned a value, printed, or used in a calculation
Background image of page 4
COMP 202 – Introduction to Computing 1 COMP 202 - Arrays 5 Arrays • An array stores multiple values of the same type – That type can be primitive types or objects – Therefore, we can create an array of integers, or an array of characters, or an array of String objects, etc. • In Java, the array itself is an object – Therefore the name of the array is an object reference variable, and the array itself is instantiated separately
Background image of page 5

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

View Full DocumentRight Arrow Icon
COMP 202 – Introduction to Computing 1 COMP 202 - Arrays 6 Declaring Arrays The marks array could be declared as follows: int[] marks = new int[10]; Note: – that the type of the array does not specify its size, but each object of that type has a specific size – The type of the variable marks is int[] (an array of integers) – It is set to a new array object that can hold 10 integers
Background image of page 6
COMP 202 – Introduction to Computing 1 COMP 202 - Arrays 7 Array Object with elements of primitive type (e.g., integer) 0 1 2 3 4 5 6 7 8 9 79 87 94 82 67 98 87 81 74 91 marks
Background image of page 7

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

View Full DocumentRight Arrow Icon
COMP 202 - Arrays 8 Why Arrays? Imagine you want to write a program that asks the user to enter in 50 student midterm marks. The program would then display each grade with the student’s ID number and then the average for that assignment in a nice report. How would you do it? – One way would be to provide 100 variables: • int mark1, mark2, mark3, (until 50) • int ID1, ID2, ID3, (until 50) – The array provides for a way to create only two variables: • int marks[] = new int[50]; • int IdNumbers[] = new int[50]; – Isn’t this nicer? – Imagine trying to calculate the average with 50 variables…
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.

This note was uploaded on 12/29/2010 for the course COMP 202 taught by Professor Verbrugge during the Fall '07 term at McGill.

Page1 / 54

09-arrays - COMP 202 COMP 202 Introduction to Computing 1...

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