Array. Lecture 12. Based on Slides of Dr. Norazah Yusof

Similar documents
CS1150 Principles of Computer Science Arrays

Chapter 6 Arrays. Liang, Introduction to Java Programming, Eighth Edition, (c) 2011 Pearson Education, Inc. All rights reserved.

Chapter 6 Single-Dimensional Arrays. Liang, Introduction to Java Programming, Tenth Edition, (c) 2015 Pearson Education, Inc. All rights reserved.

Java-Array. This tutorial introduces how to declare array variables, create arrays, and process arrays using indexed variables.

CS1150 Principles of Computer Science Arrays

14. Array Basics. Java. Summer 2008 Instructor: Dr. Masoud Yaghini

Chapter 7: Single-Dimensional Arrays. Declaring Array Variables. Creating Arrays. Declaring and Creating in One Step.

Liang, Introduction to Java Programming, Eighth Edition, (c) 2011 Pearson Education, Inc. All rights reserved

Announcements. PS 4 is ready, due next Thursday, 9:00pm. Midterm Exam 1: 10/14 (Fri), 9:00am-10:53am

Arrays. Eng. Mohammed Abdualal

Arrays. Introduction to OOP with Java. Lecture 06: Introduction to OOP with Java - AKF Sep AbuKhleiF - 1

Chapter 7 Single-Dimensional Arrays

Opening Problem EXAMPLE. 1. Read one hundred numbers, 2. compute their average, and 3. find out how many numbers are above the average.

Module 7: Arrays (Single Dimensional)

CS115 Principles of Computer Science

Arrays and Lists CSC 121 Fall 2016 Howard Rosenthal

Introduction to Java & Fundamental Data Types

Arrays and Lists CSC 121 Fall 2014 Howard Rosenthal

Arrays and Lists CSC 121 Fall 2015 Howard Rosenthal

Arrays and Lists Review CSC 123 Fall 2018 Howard Rosenthal

Computer Programming, I. Laboratory Manual. Final Exam Solution

Object Oriented Programming. Java-Lecture 6 - Arrays

ECE 122. Engineering Problem Solving with Java

Object-oriented programming. and data-structures CS/ENGRD 2110 SUMMER 2018

Review. Primitive Data Types & Variables. String Mathematical operators: + - * / % Comparison: < > <= >= == int, long float, double boolean char

Chapter 6 SINGLE-DIMENSIONAL ARRAYS

Declaring Array Variable

Chapter 4 Loops. Liang, Introduction to Java Programming, Eighth Edition, (c) 2011 Pearson Education, Inc. All rights reserved.

Programming with Java

Loops. CSE 114, Computer Science 1 Stony Brook University

CSE 201 JAVA PROGRAMMING I. Copyright 2016 by Smart Coding School

Getting started with Java

Birkbeck (University of London) Software and Programming 1 In-class Test Mar Answer ALL Questions

Software Practice 1 Basic Grammar

Computer programming Code exercises [1D Array]

Types in Java. 8 Primitive Types. What if we want to store lots of items e.g. midsem marks?

Software Practice 1 - Basic Grammar Basic Syntax Data Type Loop Control Making Decision

C212 Early Evaluation Exam Mon Feb Name: Please provide brief (common sense) justifications with your answers below.

Tutorial 11. Exercise 1: CSC111 Computer Programming I. A. Write a code snippet to define the following arrays:

(A) 99 ** (B) 100 (C) 101 (D) 100 initial integers plus any additional integers required during program execution

Java Coding 3. Over & over again!

Chapter 6. Arrays. Java Actually: A Comprehensive Primer in Programming

Give one example where you might wish to use a three dimensional array

Controls Structure for Repetition

CS111: PROGRAMMING LANGUAGE II

Java Loop Control. Programming languages provide various control structures that allow for more complicated execution paths.

Passing Array to Methods

Final Examination Semester 2 / Year 2011

Problems with simple variables

(A) 99 (B) 100 (C) 101 (D) 100 initial integers plus any additional integers required during program execution

Arrays. Here is the generic syntax for an array declaration: type[] <var_name>; Here's an example: int[] numbers;

Java Programming. MSc Induction Tutorials Stefan Stafrace PhD Student Department of Computing

CS313D: ADVANCED PROGRAMMING LANGUAGE

Handout 5 cs180 - Programming Fundamentals Spring 15 Page 1 of 8. Handout 5. Loops.

Loops and Expression Types

CS 101 Spring 2007 Midterm 2 Name: ID:

JAVA PROGRAMMING LAB. ABSTRACT In this Lab you will learn to define and invoke void and return java methods

Method OverLoading printf method Arrays Declaring and Using Arrays Arrays of Objects Array as Parameters

13 th Windsor Regional Secondary School Computer Programming Competition

Basic computer skills such as using Windows, Internet Explorer, and Microsoft Word. Chapter 1 Introduction to Computers, Programs, and Java

Chapter 8 Multi-Dimensional Arrays

CompSci 125 Lecture 11

CS111: PROGRAMMING LANGUAGE II

CSE 20. SAMPLE FINAL Version A Time: 180 minutes. The following precedence table is provided for your use:

Chapter 8 Multidimensional Arrays

Array. Array Declaration:

Choose 3 of the 1 st 4 questions (#'s 1 through 4) to complete. Each question is worth 12 points.

Assignment 2.4: Loops

CIS 1068 Program Design and Abstraction Spring2016 Midterm Exam 1. Name SOLUTION

CS5000: Foundations of Programming. Mingon Kang, PhD Computer Science, Kennesaw State University

Birkbeck (University of London) Software and Programming 1 In-class Test Mar 2018

Interpreted vs Compiled. Java Compile. Classes, Objects, and Methods. Hello World 10/6/2016. Python Interpreted. Java Compiled

INDEX. A SIMPLE JAVA PROGRAM Class Declaration The Main Line. The Line Contains Three Keywords The Output Line

Arrays. Lecture 11 CGS 3416 Fall October 26, 2015

1.00 Introduction to Computers and Engineering Problem Solving. Quiz 1 March 7, 2003

Lecture 14. 'for' loops and Arrays

CISC-124. Casting. // this would fail because we can t assign a double value to an int // variable

CSC 1051 Algorithms and Data Structures I. Final Examination May 12, Name: KEY. Question Value Score

Chapter 12: Arrays Think Java: How to Think Like a Computer Scientist by Allen B. Downey

Fundamentals of Programming Data Types & Methods

COMP 250: Java Programming I. Carlos G. Oliver, Jérôme Waldispühl January 17-18, 2018 Slides adapted from M. Blanchette

Lecture 2 Arrays, Searching and Sorting (Arrays, multi-dimensional Arrays)

Computer Science is...

University of Cape Town ~ Department of Computer Science. Computer Science 1015F ~ 2007

1. Which of the following is the correct expression of character 4? a. 4 b. "4" c. '\0004' d. '4'

Arrays and the TOPICS CHAPTER 8. CONCEPT: An array can hold multiple values of the same data type simultaneously.

Java Basic Programming Constructs

PROGRAMMING FUNDAMENTALS

Fall 2017 CISC124 9/16/2017

10/30/2010. Introduction to Control Statements. The if and if-else Statements (cont.) Principal forms: JAVA CONTROL STATEMENTS SELECTION STATEMENTS

COMP Primitive and Class Types. Yi Hong May 14, 2015

Course Outline. Introduction to java

Arrays. Lecture 11 CGS 3416 Spring March 6, Lecture 11CGS 3416 Spring 2017 Arrays March 6, / 19

CS 211: Methods, Memory, Equality

CSE 1223: Exam II Autumn 2016

SAMPLE QUESTIONS FOR DIPLOMA IN INFORMATION TECHNOLOGY; YEAR 1

Lecture Set 2: Starting Java

Identifiers and Variables

Sequence structure. The computer executes java statements one after the other in the order in which they are written. Total = total +grade;

CS 152: Data Structures with Java Hello World with the IntelliJ IDE

Transcription:

Array Lecture 12 Based on Slides of Dr. Norazah Yusof 1

Introducing Arrays Array is a data structure that represents a collection of the same types of data. In Java, array is an object that can store a group of values, all of the same type.

Declare and create an array Declare and create array is similar to create and create any other type of object. Example question: Declare and create 1-element double array referenced by the variable, mylist.

Declare and create an array in two steps Step 1: declare an array reference datatype[] arrayrefvar; or datatype arrayrefvar[]; // This style is allowed, // but not preferred Step 2: create an array arrayrefvar = new datatype[arraysize]; Example double[] mylist; mylist = new double[1]; 4

Declare and create an array in one step Declare and create the array datatype[] arrayrefvar = new datatype[arraysize]; or datatype arrayrefvar[] = new datatype[arraysize]; Example double[] mylist = new double[1]; or double mylist[] = new double[1]; 5

The Length of an Array Once an array is created, its size is fixed. It cannot be changed. You can find its size using arrayrefvar.length For example, mylist.length returns 1 6

Default Values When an array is created, its elements are assigned the default value of for the numeric primitive data types, '\u' (unicode value) for char types, and false for boolean types. 7

Arrays Subscript Integer contained within square brackets Indicates one of array s variables or elements double[] mylist = new double[1]; mylis t reference mylist[] mylist[1] Array reference variable mylist[2] mylist[3] Array element at index 5 mylist[4] mylist[5] mylist[6] Element value mylist[7] mylist[8] mylist[9] 8

Arrays Array s elements numbered beginning with zero Can legally use any subscript from through mylist.length-1 double[] mylist = new double[1]; mylis t reference mylist[] mylist[1] Array reference variable mylist[2] mylist[3] Array element at index 5 mylist[4] mylist[5] mylist[6] mylist[7] mylist[8] mylist[9] Element value 9

Self- test 1 Identify and correct the syntax error in each of the following array declaration. (a) int arr = new int[15]; (b) int arr[] = new int(15); (c) float arr[] = new [3]; 1

Accessing Array Elements The array elements are accessed through the index, known as indexed variable arrayrefvar[index]; An indexed variable can be used in the same way as a regular variable. For example, the value of the array element may be assigned as follows: mylist[] = 5.6; mylist[1] = mylist[] 1.1; Scanner inp = new Scanner(System.in); mylist[2] = inp.nextdouble(); 11

Accessing Array Elements The array elements can be output using the println method. System.out.println (mylist[]); System.out.println (mylist[1]); System.out.println (mylist[2]); 12

Using loop to cycle through the entire array Perform loops that vary loop control variable Java Programming, Fourth Edition Start at End at one less than size of array Using constant that is equal to size of array final int SIZE = 1; for(int i = ; i < SIZE; i++) mylist[i] += 3; Or using the length field, that contains the number of elements in array for(int i = ; i < mylist.length; i++) mylist[i] += 3; 13

Declaring, creating, initializing Using the Shorthand Notation Declare and create the array datatype[] arrayrefvar = {arrylist1, arrylist2, ; Example double[] mylist = {5.6, 4.5, 3.3, 13.2, 4, 34.33, 34, 45.45, 99.993, 11.3; 14

Declaring, creating, initializing Using the Shorthand Notation double[] mylist = {5.6, 4.5, 3.3, 13.2, 4, 34.33, 34, 45.45, 99.993, 11.3; This shorthand notation is equivalent to the following statements: double[] mylist = new double[1]; mylist[] = 5.6; mylist[1] = 4.5; mylist[2] = 3.3; mylist[3] = 13.2; mylist[4] = 4; mylist[5] = 34.33; mylist[6] = 34; mylist[7] = 45.45; mylist[8] = 99.993; mylist[9] = 11.3; 15

CAUTION!! Using the shorthand notation, you have to declare, create, and initialize the array all in one statement. Splitting it would cause a syntax error. For example, the following is wrong: double[] mylist; mylist = {1.9, 2.9, 3.4, 3.5; 16

Arrays Array s elements numbered beginning with zero Can legally use any subscript from through 9 double[] mylist = new double[1]; mylis t reference mylist[] 5.6 mylist[1] 4.5 Array reference variable mylist[2] mylist[3] 3.3 13.2 Array element at index 5 mylist[4] mylist[5] mylist[6] 4 34.33 34 Element value mylist[7] 45.45 mylist[8] mylist[9] 99.993 11.3 17

Self- test 3 Identify and correct the syntax error in each of the following array declaration. (a) float arr[] = new float {1.,2.,3.; 18

Self- test 4 Assume the array declaration and initialization is as follows. int arr[] = {2,4,6,8,1; State whether it is valid/invalid? (a) arr[4] (f) arr[ 5 % 2 ] (b) arr[ arr.length ] (g) arr[ arr[ arr[] ] ] (c) arr[ arr[] ] (h) arr[ 5 / 2. ] (d) arr[ arr.length / 2 ] (i) arr[ arr[3] / 2] (e) arr[ arr[1] ] 19

Trace Program with Arrays Given the following program, what will be the output? public class Test { public static void main(string[] args) { int[] values = new int[5]; for (int i = 1; i < 5; i++) { values[i] = i + values[i-1]; values[] = values[1] + values[4]; 2

Trace Program with Arrays Declare array variable values, create an array, and assign its reference to values public class Test { public static void main(string[] args) { int[] values = new int[5]; for (int i = 1; i < 5; i++) { values[i] = values[i] + values[i-1]; values[] = values[1] + values[4]; After the array is created 1 2 3 4 21

Trace Program with Arrays i becomes 1 public class Test { public static void main(string[] args) { int[] values = new int[5]; for (int i = 1; i < 5; i++) { values[i] = values[i] + values[i-1]; values[] = values[1] + values[4]; After the array is created 1 2 3 4 22

Trace Program with Arrays i (=1) is less than 5 public class Test { public static void main(string[] args) { int[] values = new int[5]; for (int i = 1; i < 5; i++) { values[i] = values[i] + values[i-1]; values[] = values[1] + values[4]; After the array is created 1 2 3 4 23

Trace Program with Arrays After this line is executed, value[1] is 1 public class Test { public static void main(string[] args) { int[] values = new int[5]; for (int i = 1; i < 5; i++) { values[i] = i + values[i-1]; values[] = values[1] + values[4]; After the first iteration 1 1 2 3 4 24

Trace Program with Arrays After i++, i becomes 2 public class Test { public static void main(string[] args) { int[] values = new int[5]; for (int i = 1; i < 5; i++) { values[i] = values[i] + values[i-1]; values[] = values[1] + values[4]; After the first iteration 1 1 2 3 4 25

Trace Program with Arrays i (= 2) is less than 5 public class Test { public static void main(string[] args) { int[] values = new int[5]; for (int i = 1; i < 5; i++) { values[i] = values[i] + values[i-1]; values[] = values[1] + values[4]; After the first iteration 1 1 2 3 4 26

Trace Program with Arrays After this line is executed, values[2] is 3 (2 + 1) public class Test { public static void main(string[] args) { int[] values = new int[5]; for (int i = 1; i < 5; i++) { values[i] = i + values[i-1]; values[] = values[1] + values[4]; After the second iteration 1 1 2 3 3 4 27

Trace Program with Arrays After this, i becomes 3. public class Test { public static void main(string[] args) { int[] values = new int[5]; for (int i = 1; i < 5; i++) { values[i] = i + values[i-1]; values[] = values[1] + values[4]; After the second iteration 1 1 2 3 3 4 28

Trace Program with Arrays i (=3) is still less than 5. public class Test { public static void main(string[] args) { int[] values = new int[5]; for (int i = 1; i < 5; i++) { values[i] = i + values[i-1]; values[] = values[1] + values[4]; After the second iteration 1 1 2 3 3 4 29

Trace Program with Arrays After this line, values[3] becomes 6 (3 + 3) public class Test { public static void main(string[] args) { int[] values = new int[5]; for (int i = 1; i < 5; i++) { values[i] = i + values[i-1]; values[] = values[1] + values[4]; After the third iteration 1 1 2 3 3 6 4 3

Trace Program with Arrays After this, i becomes 4 public class Test { public static void main(string[] args) { int[] values = new int[5]; for (int i = 1; i < 5; i++) { values[i] = i + values[i-1]; values[] = values[1] + values[4]; After the third iteration 1 1 2 3 3 6 4 31

Trace Program with Arrays i (=4) is still less than 5 public class Test { public static void main(string[] args) { int[] values = new int[5]; for (int i = 1; i < 5; i++) { values[i] = i + values[i-1]; values[] = values[1] + values[4]; After the third iteration 1 1 2 3 3 6 4 32

Trace Program with Arrays After this, values[4] becomes 1 (4 + 6) public class Test { public static void main(string[] args) { int[] values = new int[5]; for (int i = 1; i < 5; i++) { values[i] = i + values[i-1]; values[] = values[1] + values[4]; After the fourth iteration 1 1 2 3 3 6 4 1 33

Trace Program with Arrays After i++, i becomes 5 public class Test { public static void main(string[] args) { int[] values = new int[5]; for (int i = 1; i < 5; i++) { values[i] = i + values[i-1]; values[] = values[1] + values[4]; After the fourth iteration 1 1 2 3 3 4 6 1 34

Trace Program with Arrays i ( =5) < 5 is false. Exit the loop public class Test { public static void main(string[] args) { int[] values = new int[5]; for (int i = 1; i < 5; i++) { values[i] = i + values[i-1]; values[] = values[1] + values[4]; After the fourth iteration 1 1 2 3 3 6 4 1 35

Trace Program with Arrays After this line, values[] is 11 (1 + 1) public class Test { public static void main(string[] args) { int[] values = new int[5]; for (int i = 1; i < 5; i++) { values[i] = i + values[i-1]; values[] = values[1] + values[4]; 1 2 3 4 11 1 3 6 1 36

Enhanced for Loop JDK 1.5 and above introduced a new for loop that enables you to traverse the complete array sequentially without using an index variable. For example, the following code displays all elements in the array scorearray: for(int val : scorearray) System.out.println(val); In general, the syntax is for (elementtype value: arrayrefvar) { // Process the value You still have to use an index variable if you wish to traverse the array in a different order or change the elements in the array. 37

Array Processing 1. (Printing arrays) 2. (Summing all elements) 3. (Finding the largest element) 4. (Finding the smallest index of the largest element) 38

Example: Print an Array Print an array of int and an array of double: public class PrintArrays { static final int ARRSIZE = 1; // The array's size static int intarr[] = new int[arrsize]; // Create the int array static double realarr[] = { 1.1, 2.2, 3.3, 4.4, 5.5, 6.6, 7.7, 8.8, 9.9, 1.1 ; // And a double array public static void main(string args[]) { System.out.println("Ints \t Reals"); for (int k = ; k < intarr.length; k++) System.out.println( intarr[k] + " \t " + realarr[k]); // main() // PrintArrays in order to refer to them in static main() Uninitialized int array has default values of. These must be static... Program Output Ints Reals 1.1 2.2 3.3 4.4 5.5 6.6 7.7 8.8 9.9 1.139

Example: Store the First 1 Squares public class Squares { static final int ARRSIZE = 1; static int intarr[] = new int[arrsize]; // The array's size // Create an int array public static void main(string args[]) { for (int k = ; k < intarr.length; k++) // Initialize the array intarr[k] = (k+1) * (k+1); heading System.out.print("The first 1 squares are"); // Print a for (int k = ; k < intarr.length; k++) { // Print the array if (k % 1 == ) System.out.println(" "); // 1 elements per row // for // main() // Squares System.out.print( intarr[k] + " "); Program Output The first 1 squares are 1 4 9 16 25 36 49 64 81 1 121 144 169 196 225 256 289 324 361 4 441 484 529 576 625 676 729 784 841 9 961 124 189 1156 1225 1296 1369 1444 1521 16 1681 1764 1849 1936 225 2116 229 234 241 25 261 274 289 2916 325 3136 3249 3364 3481 36 3721 3844 3969 496 4225 4356 4489 4624 4761 4 49 541 5184 5329 5476 5625 5776 5929 684 6241 64 6561 6724 6889 756 7225 7396 7569 7744 7921 81

class ArrayDemo1 { public static void main(string[] args) { int anarray[] = {1, 2, 3, 4, 5, 6, 7, 8, 9, 1; for (int i = ; i < anarray.length; i++) { System.out.println("Element at index" + i + ": " + anarray[i]);

class ArrayDemo2 { public static void main(string[] args) { int anarray[] = {1, 2, 3, 4, 5, 6, 7, 8, 9, 1; for (int i: anarray) { System.out.println(i);