Download presentation
Presentation is loading. Please wait.
Published bySheila Preston Modified over 9 years ago
1
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.
2
scores 8895336785 array name: scores (identifier chosen by programmer) has 5 elements the memory allocated is contiguous
3
Array Declaration dataType arrayName[]; dataType []arrayName; or the brackets can appear on either side of arrayName with any number of spaces surrounding the brackets
4
does not allocate memory for the array Array Declaration dataType arrayName[]; To Allocate Memory for An Array int score[]; score = new int[5]; 20 bytes (integer is 4 bytes) of memory is allocated
5
Declaration And Allocation int scores[] = new int[5]; int scores[]; scores = new int[5]; equivalent
6
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 8895336785 scores
7
int x[] = new int[5]; Array Assignment 00000
8
Method 1: store data at time of declaration int x[] = {10, 20, 30,40,50}; 1020305040 Method 2: store data one cell at a time public void storeData(int x[]){ for (int i = 0; i<x.length; i++) x[i] = 10 * i + 10;}
9
someObject.printArray(someArray); public void printArray(int n[]){ for (int i = 0; i<n.length; i++) System.out.print(n[i]); } Passing Arguments To Methods (Reference)
10
non primitive data types pass arguments by reference arrays are passed by reference arrays are treated as objects Notes: 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
11
Array Assignment one array can be assigned to another int x[] = new int[5]; int a [] = new int[7];
12
Array Assignment int a [] = new int[7]; int b[] = {10, 20, 30}; int x[] = new int[5]; 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 x = b;
13
Array Assignment int x[] = new int[5]; int b[] = {10, 20, 30}; 102030 00000 00000 102030 After x = b
14
Array Of Objects String dayNames [] = { “Sunday”, “Monday”, “Tuesday”, “Wednesday”, “Thursday”, “Friday”, “Saturday” }
15
Array Of Objects “Sunday”“Monday”“Tuesday”“Wednesday”“Thursday”“Friday”“Saturday” dayNames[0]dayNames[6]... 0123456
Similar presentations
© 2025 SlidePlayer.com. Inc.
All rights reserved.