COMPROG2 Programming Language using JAVA LESSON 1 – ARRAY (Part 1) Contents Basic Concepts Array Declaration Array Processing Parallel Array Basic Concepts: Array An array stores values similarly to the way in which a one-row egg carton holds eggs. Sometimes it is advantageous to store a collection of data together as a unit. Definition of an Array An array is a collection of a fixed number of objects all of the same type, which are stored sequentially in computer memory. The objects in an array are the array's elements. We access array elements using their index or subscript. Definition of an Array Definition: An array can hold multiple values of the same data type simultaneously. Steps in declaring array 1. Declare an array reference variable 2. Next step in the process is to use the new key word to create an array and assign its address to the numbers variable. Declaring an array Option 1: Option 2: Array declaration The number inside the brackets is the array’s size declarator It indicates the number of elements, or values, the array can hold. It is integer type (whole number) ONLY! numbers = new double[1.5] It should be not negative numbers = new int[-5] Array declaration numbers will reference an array that can hold six elements, each one an int data type. Array declaration Other valid array declaration: Array declaration Using a constant variable Note: This practice can make programs easier to maintain. When we need to change the size of the array. Once an array is created, its size cannot be changed. Array initialization To initialize an array, you use an initialization list of values separated by commas and enclosed within curly braces. Providing values for all the elements in an array also is called populating the array. Access to Array elements elements in the array may be accessed and used as individual variables. A subscript is used as an index to pinpoint a specific element within an array. 1st element of array has (0) zero subscript (always). Access to Array elements Subscript numbering always starts at zero. The last element in array is one less than the total number of elements in array. Array [n-1] Access to Array elements Subscript numbering always starts at zero. 20 30 Access to Array elements Subscript numbering always starts at zero. Assigning values to array: Displaying array elements System.out.println(numbers[0]); System.out.println(numbers[3]); System.out.println(numbers[1]); Screen output: 20 30 0 Displaying array elements Displaying array elements using: A) For Loop B) Enhanced For Loop Displaying array elements Using For Loop int[] age = {12, 4, 5}; for(int i = 0; i < 3; i++) System.out.println(age[i]); Processing of Array Array Length Each array in Java has a public field named length. This field contains the number of elements in the array. Enhanced FOR Loop Java provides a specialized version of the for loop that, in many circumstances, simplifies array processing. General Syntax: Enhanced FOR Loop Java provides a specialized version of the for loop that, in many circumstances, simplifies array processing. General Syntax: Enhanced FOR Loop int[] primes = {2, 3, 5, 7, 11}; for (int prime : primes) { System.out.println("current number is " + prime);} Enhanced FOR Loop When to use enhanced FOR Loop? When you need to access the values that are stored in an array, from the first element to the last element, the enhanced for loop is simpler to use than the traditional for loop. Enhanced FOR Loop Limitations of enhanced FOR Loop if you need to change the contents of an array element if you need to work through the array elements in reverse order if you need to access some of the array elements, but not all of them if you need to simultaneously work with two or more arrays within the loop if you need to refer to the subscript number of a particular element Displaying array elements DEMONSTRATION in JGRASP Input in array Scanner keyboard = new Scanner(System.in); int numbers[]=new int[6]; System.out.println(“Enter value in numbers[1]”); numbers[1] = keyboard.nextInt(); Processing of Array using FOR Processing of Array using FOR Processing of Array using FOR CHALLENGE! Create a program that will ask the number or size of an array. (To be inputted by the user) Processing of Array using FOR CONCEPT Individual array elements are processed like any other type of variable. Parallel Array In computing, a parallel array is a data structure for representing arrays of records. Values located at the same index in each array are implicitly the fields of the same record. Parallel Array Parallel Array Parallel Array NOTE: The true beauty of parallel arrays, is that each array may be of a different data type. Parallel Array DEMONSTRATION in JGRASP Summary 1. We process array elements thru their subscript or index. 2. An efficient way to process them is using a looping control structure (a counter controlled looping structure) because we know the size or number of elements. 3. Inputting, displaying and other processing done in the array and its elements needs individual looping statements.