Presentation is loading. Please wait.

Presentation is loading. Please wait.

Arrays.

Similar presentations


Presentation on theme: "Arrays."— Presentation transcript:

1 Arrays

2 Array Basics Declare variables to store and add 3 blood pressures:
int bp1, bp2, bp3; bp1 = myScanner.nextInt(); bp2 = myScanner.nextInt(); bp3 = myScanner.nextInt(); int total = bp1 + bp2 + bp3; What if you wanted to store and total 1000 blood pressures? Declare an array of 1000 int values.

3 Array Basics An array is a collection of individual variables that are usually related. Each individual variable called an element. Arrays can be composed of any data type, but all of the elements are the same type. An array is given a single name, and stored in consecutive memory locations.

4 Array Basics Each element of an array is accessed by the array name followed by an index in brackets []. The index is also known as subscript and indicates the position of the element. In Java, the first array element always has subscript 0, the second element has subscript 1, and so on ...

5 Array Declaration Example
int myArray[] = new int[5]; Or int[] myArray = new int[5]; This declaration creates an array of five elements, all of which are integers. Consecutive memory locations are allocated for the five elements of the array. The base address of an array is its beginning address in memory.

6 Array Declaration (Cont ..)
Index/Subscript int scores[] = new int[5]; score[1] score[3] score[4] score[0] score[2] Number of elements in the array

7 Array Initialization Elements can be initialized at declaration:
int scores[] = {78,84,62,93,97}; 84 93 97 78 62 score[1] score[3] score[4] score[0] score[2]

8 0-based indexing Arrays are 0-based, meaning they begin at 0 rather than 1. Example: int scores[] = {99, 85, 43, 72, 82 }; Position 1 2 3 4 5 Index Value 99 85 43 72 82

9 Assigning values to individual array elements
int scores[] = new int[5]; int i = 4; score[2] = 98; score[3] = 83; score[i] = 79; score[0] = score[3] + 5; 83 79 88 98 score[1] score[3] score[4] score[0] score[2]

10 Example public static void main(String[] args) {
final int NUM_OF_STUDENTS = 5; int score[NUM_OF_STUDENTS] = {78,84,62,93,97}; for (int count = 0; count < NUM_OF_STUDENTS; count++) System.out.println("Score# “ + score[count]); } Score#0 78 Score#1 84 Score#2 62 Score#3 93 Score#4 97

11 Tips Array index has to be an integer type.
It is the programmer’s responsibility to make sure that an array index does not go out of bounds. The index must be within the range 0 through the declared array size minus one. Using an index value outside this range causes the program to access memory locations outside the array => Program crashes! The index value determines which memory location is used

12 Common Uses of Arrays Sometimes when we want to encode a series of values, we need a series of if statements. With arrays we can “index” the values so it moves quicker This technique is widely used for its speed/efficiency and is known as hashing Ex. public string translate( int dayOfWeek ) { if( dayOfWeek < 0 || dayOfWeek > 6)//error return “error: day out of range”;//error else String[] daysOfWeek = {“Monday”, “Tuesday”, “Wednesday”, “Thursday”, “Friday”, “Saturday”, “Sunday”}; return daysOfWeek[dayOfWeek]; }

13 Why Hashing Alternative is slow:
public String translate(int dayOfWeek) { if( dayOfWeek == 0 ) return “Monday”; else if(dayOfWeek == 1) return “Tuesday”; else if(dayOfWeek == 2) return “Wednesday”; else if(dayOfWeek == 3) return “Thursday”; else if(dayOfWeek == 4) return “Friday”; else if(dayOfWeek == 5) return “Saturday”; else if(dayOfWeek == 6) return “Sunday”; else return “Error”; }

14 Common Uses of Arrays Sometimes when we want to encode a series of values, we need a series of if statements. With arrays we can “index” the values so it moves quicker Ex. public string translate( int dayOfWeek ) { if( dayOfWeek < 0 || dayOfWeek > 6)//error return “error: day out of range”;//error else String daysOfWeek[7] = {“Monday”, “Tuesday”, “Wednesday”, “Thursday”, “Friday”, “Saturday”, “Sunday”}; return daysOfWeek[dayOfWeek]; }

15 Arrays in functions Arrays are ALWAYS passed by reference
If you change the array, you will change the values associated with it after the functions returns. Ex. void fill( int array[], int size ) { for( int n = 0; n < size; n++ ) array[ n ] = 0;//set all values to 0 }


Download ppt "Arrays."

Similar presentations


Ads by Google