Presentation is loading. Please wait.

Presentation is loading. Please wait.

Arrays. What is an array An array is used to store a collection of data It is a collection of variables of the same type.

Similar presentations


Presentation on theme: "Arrays. What is an array An array is used to store a collection of data It is a collection of variables of the same type."— Presentation transcript:

1 Arrays

2 What is an array An array is used to store a collection of data It is a collection of variables of the same type

3 Declaring Array Variables dataType [] name; // preferred way double[] myList; dataType name []; // works but not preferred double myList [];

4 4 Arrays An array is an ordered list of values 0 1 2 3 4 5 6 7 8 9 79 87 94 82 67 98 87 81 74 91 An array of size N is indexed from zero to N-1 scores The entire array has a single name Each value has a numeric index This array holds 10 values that are indexed from 0 to 9

5 5 Arrays A particular value in an array is referenced using the array name followed by the index in brackets For example, the expression scores[2] refers to the value 94 (the 3rd value in the array) That expression represents a place to store a single integer and can be used wherever an integer variable can be used

6 6 Arrays The values held in an array are called array elements An array stores multiple values of the same type (the element type) The element type can be a primitive type or an object reference Therefore, we can create an array of integers, or an array of characters, or an array of String objects, etc. In Java, the array itself is an object Therefore the name of the array is a object reference variable, and the array itself must be instantiated

7 Creating Arrays You can create an array by using the new operator with the following syntax dataType [] name = new dataType [arraySize]; The above statement does two things: It creates an array using new dataType[arraySize]; It assigns the reference of the newly created array to the variable name.

8 Declaring an array variable, creating an array, and assigning the reference of the array to the variable can be combined in one statement, as shown below: dataType [] name = new dataType [arraySize]; dataType [] name = {value0, value1,..., valuek}; int [] age = {10, 15, 20, 5}; int [] age = new int [4];

9 9 Declaring Arrays Some examples of array declarations: double[] prices = new double[500]; boolean[] flags; flags = new boolean[20]; char[] codes = new char[1750];

10 10 Bounds Checking Once an array is created, it has a fixed size An index used in an array reference must specify a valid element That is, the index value must be in bounds (0 to N-1) The Java interpreter throws an ArrayIndexOutOfBoundsException if an array index is out of bounds This is called automatic bounds checking

11 11 Bounds Checking For example, if the array codes can hold 100 values, it can be indexed using only the numbers 0 to 99 If count has the value 100, then the following reference will cause an exception to be thrown: System.out.println (codes[count]); It’s common to introduce off-by-one errors when using arrays for (int index=0; index <= 100; index++) codes[index] = index*50 + epsilon; problem

12 Example double[] myList = new double[4]; double[] myList = {1.9, 2.9, 3.4, 3.5}; 1.9 2.9 3.4 3.5 myList[0] myList[1] myList[2] myList[3] myList.length will be 4 0.0 myList[0] myList[1] myList[2] myList[3]

13 Processing Arrays: When processing array elements, we often use either for loop or foreach loop because all of the elements in an array are of the same type and the size of the array is known.

14 Create an array public class TestArray { public static void main(String[] args) { double[] myList = {1.9, 2.9, 3.4, 3.5};

15 Print an array // Print all the array elements for (int i = 0; i < myList.length; i++) { System.out.println(myList[i] + " "); }

16 Sum of all elements in an array // Summing all elements double total = 0; for (int i = 0; i < myList.length; i++) { total += myList[i]; } System.out.println("Total is " + total);

17 Largest element in an array // Finding the largest element double max = myList[0]; for (int i = 1; i < myList.length; i++) { if (myList[i] > max) max = myList[i]; } System.out.println("Max is " + max); }

18 Write a program to reverse the elements in an array public static void reverse(int[] arrayOne) { for(int i = 0; i < arrayOne.length; i++) { int temp = arrayOne[i]; arrayOne[i] = arrayOne[arrayOne.length - i - 1]; arrayOne[arrayOne.length - 1] = temp; System.out.println("myArray[" + i + "] = " + arrayOne[i]); }


Download ppt "Arrays. What is an array An array is used to store a collection of data It is a collection of variables of the same type."

Similar presentations


Ads by Google