Fixed-sized collections Introduction to arrays 6.0.

Slides:



Advertisements
Similar presentations
Lists and the Collection Interface Chapter 4. Chapter Objectives To become familiar with the List interface To understand how to write an array-based.
Advertisements

Grouping objects Arrays and for loops. Fixed-size collections Sometimes the maximum collection size can be pre-determined. Programming languages usually.
1 Features of the collection It increases its capacity as necessary. It keeps a private count: –size() accessor. It keeps the objects in order. Details.
Grouping objects Introduction to collections 5.0.
Grouping Objects Arrays and for loops. Objects First with Java - A Practical Introduction using BlueJ, © David J. Barnes, Michael Kölling Fixed-Size Collections.
Computer Science A 10: 20/3. Array Array: Sequence of values of the same type Construct array: new double[10] Store in variable of type double[] double[]
Grouping Objects 3 Iterators, collections and the while loop.
Programming with Collections Collections in Java Using Arrays Week 9.
JAVA: An Introduction to Problem Solving & Programming, 5 th Ed. By Walter Savitch and Frank Carrano. ISBN © 2008 Pearson Education, Inc., Upper.
Chapter 7 Arrays. © 2004 Pearson Addison-Wesley. All rights reserved7-2 Arrays Arrays are objects that help us organize large amounts of information Chapter.
Loops – While, Do, For Repetition Statements Introduction to Arrays
Datalogi A 8: 27/10. Array Array: Sequence of values of the same type Construct array: new double[10] Store in variable of type double[] double[] data.
1 Arrays  Arrays are objects that help us organize large amounts of information  Chapter 8 focuses on: array declaration and use passing arrays and array.
Grouping objects Collections and iterators. 04/11/2004Lecture 4: Grouping Objects2 Main concepts to be covered Collections Loops Iterators Arrays.
Chapter 9 Introduction to Arrays
Programming with Collections Grouping & Looping - Collections and Iteration Week 7.
Grouping objects Collections and iterators. Main concepts to be covered Collections Loops Iterators.
Grouping Related Items
JAVA: An Introduction to Problem Solving & Programming, 7 th Ed. By Walter Savitch ISBN © 2015 Pearson Education, Inc., Upper Saddle River,
REPETITION CITS1001. Scope of this lecture Repetition for loops while loops 2.
5-Aug-2002cse Arrays © 2002 University of Washington1 Arrays CSE 142, Summer 2002 Computer Programming 1
Objects First With Java A Practical Introduction Using BlueJ Grouping objects Collections and iterators 2.0.
Grouping objects Arrays, Collections and Iterators 1.0.
JAVA: An Introduction to Problem Solving & Programming, 5 th Ed. By Walter Savitch and Frank Carrano. ISBN © 2009 Pearson Education, Inc., Upper.
Object-Oriented Program Development Using Java: A Class-Centered Approach, Enhanced Edition.
1 © 2002, Cisco Systems, Inc. All rights reserved. Arrays Chapter 7.
CHAPTER: 12. Array is a collection of variables of the same data type that are referenced by a common name. An Array of 10 Elements of type double.
Copyright © 2012 Pearson Education, Inc. Chapter 6 More Conditionals and Loops Java Software Solutions Foundations of Program Design Seventh Edition John.
Grouping objects Collections and iterators Objects First with Java - A Practical Introduction using BlueJ, © David J. Barnes, Michael Kölling Main.
Documentation Array and Searching. Documentation rules Easy rules: –Naming convention for variables, constants and methods Difficult rules: –Professional.
Chapter 4 Grouping Objects. Flexible Sized Collections  When writing a program, we often need to be able to group objects into collections  It is typical.
OOP (Java): Grouping/ OOP (Java) Objectives – –discuss call-by-value and call-by-reference, arrays, collections, and iterators Semester.
Chapter 5 – Part 3 Conditionals and Loops. © 2004 Pearson Addison-Wesley. All rights reserved5-2 Outline The if Statement and Conditions Other Conditional.
Grouping objects Iterators. Iterator type Third variation to iterate over a collection Uses a while loop and Iterator object But NO integer index variable.
JAVA: An Introduction to Problem Solving & Programming, 6 th Ed. By Walter Savitch ISBN © 2012 Pearson Education, Inc., Upper Saddle River,
GROUPING OBJECTS CITS1001. Lecture outline The ArrayList collection Process all items: the for-each loop 2.
Grouping objects Arrays. 2 Fixed-size collections The maximum collection size may be pre-determined with an upper limit Array is an fixed-size collection.
Programming Fundamentals 2: Grouping/ F II Objectives – –discuss call-by-value and call-by-reference, arrays, collections, and iterators.
JAVA: An Introduction to Problem Solving & Programming, 5 th Ed. By Walter Savitch and Frank Carrano. ISBN © 2008 Pearson Education, Inc., Upper.
1 COS 260 DAY 7 Tony Gauvin. 2 Agenda Questions? 3 rd Mini quiz next class (September 28) –Chap 3 concepts Assignment 1 Corrected Assignment 2 posted.
Chapter 8: Part 3 Collections and Two-dimensional arrays.
1 COS 260 DAY 9 Tony Gauvin. 2 Agenda Questions? 3 rd Mini quiz –Good results ->All A’s –Threw out question on name overloading 4 th Mini quiz next class.
Collections and Iteration Week 13.  Collections  ArrayList objects  Using loops with collections Collections and Iteration CONCEPTS COVERED THIS WEEK.
Arrays in java Unit-1 Introduction to Java. Array There are situations where we might wish to store a group of similar type of values in a variable. Array.
 Introducing Arrays  Declaring Array Variables, Creating Arrays, and Initializing Arrays  Copying Arrays  Multidimensional Arrays  Search and Sorting.
Liang, Introduction to Java Programming, Fifth Edition, (c) 2005 Pearson Education, Inc. All rights reserved Chapter 6 Arrays.
Grouping objects Iterators, collections and the while loop Equality and Equality == and equals(…)
Chapter 9 Introduction to Arrays Fundamentals of Java.
Functional Processing of Collections (Advanced) 6.0.
© 2004 Pearson Addison-Wesley. All rights reserved October 5, 2007 Arrays ComS 207: Programming I (in Java) Iowa State University, FALL 2007 Instructor:
Chapter 8: Understanding Collections Textbook: Chapter 4.
1 © 2017 Pearson Education, Inc. Hoboken, NJ. All rights reserved. for-each PROS easy to use access to ALL items one-by-one ability to change the state.
for-each PROS CONS easy to use access to ALL items one-by-one
Objects First with Java CITS1001 week 4
Objects First with Java CITS1001
Fixed-sized collections
Chapter 7 Part 1 Edited by JJ Shepherd
Loop Structures.
Functional Processing of Collections (Advanced)
COS 260 DAY 13 Tony Gauvin.
Java How to Program, Late Objects Version, 10/e
COS 260 DAY 10 Tony Gauvin.
COS 260 DAY 8 Tony Gauvin.
Arrays .
Objects First with Java Introduction to collections
CS2011 Introduction to Programming I Arrays (I)
COS 260 DAY 11 Tony Gauvin.
Collections and iterators
Arrays October 6, 2006 ComS 207: Programming I (in Java)
Collections and iterators
Presentation transcript:

Fixed-sized collections Introduction to arrays 6.0

2 Features of arrays Fixed in length, unlike ArrayList, HashSet, HashMap, etc. Use a special syntax. –For historical reasons. Objects with no methods. –Methods are provided by other classes; e.g., java.util.Arrays. –Methods that are static. © 2017 Pearson Education, Inc. Hoboken, NJ. All rights reserved.

3 Fixed-size collections Sometimes the maximum collection size can be pre-determined. A special fixed-size collection type is available: an array. Unlike the flexible List collections, arrays can store object references or primitive-type values (without autoboxing). © 2017 Pearson Education, Inc. Hoboken, NJ. All rights reserved.

4 The weblog-analyzer project Web server records details of each access. Supports analysis tasks: –Most popular pages. –Busiest periods. –How much data is being delivered. –Broken references. Analyze accesses by hour – there is a fixed number of these in a day! © 2017 Pearson Education, Inc. Hoboken, NJ. All rights reserved.

5 Creating an array object © 2017 Pearson Education, Inc. Hoboken, NJ. All rights reserved. public class LogAnalyzer { private int[] hourCounts; private LogfileReader reader; public LogAnalyzer() { hourCounts = new int[24]; reader = new LogfileReader(); }... } Array object creation — specifies size Array type — does not contain size

6 The hourCounts array © 2017 Pearson Education, Inc. Hoboken, NJ. All rights reserved.

7 Using an array Square-bracket notation is used to access an array element: hourCounts[...] Elements are used like ordinary variables. The target of an assignment: hourCounts[hour] =...; In an expression: hourCounts[hour]++; if(hourCounts[hour] > 0)... © 2017 Pearson Education, Inc. Hoboken, NJ. All rights reserved.

8 Standard array use © 2017 Pearson Education, Inc. Hoboken, NJ. All rights reserved. private int[] hourCounts; private String[] names;... hourCounts = new int[24];... hourcounts[i] = 0; hourcounts[i]++; System.out.println(hourcounts[i]); declaration creation use

9 Array literals Array literals in this form can only be used in declarations. Related uses require new : © 2017 Pearson Education, Inc. Hoboken, NJ. All rights reserved. private int[] numbers = { 3, 15, 4, 5 }; declaration, creation and initialization numbers = new int[] { 3, 15, 4, 5 }; The size is inferred from the data.

10 Array length NB: length is a field rather than a method. It’s value cannot be changed – ‘fixed size’. © 2017 Pearson Education, Inc. Hoboken, NJ. All rights reserved. private int[] numbers = { 3, 15, 4, 5 }; int n = numbers.length; not a method call!

11 The for loop There are two variations of the for loop, for-each and for. The for loop is often used to iterate a fixed number of times. Often used with a variable that changes a fixed amount on each iteration. © 2017 Pearson Education, Inc. Hoboken, NJ. All rights reserved.

12 For loop pseudo-code © 2017 Pearson Education, Inc. Hoboken, NJ. All rights reserved. for(initialization; condition; post-body action) { statements to be repeated } General form of the for loop Equivalent while-loop version initialization; while(condition) { statements to be repeated post-body action }

13 Array iteration © 2017 Pearson Education, Inc. Hoboken, NJ. All rights reserved. for(int hour = 0; hour < hourCounts.length; hour++) { System.out.println(hour + ": " + hourCounts[hour]); } int hour = 0; while(hour < hourCounts.length) { System.out.println(hour + ": " + hourCounts[hour]); hour++; } for loop version while loop version

14 Array-related methods System has static arraycopy. java.util.Arrays contains static utility methods for processing arrays: –binarySearch –fill –sort ArrayList has toArray. © 2017 Pearson Education, Inc. Hoboken, NJ. All rights reserved.

15 Practice Given an array of numbers, print out all the numbers in the array, using a for loop. © 2017 Pearson Education, Inc. Hoboken, NJ. All rights reserved. int[] numbers = { 4, 1, 22, 9, 14, 3, 9}; for...

16 Practice Fill an array with the Fibonacci sequence. © 2017 Pearson Education, Inc. Hoboken, NJ. All rights reserved. int[] fib = new int[howMany]; fib[0] = 0; fib[1] = 1; for(...)

17 for loop with bigger step © 2017 Pearson Education, Inc. Hoboken, NJ. All rights reserved. // Print multiples of 3 that are below 40. for(int num = 3; num < 40; num = num + 3) { System.out.println(num); }

18 for loop and Iterator © 2017 Pearson Education, Inc. Hoboken, NJ. All rights reserved. for(Iterator it = tracks.iterator(); it.hasNext(); ) { Track track = it.next(); if(track.getArtist().equals(artist)) { it.remove(); } No post-body action required.

19 Review Arrays are appropriate where a fixed- size collection is required. Arrays use a special syntax. For loops are used when an index variable is required. For loops offer an alternative to while loops when the number of repetitions is known. Used with a regular step size. © 2017 Pearson Education, Inc. Hoboken, NJ. All rights reserved.

20 The automaton project An array of ‘cells’. Each cell maintains a simple state. –Usually a small numerical value. –E.g., on/off or alive/dead. The states change according to simple rules. Changes affected by neighboring states. © 2017 Pearson Education, Inc. Hoboken, NJ. All rights reserved.

21 A simple automaton © 2017 Pearson Education, Inc. Hoboken, NJ. All rights reserved. nextState[i] = (state[i-1] + state[i] + state[i+1]) % 2; StepCell states – blank cells are in state

22 The conditional operator Choose between two values: © 2017 Pearson Education, Inc. Hoboken, NJ. All rights reserved. for(int cellValue : state) { System.out.print(cellValue == 1 ? '+' : ' '); } System.out.println(); condition ? value1 : value1

Further advanced material

24 Arrays of more than one dimension Array syntax supports multiple dimensions. –E.g., 2D array to represent a game board, or a grid of cells. Can be thought of as an array of arrays. © 2017 Pearson Education, Inc. Hoboken, NJ. All rights reserved.

25 The brain project © 2017 Pearson Education, Inc. Hoboken, NJ. All rights reserved. Cell[][] cells;... cells = new Cell[numRows][numCols];... for(int row = 0; row < numRows; row++) { for(int col = 0; col < numCols; col++) { cells[row][col] = new Cell(); }

26 Alternative iteration ‘Array of array’ style. Requires no access to numRows and numCols. Works with irregular shape arrays, which are supported in Java. © 2017 Pearson Education, Inc. Hoboken, NJ. All rights reserved. for(int row = 0; row < cells.length; row++) { Cell[] nextRow = cells[row]; for(int col = 0; col < nextRow.length; col++) { nextRow[col] = new Cell(); }

27 Arrays and Streams java.util.Arrays has several stream() methods that return a Stream based on an array. IntStream is a Stream of int values. Instream.range() creates a sequential IntStream. toArray returns an array from a Stream. © 2017 Pearson Education, Inc. Hoboken, NJ. All rights reserved.