Lecture 5 - Arrays - ARRAYS How to create and handle a very...

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

View Full Document Right Arrow Icon
Lecture 5 Arrays ARRAYS How to create and handle a very large number of data items? How to enter 7 temperature readings? • Try using a for loop? What is an array? • An array is a data type that stores a collection of items . • These items are sometimes referred to as the elements of the array. • All elements must be of the same type BUT there is no restriction on which type this is. For example • arrays can be used to hold a collection of int values; • or a collection of char values; • BUT they cannot be used to hold a mixture of int and char values. Arrays are used to hold a collection of items all of the same type . Array creation is a two stage process 1. Declare an array. ArrayType [ ] arrayName ; 2. Create memory to store the array. arrayName = new ArrayType [ sizeOfArray ]; Creating an array variable Declaring an array variable • Example: an array to hold a collection of integer variables: int[] someArray; • So, to declare an array temperature containing double values:
Background image of page 1

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

View Full DocumentRight Arrow Icon
Allocating memory to store the array elements • Need to state – the size of the array ; – the type of each individual array element . • The array type and size are then put together with a special new operator. Example: an array of 10 integers: arrayName = new int[10]; Returning to the temperature array • The temperature array holds seven double values: • The two stages of array creation can be combined into one step as follows:
Background image of page 2
Image of page 3
This is the end of the preview. Sign up to access the rest of the document.

Page1 / 7

Lecture 5 - Arrays - ARRAYS How to create and handle a very...

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

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