Download presentation
Presentation is loading. Please wait.
1
Grouping Objects Arrays and for loops
2
Objects First with Java - A Practical Introduction using BlueJ, © David J. Barnes, Michael Kölling Fixed-Size Collections Sometimes the maximum collection size is known. Sometimes the maximum collection size is known. Programming languages usually offer a special fixed-size collection type: the array. Programming languages usually offer a special fixed-size collection type: the array. Java arrays can store objects or primitive values. Java arrays can store objects or primitive values. Arrays use a special syntax. Arrays use a special syntax.
3
Objects First with Java - A Practical Introduction using BlueJ, © David J. Barnes, Michael Kölling The weblog-analyzer Project A web server records details of each access. It supports webmaster tasks: Most popular pages Busiest periods How much data is being delivered Broken references It analyses accesses by hour.
4
Objects First with Java - A Practical Introduction using BlueJ, © David J. Barnes, Michael Kölling Creating an Array Object public class LogAnalyzer { private int[] hourCounts; private int[] hourCounts; private LogfileReader reader; private LogfileReader reader; public LogAnalyzer() public LogAnalyzer() { hourCounts = new int[24]; hourCounts = new int[24]; reader = new LogfileReader(); reader = new LogfileReader(); }......} Array variable declaration Array object construction
5
Objects First with Java - A Practical Introduction using BlueJ, © David J. Barnes, Michael Kölling The hourCounts Array
6
Objects First with Java - A Practical Introduction using BlueJ, © David J. Barnes, Michael Kölling Using an Array Square-brackets notation is used to access an array element - e.g. hourCounts[hour] hourCounts[hour] where hour must be an integer variable holding a valid index to the hourCounts array (0..23). Array elements are used just like variables. They can be assigned: hourCounts[hour] = 42; hourCounts[hour] = 42; Or used in expressions: adjusted = 2*(hourCounts[hour] - 3); System.out.println (hourCounts[hour]); adjusted = 2*(hourCounts[hour] - 3); System.out.println (hourCounts[hour]);
7
Objects First with Java - A Practical Introduction using BlueJ, © David J. Barnes, Michael Kölling private int[] hourCounts; private Person[] students;... hourCounts = new int[24]; students = new Person[100];... hourcounts[i] = 0; students[j].enroll ("Co320-S"); System.out.println (hourcounts[i]); declaration construction use Using an Array
8
Objects First with Java - A Practical Introduction using BlueJ, © David J. Barnes, Michael Kölling Special Syntax for Array Literals private int[] numbers = {3, 15, 4, 5};... System.out.println (numbers[i]); declaration and initialisation Array literals can only be used in initialisations
9
Objects First with Java - A Practical Introduction using BlueJ, © David J. Barnes, Michael Kölling Array literals can only be used in initialisations Note: ‘ length ’ is a field of the array object Special Syntax for Array Literals private int[] numbers = {3, 15, 4, 5};... int n = numbers.length; Unlike ‘ length() ’, which is a method of String no brackets! declaration and initialisation
10
Objects First with Java - A Practical Introduction using BlueJ, © David J. Barnes, Michael Kölling Unlike ‘ length() ’, which is a method of String Array literals can only be used in initialisations Note: ‘ length ’ is a field of the array object Special Syntax for Array Literals private int[] numbers = {3, 15, 4, 5};... int n = numbers.length; Unlike ‘ size() ’, which is a method of ArrayList no brackets! declaration and initialisation
11
Objects First with Java - A Practical Introduction using BlueJ, © David J. Barnes, Michael Kölling The for Loop There are two variations of the for loop, for-each (already seen) and for. There are two variations of the for loop, for-each (already seen) and for. The for loop is often used to iterate a fixed number of times. The for loop is often used to iterate a fixed number of times. It is usually used with a control variable that changes a fixed amount on each iteration. It is usually used with a control variable that changes a fixed amount on each iteration.
12
Objects First with Java - A Practical Introduction using BlueJ, © David J. Barnes, Michael Kölling Structure of the for Loop for (initialisation; condition; post-body action) {... loop body... loop body} Equivalent while-loop form: initialisation; while (condition) {... loop body... post-body action } loop header Statement(s) to be repeated
13
Objects First with Java - A Practical Introduction using BlueJ, © David J. Barnes, Michael Kölling An Example for (int hour = 0; hour < hourCounts.length; hour++) { System.out.println (hour + ": " + hourCounts[hour]); System.out.println (hour + ": " + hourCounts[hour]);} int hour = 0; while (hour < hourCounts.length) { System.out.println(hour + ": " + hourCounts[hour]); System.out.println(hour + ": " + hourCounts[hour]); hour++; hour++;} for loop version while loop version Note: variable hour no longer exists here … Note: variable hour still exists here …
14
Objects First with Java - A Practical Introduction using BlueJ, © David J. Barnes, Michael Kölling int[] numbers = {4, 1, 22, 9, 14, 3, 9}; for (... ) {... } Practice Given an array of numbers, print out all the numbers in the array – one number per line. Use a for loop: Write this now!
15
Objects First with Java - A Practical Introduction using BlueJ, © David J. Barnes, Michael Kölling int[] numbers = {4, 1, 22, 9, 14, 3, 9}; for (... ) {... } Practice Given an array of numbers, print out all the numbers in the array – one number per line. Use a for loop: int[] numbers = {4, 1, 22, 9, 14, 3, 9}; for ( int i = 0; i < numbers.length; i++) { System.out.println (numbers[i]); }
16
Objects First with Java - A Practical Introduction using BlueJ, © David J. Barnes, Michael Kölling Given an array of numbers, print out all the numbers in the array – one number per line. Use a for loop: Actually, arrays also count as collections – so we can use a for-each loop on them: int[] numbers = {4, 1, 22, 9, 14, 3, 9}; for ( int i = 0; i < numbers.length; i++) { System.out.println (numbers[i]); } Practice for ( int n : numbers ) { System.out.println ( n ); } simpler!!!simpler!!!
17
Objects First with Java - A Practical Introduction using BlueJ, © David J. Barnes, Michael Kölling for ( int n : numbers ) { System.out.println ( n ); } Warning !!! Collection (or array) elements accessed through a for-each loop are copies of the actual elements: Works Works But don’t try assigning to them: for ( int n : numbers ) { n = 2*n; } Changes copies of elements in numbers – does not change any of the elements in numbers itself!
18
Objects First with Java - A Practical Introduction using BlueJ, © David J. Barnes, Michael Kölling Warning !!! But don’t try assigning to them: for ( int n : numbers ) { n = 2*n; } Changes copies of elements in numbers – does not change any of the elements in numbers itself!
19
Objects First with Java - A Practical Introduction using BlueJ, © David J. Barnes, Michael Kölling Warning !!! But don’t try assigning to them: for ( int n : numbers ) { n = 2*n; } Changes copies of elements in numbers – does not change any of the elements in numbers itself! To do this, we must use a for loop: for ( int i = 0; i < numbers.length; i++) { numbers[i] = 2*numbers[i]; } Changes the elements in numbers itself! Changes the elements in numbers itself!
20
Objects First with Java - A Practical Introduction using BlueJ, © David J. Barnes, Michael Kölling int[] fib = new int[100]; fib[0] = 0; fib[1] = 1; for (... ) {... } Fibonacci: 0, 1, 1, 2, 3, 5, 8, 13, 21, 34,... (each number is the sum of the previous two numbers) (start with zero and one) Fibonacci: 0, 1, 1, 2, 3, 5, 8, 13, 21, 34,... (each number is the sum of the previous two numbers) (start with zero and one) Practice Fill an array with the first 100 numbers of the Fibonacci sequence. Write this now!
21
Objects First with Java - A Practical Introduction using BlueJ, © David J. Barnes, Michael Kölling int[] fib = new int[100]; fib[0] = 0; fib[1] = 1; for (... ) {... } Fibonacci: 0, 1, 1, 2, 3, 5, 8, 13, 21, 34,... (each number is the sum of the previous two numbers) (start with zero and one) Fibonacci: 0, 1, 1, 2, 3, 5, 8, 13, 21, 34,... (each number is the sum of the previous two numbers) (start with zero and one) Practice Fill an array with the first 100 numbers of the Fibonacci sequence. int[] fib = new int[100]; fib[0] = 0; fib[1] = 1; for ( int i = 2; i < fib.length; i++ ) { fib[i] = fib[i-2] + fib[i-1]; } Cannot be written with a for-each loop.
22
Objects First with Java - A Practical Introduction using BlueJ, © David J. Barnes, Michael Kölling for loops can have any size of increment // Print multiples of 3 that are below 40. for (int num = 3; num < 40; num = num + 3) { System.out.println (num); System.out.println (num);}
23
Objects First with Java - A Practical Introduction using BlueJ, © David J. Barnes, Michael Kölling while loop without a collection int index = 0; while (index <= 42) { System.out.println (index); System.out.println (index); index = index + 2; index = index + 2;} int index = 0; while (index >= 42) { System.out.println (index); System.out.println (index); index = index + 2; index = index + 2;} int index = 0; while (index <= 42) { System.out.println (index); System.out.println (index); index = index - 2; index = index - 2;} // Print all even numbers from 0 to 42 inclusive. // Programming error? This does nothing at all! // Programming error? This loop never ends! Seen before
24
Objects First with Java - A Practical Introduction using BlueJ, © David J. Barnes, Michael Kölling The same – but with for loops for (int index = 0; index <= 42; index = index + 2) { System.out.println (index); } System.out.println (index); } for (int index = 0; index >= 42; index = index + 2) { System.out.println (index); } for (int index = 0; index <= 42; index = index - 2) { System.out.println (index); System.out.println (index);} // Print all even numbers from 0 to 42 inclusive. // Programming error? This does nothing at all! // Programming error? This loop never ends!
25
Objects First with Java - A Practical Introduction using BlueJ, © David J. Barnes, Michael Kölling for loops can exit early /** * Search an array for a zero. * * @return The index of the first zero element * (if none found, return -1) * Search an array for a zero. * * @return The index of the first zero element * (if none found, return -1) */ */ public int findZero (int[] A) { for ( int i = 0; i < numbers.length; i++) { if (numbers[i] == 0) { return i; } } return -1; return i; } } return -1;}
26
Objects First with Java - A Practical Introduction using BlueJ, © David J. Barnes, Michael Kölling Review Arrays are appropriate where a fixed-size collection is required. Arrays are appropriate where a fixed-size collection is required. Arrays use special [square brackets] syntax. Arrays use special [square brackets] syntax. For loops are a better alternative to while loops when the number of repetitions is known. For loops are a better alternative to while loops when the number of repetitions is known. For loops are often used to introduce and control an index variable when iterating through arrays. For loops are often used to introduce and control an index variable when iterating through arrays. For loops can exit early (with a return ). For loops can exit early (with a return ).
Similar presentations
© 2025 SlidePlayer.com. Inc.
All rights reserved.