CSS161: Fundamentals of Computing

Slides:



Advertisements
Similar presentations
1 Arrays An array is a special kind of object that is used to store a collection of data. The data stored in an array must all be of the same type, whether.
Advertisements

Copyright 2010 by Pearson Education Building Java Programs Chapter 7 Lecture 7-2: Arrays as Parameters reading: , 3.3 self-checks: Ch. 7 #5, 8,
1 Various Methods of Populating Arrays Randomly generated integers.
Introduction to arrays Data in economy size packages.
Copyright 2010 by Pearson Education Building Java Programs Chapter 7 Lecture 7-1: Arrays reading: 7.1 self-checks: #1-9 videos: Ch. 7 #4.
Arrays Chapter 6 Chapter 6.
CS102--Object Oriented Programming Lecture 5: – Arrays – Sorting: Selection Sort Copyright © 2008 Xiaoyan Li.
Chapter 6 Arrays Slides prepared by Rose Williams, Binghamton University Copyright © 2008 Pearson Addison-Wesley. All rights reserved.
1 More on Arrays Passing arrays to or from methods Arrays of objects Command line arguments Variable length parameter lists Two dimensional arrays Reading.
Lecture 15 Arrays: Part 1 COMP1681 / SE15 Introduction to Programming.
CS102--Object Oriented Programming Lecture 6: – The Arrays class – Multi-dimensional arrays Copyright © 2008 Xiaoyan Li.
Slides prepared by Rose Williams, Binghamton University Chapter 6 Arrays.
Java Unit 9: Arrays Declaring and Processing Arrays.
By Nicholas Policelli An Introduction to Java. Basic Program Structure public class ClassName { public static void main(String[] args) { program statements.
French Territory of St. Pierre CSE 114 – Computer Science I Arrays.
Comp 248 Introduction to Programming Chapter 6 Arrays Part B Dr. Aiman Hanna Department of Computer Science & Software Engineering Concordia University,
CMSC 202 Arrays. Aug 6, Introduction to Arrays An array is a data structure used to process a collection of data that is all of the same type –An.
Problem Solving using the Java Programming Language May 2010 Mok Heng Ngee Day 5: Arrays.
Arrays Chapter 8. What if we need to store test scores for all students in our class. We could store each test score as a unique variable: int score1.
Chapter 6Java: an Introduction to Computer Science & Programming - Walter Savitch 1 Chapter 6 l Array Basics l Arrays and Methods l Programming with Arrays.
EE 422C Day 2 Java, Eclipse. Copyright Pearson Education, 2010 Based on slides bu Marty Stepp and Stuart Reges from
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.
Copyright 2010 by Pearson Education Building Java Programs Chapter 7 Lecture 7-2: Arrays as Parameters reading:
Building Java Programs Chapter 7 Arrays Copyright (c) Pearson All rights reserved.
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.
CS 180 Recitation 7 Arrays. Used to store similar values or objects. An array is an indexed collection of data values of the same type. Arrays are the.
Chapter 9 Introduction to Arrays Fundamentals of Java.
Chapter 8 Arrays and the ArrayList Class Arrays of Objects.
Arrays 3/4 By Pius Nyaanga.
Chapter 6 Arrays Slides prepared by Rose Williams, Binghamton University Kenrick Mock, University of Alaska Anchorage.
Arrays 2/4 By Pius Nyaanga.
Introduction to Computing Using Java
Dr. Kyung Eun Park Summer 2017
Chapter 6 Arrays Slides prepared by Rose Williams, Binghamton University Copyright © 2008 Pearson Addison-Wesley. All rights reserved.
Building Java Programs
Building Java Programs
Array traversals, text processing
Program Style Console Input and Output
CSS161: Fundamentals of Computing
Java How to Program, Late Objects Version, 10/e
Building Java Programs Chapter 7
Pass by Reference, const, readonly, struct
An Introduction to Java – Part I, language basics
Building Java Programs
AP Java Warm-up Boolean Array.
Building Java Programs
Building Java Programs
Defining methods and more arrays
Object Oriented Programming in java
Building Java Programs
برنامه نویسی پیشرفته اصول جاوا.
CS2011 Introduction to Programming I Arrays (II)
python.reset() Also moving to a more reasonable room (CSE 403)
Announcements Lab 7 due Wednesday Assignment 4 due Friday.
OBJECT ORIENTED PROGRAMMING II LECTURE 13_2 GEORGE KOUTSOGIANNAKIS
Single-Dimensional Arrays chapter6
Object Oriented Programming
Arrays in Java.
Suggested self-checks: Section 7.11 #1-11
CSS161: Fundamentals of Computing
Building Java Programs
Building Java Programs
Programming with Arrays 2
Building Java Programs
Arrays Wellesley College CS230 Lecture 02 Thursday, February 1
CSS161: Fundamentals of Computing
Arrays 3/4 June 3, 2019 ICS102: The course.
Chapter 6 Arrays.
CSS161: Fundamentals of Computing
Programming with Arrays 1
Presentation transcript:

CSS161: Fundamentals of Computing Arrays and References This slide set was compiled from the Absolute Java textbook slides (Walter Savitch) and the professor’s own class materials. CSS161: Fundamentals of Computing

CSS161: Fundamentals of Computing Arrays Are Objects A reference variable double[] a; An array creation a = new double[10]; = and == operators int[] x = {1, 2, 3}; int[] y = {1, 2, 3}; if ( x == y ) System.out.println( “x and y are the same” ); int[] z = y; if ( y == z ) { System.out.println( “y and z are the same” ); z[2] = 10; // this means y[2] = 10 } a nothing a 0.0 a[0] a[1] a[2] a[8] a[9] 1 [0] 2 [1] 3 [2] x 1 [0] 2 [1] 3 [2] y 10 z CSS161: Fundamentals of Computing

Revisiting Array Initialization Default values int[] age = new int[3]; double[] score = new double[10]; doolean[] passOrNot = new boolean[5]; How about Date[] holidayList = new Date[7]; age[0] age[1] age[2] score[0] 0.0 score[1] 0.0 score[2] 0.0 score[3] 0.0 score[4] 0.0 score[5] 0.0 score[6] 0.0 score[7] 0.0 passOrNot[0] score[8] false 0.0 passOrNot[1] score[9] false 0.0 passOrNot[2] false passOrNot[3] false passOrNot[4] false null holidayList[0] null holidayList[1] null holidayList[2] null holidayList[3] null holidayList[4] null holidayList[5] null holidayList[6] CSS161: Fundamentals of Computing

Arrays with a Class Base Type Instantiate an object of a given class and assign it to each array element. holidayList[0] = new Date(); . . . holidayList[6] = new Date(); OR for (int i = 0; i < holidayList.length; i++) holidayList[i] = new Date(); holidayList[0] Date object holidayList[1] Date object holidayList[2] Date object holidayList[3] Date object holidayList[4] Date object holidayList[5] Date object holidayList[6] Date object CSS161: Fundamentals of Computing

CSS161: Fundamentals of Computing Array Parameters Passing an independent array element A copy of the array element’s value is passed to a method double[] a = { 1.0, 1.414, 1.732, 2.0, 2.236 } square( a[0], a[1], a[2], a[3], a[5] ); Passing an entire array A copy of the array’s reference is passed to a method double[] a = { 1.0, 1.414, 1.732, 2, 2.236 } square( a ); CSS161: Fundamentals of Computing

Passing Independent Array Elements 1.0 a[0] 1.414 a[1] 1.732 a[2] 2.236 a[4] 2.0 a[3] public static void main( String[] args ) { double[] a = { 1.0, 1.414, 1.732, 2.0, 2.236 }; square( a[0], a[1], a[2], a[3], a[4] ); for ( int index = 0; index < a.length; index++ ) System.out.println( “a[“ + index + “] = “ + a[index] ); } public static void square( double a0, double a1, doube a2, double a3, double a4 ) { System.out.println( “Square of a0 = “ + ( a0 *= a0 ) ); System.out.println( “Square of a0 = “ + ( a1*= a1 ) ); System.out.println( “Square of a0 = “ + ( a2 *= a2 ) ); System.out.println( “Square of a0 = “ + ( a3 *= a3 ) ); System.out.println( “Square of a0 = “ + ( a4 *= a4 ) ); 1.0 1.999 2.999 4.0 4.999 1.0 1.414 1.732 2.0 2.236 CSS161: Fundamentals of Computing

Passing an Entire Array 1.0 a[0] 1.414 a[1] 1.732 a[2] 2.236 a[4] 2.0 a[3] length: 5 a 1.0 1.999 2.999 4.999 4.0 public static void main( String[] args ) { double[] a = { 1.0, 1.414, 1.732, 2.0, 2.236 }; square( a ); for ( int index = 0; index < a.length; index++ ) System.out.println( “a[“ + index + “] = “ + a[index] ); } public static void square( double[] a ) { System.out.println( “Square of a[index] = “ + ( a[index] *= a[index] ) ); No need to specify the length [] is necessary to specify an array parameter CSS161: Fundamentals of Computing

CSS161: Fundamentals of Computing Copying an Array Pitfall: use of = with arrays Copying just a reference, (I.e., shallow copy) int[] score = {1, 2, 3, 4, 5}; int[] copy = score; copy[3] = 10; System.out.println( “score[3] = “ + score[3] + “ copy[3] = “ + copy[3] ); Deep copy Copying each array element int[] copy = new int[score.length]; for ( int index = 0; index < length; length++ ) copy[index] = score[index]; [0] [1] [2] [3] [4] 1 2 3 4 5 10 10 [0] [1] [2] [3] [4] 1 2 3 4 5 [0] [1] [2] [3] [4] 1 2 3 10 4 5 4 10 CSS161: Fundamentals of Computing

CSS161: Fundamentals of Computing Comparing Arrays Pitfall: use == with arrays Comparing just references int[] score1 = {1, 2, 3, 4, 5}; int[] score2 = score1; int[] score3 = {1, 2, 3, 4, 5}; if ( score1 == score2 ) System.out.println( “Their references are identical” ); if ( score1 != score3 ) System.out.println( “Their references are different” ); Comparing each array element int[] score2 = {1, 2, 3, 4, 5}; if ( score1.length != score2.length ) System.out.println( “different” ); else { int index; for ( index = 0; index < socre1.length; index++ ) if ( score1[index] != socre2[index] ) { break; } if ( index == score1.length ) System.out.println( “identical” ); [0] [1] [2] [3] [4] 1 2 3 4 5 [0] [1] [2] [3] [4] 1 2 3 4 5 [0] [1] [2] [3] [4] 1 2 3 4 5 [0] [1] [2] [3] [4] 1 2 3 4 5 CSS161: Fundamentals of Computing

CSS161: Fundamentals of Computing Example public class ArrayComp { public static void main( String[] args ) { int[] score1 = { 1, 2, 3, 4, 5 }; int[] score2 = new int[score1.length]; for ( int index = 0; index < score1.length; index++ ) score2[index] = score1[index]; System.out.println( "Are socre1 and score2 identical? " + compareArray( score1, score2 ) ); int[] score3 = { 1, 2, 3, 4, 10 }; System.out.println( "Are socre1 and score3 identical? " + compareArray( score1, score3 ) ); } public static boolean compareArray( int[] array1, int[] array2 ) { if ( array1.length != array2.length ) { return false; }else { for ( int index = 0; index < array1.length; index++ ) if ( array1[index] != array2[index] ) return true; [0] [1] [2] [3] [4] 1 2 3 4 5 [0] [1] [2] [3] [4] 1 2 3 4 5 [0] [1] [2] [3] [4] 1 2 3 4 10 CSS161: Fundamentals of Computing

CSS161: Fundamentals of Computing Self-Test Exercises Work on textbook p350 and p351’s exercises 7 ~ 10. CSS161: Fundamentals of Computing

Argument for the Method main The main method of a program has a parameter for an array of String. public static void main(String[] args) When JVM starts a program with user arguments, they are passed to args[0] …args[args.length - 1] public class SomeProgram { public static void main(String[] args) { for ( int index = 0; index < args.length; index++ ) System.out.println(args[index]); } arg[0] “hello” arg[1] “world” arg[2] “123” % java SomeProgram hello world 123 hello world 123 % CSS161: Fundamentals of Computing

Methods that Return an Array Syntax public static Base_Type[] Method_Name( Parameter_List ) { Base_Type[] temp = new Base_Type[Array_Size]; // Some code to fill temp goes here. return temp; } Example public static int[] incrementArray( int[] a, int increment ) int[] temp = new int[a.length]; for ( int index = 0; index < a.length; index++ ) temp[index] = a[index] + increment; CSS161: Fundamentals of Computing

Mechanism to Return a Value 1 2 3 4 5 [0] [1] [2] [3] [4] length: 5 public class SomeProgram { public static void main( String[] args ) { int[] original = {1, 2, 3, 4, 5} int[] incremented = incrementArray( original, 10 ); } public static int[] incrementArray( int[] a, int increment ) { int[] temp = new int[a.length]; int index; for ( index = 0; index < a.length; index++ ) temp[index] = a[index] + increment; return temp; Parameter a points to what original points to reference original copied Stored into incremented 0 ~ 5 [0] [1] [2] [3] [4] length: 5 11 12 13 14 15 A copy of reference temp is created. CSS161: Fundamentals of Computing

CSS161: Fundamentals of Computing Self-Test Exercises Work on textbook p356’s exercises 11 ~ 12. CSS161: Fundamentals of Computing