Download presentation
Presentation is loading. Please wait.
1
Programming with Collections Collections in Java Using Arrays Week 9
2
Arrays Using the For Loop with Arrays Programming with Collections CONCEPTS COVERED THIS WEEK
3
Programming with Collections FIXED-SIZE COLLECTIONS Sometimes the maximum collection size can be pre-determined. Programming languages usually offer a special fixed-size collection type: an array. Java arrays can store either objects or primitive-type values (e.g. int, float, etc…) Arrays use a special syntax.
4
What is an array? A special type of data structure which consists of a group of items of the same type, e.g. int, double, String etc. We can manipulate the whole group as a single entity and we can use any single member of that group (called an array element) independently. Each element is located by an INDEX or SUBSCRIPT pointing to the appropriate ‘cell’ number.
5
Using Arrays Declaring & initialising an array called numbers that has 10 integer elements: int[ ] numbers; numbers = new int [10]; This tells the system to create 10 linked integer memory locations that can be accessed by the name numbers[..]
6
The numbers array numbers 0 1 2 3 4 5 6 7 8 9 The statement: numbers[5] = 8; puts 8 in element 5 8 0000000000 000000000
7
Note The subscripts (elements) are numbered from 0 to 9, so subscript number 5 is the sixth one along. Array subscripts ALWAYS start at 0. Arrays have square brackets [ ]. Uninitialised numeric array elements take the value 0 by default.
8
Creating an array Choose any legal variable name Key in [ ] square brackets before (left) or after (right) of the name. int ages[ ]; String[ ] names; ages = new int [6]; names = new String [5]; Numbers in [ ] are indices - they tell how many items BUT can also be which element is referred to
9
Using Arrays Square-bracket notation is used to access an array element: c[5] Elements are used like ordinary variables. –On the left of an assignment: c[5] = 8; –In an expression: int adjusted = c[5] – 3; c[5]++;
10
Another example int c[]; c = new int[12]; //to ‘load’ each element we could use 12 separate statements c[0] = -45; c[1] = 6; c[2] = 0; c[3] = 72; c[4] = 1543; c[5] = -89; c[6] = 0; c[7] = 62; c[8] = -3; c[9] = 1; c[10] = 6453; c[11] = 78; Alternatively, we could initialise (‘populate’) the array when we declare it: int c[] = {-45, 6, 0, 72, 1543, -89, 0, 62, -3, 1, 6453, 78};
11
Arrays and for loops Suppose we wanted to display all the elements of array c[], we could write 12 System.out.print statements OR use a ‘for’ loop: for (int i = 0; i < c.length; i++) System.out.print ( c[i] + “ ” ); c.length returns the number of elements in the array, 12 in this case
12
Arrays and for loops To add all the elements of array c[] and put the answer in sum: int sum = 0; for (int i = 0; i < c.length; i++) sum += c[i]; System.out.println (“The sum is ” + sum);
13
Arrays & for loops //To find the largest element in c int largestValue = c[0]; int indexOfLargest = 0; for (int i = 0; i < c.length; i++) { if (c[i] > largestValue) { largestValue = c[i]; indexOfLargest = i; }
14
Good Programming Tip It’s better to declare the size of the array independently of the array declaration, e.g. int ARRAY_SIZE = 12; int c[]; c = new int [ARRAY_SIZE]; Then, to change the size, you only need to change one line.
15
Arrays Summary Arrays are appropriate where a fixed- size collection of data items is required. Arrays use special syntax. The subscript always starts at 0. For loops are often used to iterate over arrays.
16
Common Array Tasks Create Display contents Search Sum (add all numeric elements) Sort
17
Programming with Collections REVIEW OF ARRAYS Arrays are appropriate where a fixed-size collection is required. Arrays use special syntax. For loops offer an alternative to while loops when the number of repetitions is known. For loops are often used to iterate over arrays.
18
Programming with Collections ARRAYS vs ARRAYLISTS An array has to be given its size at the time it is created. An array list does not have a pre- determined size. To place an item in an array you need to place it in a specific element location. With an array list you just use the add method. An array can store either objects or primitive data types but an array list can only store objects.
Similar presentations
© 2025 SlidePlayer.com. Inc.
All rights reserved.