Lec 10 Arrays

Lec 10 Arrays - FIT1002 Computer Programming FIT1002...

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

View Full Document Right Arrow Icon
FIT1002 Computer Programming Module 10 Arrays www.monash.edu.au FIT1002 Computer Programming Module 10 Arrays COMMONWEALTH OF AUSTRALIA Copyright Regulations 1969 WARNING This material has been reproduced and communicated to you by or on behalf of Monash University pursuant to Part VB of the Copyright Act 1968 (the Act). The material in this communication may be subject to copyright under the Act. Any further reproduction or communication of this material by you may be the subject of copyright protection under the Act. Do not remove this notice. www.monash.edu.au 2 • Before the beginning of this lecture, students should be familiar with: – typing (and type casting) – the new operator – the concept of reference objects – the Java Scanner class – iteration using Java’s different loop syntaxes • Before the beginning of this lecture, students should have read over: – Chapters 7.1 to 7.3 of Lewis et al. Assumed Knowledge www.monash.edu.au 3 Objectives • By the end of this lecture, students should: – understand the basic concepts of arrays – be able to create, initialize and access arrays of primitive data types and of objects – understand iteration over arrays – understand the linear search algorithm through arrays – understand typical uses 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
FIT1002 Computer Programming Module 10 Arrays www.monash.edu.au Grouping Data Together 4 4 Often we want to group together many identically-typed values. E.g. a collection, a group, a queue . .. a set of bal s to be drawn from a barrel 10 -5.0 16 039 2 88 17 1 int number1 = 14; int number2 = 7; int number3 = 9; int number4 = -5; Why is this a bad idea? What would happen if we needed 2000 different numbers? What would the code look like to add 2 to each of these numbers? To achieve this grouping for a list of numbers in Java we could write: a set of numbers to be multiplied a list of tasks to be performed and crossed off www.monash.edu.au What do we really want? 5 Allows us to create a collection Allows us to name that collection as a single entity Allows us random access to the members of the collection Allows us to specify a particular member using an index (e.g. member 3) Rather than creating a variable for each value separately, we would like a technique that: www.monash.edu.au The Java Array 6 An array in Java is a named collection for storing values of the same data type. The total number of cel s in an array is defined when the array is declared. It can’t be changed within a single run of the program. 01234567 The cel s are numbered for easy naming and access. E.g. “myArray cell number 7” This number is cal ed an index or array subscript . In Java the first cel is numbered 0. In some other languages, the numbering starts from 1. myArray Array members are stored in contiguous “pigeon holes” in memory called cells or array elements.
Background image of page 2
FIT1002 Computer Programming Module 10 Arrays www.monash.edu.au Declaring Arrays 7 An array type can be constructed of cells to hold any single primitive type or class.
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.

This note was uploaded on 08/15/2010 for the course FIT 1002 taught by Professor Mylini during the Three '10 term at Monash.

Page1 / 18

Lec 10 Arrays - FIT1002 Computer Programming FIT1002...

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