Array contiguous memory locations that have the same

  • Slides: 17
Download presentation
Array • contiguous memory locations that have the same name and type. Note: an

Array • contiguous memory locations that have the same name and type. Note: an array may contain primitive data BUT an array is a data structure a collection of memory locations.

scores 88 95 33 67 85 • array name: scores (identifier chosen by programmer)

scores 88 95 33 67 85 • array name: scores (identifier chosen by programmer) • has 5 elements • the memory allocated is contiguous

Array Declaration data. Type array. Name[]; or data. Type []array. Name; • the brackets

Array Declaration data. Type array. Name[]; or data. Type []array. Name; • the brackets can appear on either side of array. Name with any number of spaces surrounding the brackets

Array Declaration data. Type array. Name[]; • does not allocate memory for the array

Array Declaration data. Type array. Name[]; • does not allocate memory for the array To Allocate Memory for An Array int score[]; score = new int[5]; 20 bytes (integer is 4 bytes) of memory is allocated

Declaration And Allocation • int scores[] = new int[5]; equivalent int scores[]; scores =

Declaration And Allocation • int scores[] = new int[5]; equivalent int scores[]; scores = new int[5];

Notes • when arrays are allocated, the elements are initialized to – zero for

Notes • when arrays are allocated, the elements are initialized to – zero for the primitive data types – false for boolean – null for non primitive data types • every Java array knows its own length • scores. length is equal to 5 scores 88 95 33 67 85

Array Assignment int x[] = new int[5]; 0 0 0

Array Assignment int x[] = new int[5]; 0 0 0

Method 1: store data at time of declaration int x[] = {10, 20, 30,

Method 1: store data at time of declaration int x[] = {10, 20, 30, 40, 50}; 10 20 30 40 50 Method 2: store data one cell at a time public void store. Data(int x[]){ for (int i = 0; i<x. length; i++) x[i] = 10 * i + 10; }

Passing Arguments To Methods (Reference) some. Object. print. Array(some. Array); public void print. Array(int

Passing Arguments To Methods (Reference) some. Object. print. Array(some. Array); public void print. Array(int n[]){ for (int i = 0; i<n. length; i++) System. out. print(n[i]); }

Notes: • non primitive data types pass arguments by reference • arrays are passed

Notes: • non primitive data types pass arguments by reference • arrays are passed by reference • arrays are treated as objects • a copy is not made; the called method has direct access to the argument and can change it • basically a reference (address) to the argument is passed to the method

Array Assignment • one array can be assigned to another int x[] = new

Array Assignment • one array can be assigned to another int x[] = new int[5]; int a [] = new int[7];

Array Assignment int a [] = new int[7]; int b[] = {10, 20, 30};

Array Assignment int a [] = new int[7]; int b[] = {10, 20, 30}; int x[] = new int[5]; x = b; • x and b are identical after (x=b) is executed • if the element values in b are changed, the elements of x are changed also • also note that when both were declared and allocated they are of different sizes • however, they are the same after (x=b) is executed

Array Assignment int x[] = new int[5]; After int b[] = {10, 20, 30};

Array Assignment int x[] = new int[5]; After int b[] = {10, 20, 30}; 0 0 0 10 20 30 0 0 x=b 0 10 0 20 30 0

Array Of Objects String day. Names [] = { “Sunday”, “Monday”, “Tuesday”, “Wednesday”, “Thursday”,

Array Of Objects String day. Names [] = { “Sunday”, “Monday”, “Tuesday”, “Wednesday”, “Thursday”, “Friday”, “Saturday” }

Array Of Objects day. Names[0] 0 “Sunday” 1 2 . . . 3 day.

Array Of Objects day. Names[0] 0 “Sunday” 1 2 . . . 3 day. Names[6] 4 “Monday” “Tuesday” “Wednesday” “Thursday” 5 “Friday” 6 “Saturday”