single-dimension arrays. data structures it is a collection of scalar data types that are all...

8
SINGLE-DIMENSION ARRAYS

Upload: victoria-rice

Post on 02-Jan-2016

222 views

Category:

Documents


3 download

TRANSCRIPT

Page 1: SINGLE-DIMENSION ARRAYS. Data Structures It is a collection of scalar data types that are all referenced or accessed through one identifier – scalar type

SINGLE-DIMENSIONARRAYS

Page 2: SINGLE-DIMENSION ARRAYS. Data Structures It is a collection of scalar data types that are all referenced or accessed through one identifier – scalar type

Data Structures

• It is a collection of scalar data types that are all referenced or accessed through one identifier– scalar type – it is a simple data type that holds one

value at a time. Ex. int, double, char, and boolean

•An array is a data structure

Page 3: SINGLE-DIMENSION ARRAYS. Data Structures It is a collection of scalar data types that are all referenced or accessed through one identifier – scalar type

Arrays

• It is a linear data structure• it is composed of adjacent memory locations (or

cells)• They are objects in Java• You must declare the reference and instantiate it

using new• It will hold default values:– 0 for numeric– false for Boolean– null for objects

Page 4: SINGLE-DIMENSION ARRAYS. Data Structures It is a collection of scalar data types that are all referenced or accessed through one identifier – scalar type

Declaring an array

type[] arrayName = new type[length];

An array object is constructed of a defined size. Once it has been constructed the number of slots does not change

Generally, the size of an array is defined by using a final value:final int MAX = 200;int[] numb = new int [MAX];

Page 5: SINGLE-DIMENSION ARRAYS. Data Structures It is a collection of scalar data types that are all referenced or accessed through one identifier – scalar type

Initializing an Array

• Arrays are initialized in some type of loop.• To make a copy of an array, you must retrieve

each value of the initial array and make a copy of it’s value to store into the copy array.

• for(int x = 0; x <= MAX; x++){

arrayCopy[x} = arrayOriginal[x];}

Page 6: SINGLE-DIMENSION ARRAYS. Data Structures It is a collection of scalar data types that are all referenced or accessed through one identifier – scalar type

Common Mistakes about Arrays

• copyArray = originalArray

• = Is an assignment operator, all you did was assign the memory location of the originalArray is assigned into copyArray memory cell

originalArray

copyArray

Page 7: SINGLE-DIMENSION ARRAYS. Data Structures It is a collection of scalar data types that are all referenced or accessed through one identifier – scalar type

What Usually Happens to an Array

• Insertion – At the beginning– At the end– Somewhere in the middle

• Deleting• Traversing• Sorting• Searching

Page 8: SINGLE-DIMENSION ARRAYS. Data Structures It is a collection of scalar data types that are all referenced or accessed through one identifier – scalar type

Arrays as Parameters

• When a method is sending an array, the method’s formal parameter becomes an alias for the original calling parameter.

• Let’s see a program!