Objects First with Java CITS1001

Slides:



Advertisements
Similar presentations
Arrays Chapter 6. Outline Array Basics Arrays in Classes and Methods Sorting Arrays Multidimensional Arrays.
Advertisements

Grouping Objects Arrays and for loops. Objects First with Java - A Practical Introduction using BlueJ, © David J. Barnes, Michael Kölling Fixed-Size Collections.
Topic 9 – Introduction To Arrays. CISC105 – Topic 9 Introduction to Data Structures Thus far, we have seen “simple” data types. These refers to a single.
Programming with Collections Collections in Java Using Arrays Week 9.
Computer programming1 Arrays. Computer programming2 ARRAYS Motivation Introduction to Arrays Static arrays Arrays and Functions Arrays, Classes, and typedef.
CS102--Object Oriented Programming Lecture 5: – Arrays – Sorting: Selection Sort Copyright © 2008 Xiaoyan Li.
Introduction to Computers and Programming Lecture 15: Arrays Professor: Evan Korth New York University.
1 More on Arrays Passing arrays to or from methods Arrays of objects Command line arguments Variable length parameter lists Two dimensional arrays Reading.
Chapter 3 Using Classes and Objects. 2 Creating Objects  A variable holds either a primitive type or a reference to an object  A class name can be used.
ECE122 L13: Arrays of Objects March 15, 2007 ECE 122 Engineering Problem Solving with Java Lecture 13 Arrays of Objects.
Object References. Objects An array is a collection of values, all of the same type An object is a collection of values, which may be of different types.
Sanjay Goel, School of Business, University at Albany, SUNY 1 MSI 692: Special Topics in Information Technology Lecture 4: Strings & Arrays Sanjay Goel.
Object-Oriented Program Development Using Java: A Class-Centered Approach, Enhanced Edition.
12-CRS-0106 REVISED 8 FEB 2013 CSG2H3 Object Oriented Programming.
M180: Data Structures & Algorithms in Java Arrays in Java Arab Open University 1.
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.
Arrays…JavaCPython have fixed lengthyes*yesno are initialized to default values yesno? track their own lengthyesnoyes trying to access “out of bounds”
Arrays Chapter 13 How to do the following with a one dimensional array: Declare it, use an index.
CMSC 202 Arrays 2 nd Lecture. Aug 6, Array Parameters Both array indexed variables and entire arrays can be used as arguments to methods –An indexed.
CMSC 202 Advanced Section Classes and Objects: Object Creation and Constructors.
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.
Arrays Chapter 6. Objectives learn about arrays and how to use them in Java programs learn how to use array parameters and how to define methods that.
Java – An Object Oriented Language CS 307 Lecture Notes Lecture Weeks 5-6 Khalid Siddiqui.
C++ Programming Lecture 14 Arrays – Part I The Hashemite University Computer Engineering Department (Adapted from the textbook slides)
CS-1030 Dr. Mark L. Hornick 1 References & Pointers.
FUNCTIONS (C) KHAERONI, M.SI. OBJECTIVE After this topic, students will be able to understand basic concept of user defined function in C++ to declare.
Introduction to programming in java Lecture 21 Arrays – Part 1.
Arrays Collections of data Winter 2004CS-1010 Dr. Mark L. Hornick 1.
Coming up ArrayList ArrayList vs Array – Declaration – Insertion – Access – Removal Wrapper classes Iterator object.
ARRAYS II CITS1001. Scope of this lecture Arrays of objects Multi-dimensional arrays The Game of Life (next lecture) 2.
Chapter VII: Arrays.
Array in C# Array in C# RIHS Arshad Khan
Java Arrays and ArrayLists COMP T1 #5
Objects First with Java CITS1001
Arrays.
Arrays 3/4 By Pius Nyaanga.
Foundations of Programming: Arrays
Lecture 6 C++ Programming
Building Java Programs
Arrays Part 1 Topic 19 - Stan Kelly-Bootle
Objects First with Java CITS1001
Advanced Programming Behnam Hatami Fall 2017.
2D Arrays October 12, 2007 ComS 207: Programming I (in Java)
Arrays We often want to organize objects or primitive data in a way that makes them easy to access and change. An array is simple but powerful way to.
Variables Numbers can be stored and retrieved while a program is running if they are given a home. The way that integers and decimal numbers are stored.
Java Programming Arrays
Lecture 18 Arrays and Pointer Arithmetic
Building Java Programs
Building Java Programs
Defining methods and more arrays
Arrays .
Cs212: Data Structures Computer Science Department Lecture 2: Arrays.
CS2011 Introduction to Programming I Arrays (II)
Grouped Data Arrays, and Array Lists.
int [] scores = new int [10];
Arrays.
Building Java Programs
Suggested self-checks: Section 7.11 #1-11
Amortized Analysis and Heaps Intro
Review: libraries and packages
Building Java Programs
Building Java Programs
Arrays 3/4 June 3, 2019 ICS102: The course.
Chapter 6 Arrays.
Classes and Objects Object Creation
Java Coding 6 David Davenport Computer Eng. Dept.,
SPL – PS2 C++ Memory Handling.
CMSC 202 Constructors Version 9/10.
String Objects & its Methods
Presentation transcript:

Objects First with Java CITS1001 Arrays CITS1001 © David J. Barnes and Michael Kölling

Scope of this lecture Arrays Declaring and constructing arrays Using and returning arrays Aliasing

Fixed-size collections Objects First with Java Fixed-size collections ArrayList is used when the size of a collection is not known in advance But in some situations the maximum collection size can be pre-determined For this situation, a special fixed-size collection type is available: the array Arrays can store object references or primitive values Arrays use a special syntax © David J. Barnes and Michael Kölling

Uses of arrays Arrays are used when we have large numbers of same-typed values or objects that we want to operate on as a collection A collection of temperatures that we want to average A collection of student marks that we want to analyse A collection of names that we want to sort e.g. Bureau of Meteorology monthly data We know there are twelve months in a year ALBANY Max 25.1 25.1 24.1 21.5 18.7 16.6 15.7 15.9 17.4 18.8 20.8 23.4 Min 13.5 14.3 13.3 11.6 9.8 8.1 7.4 7.4 7.9 9.0 10.6 12.3 Rain 28 25 29 66 102 104 126 104 81 80 46 24 PERTH AIRPORT Max 31.4 31.7 29.5 25.2 21.4 18.7 17.6 18.3 20.0 22.3 25.4 28.5 Min 16.7 17.4 15.7 12.7 10.2 9.0 8.0 7.9 8.8 10.1 12.4 14.6 Rain 8 14 15 46 108 175 164 117 68 48 25 12

Arrays An array is an indexed sequence of variables of the same type a The array is called a Its elements are called a[0], a[1], a[2], etc. Each element is a separate variable Notice that (as with ArrayList) indexing starts from 0 a a[0] a[1] a[2] a[3] a[4] a[5]

Declaring arrays An array is declared using similar syntax to any other variable int[] a; Declares a to be a variable representing an array of ints double[] temps; Declares temps to be a variable representing an array of doubles String[] names; Declares names to be a variable representing an array of Strings Student[] marks; Declares marks to be a variable representing an array of Student objects

Creating Arrays I An array is an object in a Java program Therefore the declaration simply creates a reference to “point to” the array, but does not create the array itself Hence the declaration int[] a; creates a space called a, big enough to hold an object reference, and initially set to the special value null The declaration creates space for the array reference, but not for the array itself a null

Creating Arrays II In order to actually create the array, we must use the keyword new (just like creating any other object) int[] a; a = new int[7]; An object is created on the heap that contains seven variables of type int a

The size of an array The size of an array is determined when the object is created, not when the variable is declared The expression a.length can be used to refer to the size of the array pointed to by a We will see examples of its use later Note that an array variable can point to different arrays at different times, possibly with different sizes int[] a; a = new int[7]; System.out.println(a.length); a = new int[66]; 7 66

Creating Arrays III The seven variables do not have individual names They are referred to by the array name, and an integer index a[0] a[1] a[2] a[3] a[4] a[5] a[6] The indices go from 0 to a.length–1 a

Referencing array elements Array elements can be used in the same ways and in the same contexts as any other variable of that type a[4] = 15; a[2] = 7; a[3] = 2 * a[2] – a[4]; a[6] = a[0] + 17; a[0] a[1] a[2] 7 a[3] -1 a[4] 15 a[5] a[6] 17

Indexing arrays A lot of the power of arrays comes from the fact that the index can be a variable or an expression int x = 3; a[x] = 5; a[7-x] = a[2*x] * 2 + 10; This is especially useful when arrays are manipulated in loops a[0] a[1] a[2] 7 a[3] 5 a[4] 44 a[5] a[6] 17

Summing the integers in an array Here i is an element of the array a private int sum(int[] a) { int sum = 0; for (int i : a) sum = sum + i; } return sum;

Or using a for loop private int sum(int[] a) { int sum = 0; Here i is being used as an index into the array a private int sum(int[] a) { int sum = 0; for (int i = 0; i < a.length; i++) sum = sum + a[i]; } return sum;

Finding the biggest integer in an array Note that this is only well-defined for non-empty arrays Again i is an element of the array a private int max(int[] a) { int max = a[0]; for (int i : a) if (i > max) max = i; } return max;

Or using a for loop private int max(int[] a) { int max = a[0]; Again i is being used as an index into the array a private int max(int[] a) { int max = a[0]; for (int i = 1; i < a.length; i++) if (a[i] > max) max = a[i]; } return max;

Finding the index of the biggest integer A for-each loop will not work for this example private int max(int[] a) { int k = 0; for (int i = 1; i < a.length; i++) if (a[i] > a[k]) k = i; } return k;

Objects First with Java Array literals An array literal is written using {} Array literals in this form can only be used in declarations Related uses require new int[] numbers = {3, 15, 4, 5}; declaration, creation, and initialization int[] numbers; … numbers = new int[] {3, 15, 4, 5}; © David J. Barnes and Michael Kölling

Returning an array Suppose we want to find the running sums of a e.g. sums({2,5,–8,3}) = {2,7,–1,2} private int[] sums(int[] a) { int[] sums = new int[a.length]; sums[0] = a[0]; for (int i = 1; i < a.length; i++) sums[i] = sums[i–1] + a[i]; } return sums;

Returning an array Suppose we want to average each pair of elements in a e.g. avs({2,6,–8,2}) = {4,–3} private int[] avs(int[] a) { int[] avs = new int[a.length / 2]; for (int i = 0; i < a.length-1; i = i+2) avs[i/2] = (a[i] + a[i+1]) / 2; } return avs;

Updating an array Sometimes instead of creating a new array, we want to update the old one But this method doesn’t return anything… private void sums(int[] a) { for (int i = 1; i < a.length; i++) a[i] = a[i–1] + a[i]; }

Arrays can share memory int[] a, b; a b

Arrays can share memory int[] a, b; a = new int[3]; a b

Arrays can share memory int[] a, b; a = new int[3]; b = a; a b

Arrays can share memory int[] a, b; a = new int[3]; b = a; System.out.println(b[2]); a b

Arrays can share memory int[] a, b; a = new int[3]; b = a; System.out.println(b[2]); a[2] = 9; 9 a b

Arrays can share memory int[] a, b; a = new int[3]; b = a; System.out.println(b[2]); a[2] = 9; 9 9 a b

Aliasing This is called aliasing Basically one array with two names It applies in the same way with any other object-types too Be careful with equality over arrays In some applications we might want to use aliasing deliberately

Changes to parameters are usually lost When we call a method with a parameter of a primitive type, any updates to the parameter are local only The parameter is a new variable private void f(int a) {a = 666;} … int x = 5; System.out.println(x); f(x); 5 x is unchanged by f

But arrays are persistent When we call a method with a parameter of object type, the parameter is a new variable but it refers to the same space on the heap private void f(int[] a) {a[0] = 666;} … int[] x = {5,3,1}; System.out.println(x[0]); f(x); 5 666 x is unchanged by f but x[0] is changed