Arrays ICS 111: Introduction to Computer Science I

Slides:



Advertisements
Similar presentations
Etter/Ingber Arrays and Matrices. Etter/Ingber One-Dimensional Arrays 4 An array is an indexed data structure 4 All variables stored in an array are of.
Advertisements

Chapter 8: Arrays.
Arrays I Savitch Chapter 6.1: Introduction to Arrays.
Arrays. What is an array An array is used to store a collection of data It is a collection of variables of the same type.
Copyright © 2007 Pearson Education, Inc. Publishing as Pearson Addison-Wesley Slide 7- 1 Overview 7.1 Introduction to Arrays 7.2 Arrays in Functions 7.3.
Chapter 10 Introduction to Arrays
Arrays Liang, Chpt 5. arrays Fintan Array of chars For example, a String variable contains an array of characters: An array is a data structure.
Arrays  Writing a program that uses a large amount of information.  Such as a list of 100 elements.  It is not practical to declare.
Arrays. Arrays  When a value is to be used in a program, a variable is declared to hold that value  What about storing the results of exams for a large.
Introduction to Computers and Programming Lecture 15: Arrays Professor: Evan Korth New York University.
 2003 Prentice Hall, Inc. All rights reserved. Modified for use with this course. Introduction to Arrays Introduction to Computers and Programming in.
Arrays.
 Pearson Education, Inc. All rights reserved Arrays.
Introduction to Arrays in Java Corresponds with Chapter 6 of textbook.
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.
JAVA Array 8-1 Outline  Extra material  Array of Objects  enhanced-for Loop  Class Array  Passing Arrays as Arguments to Methods  Returning Arrays.
Chapter 8: Arrays.
Chapter 8: Collections: Arrays. 2 Objectives One-Dimensional Arrays Array Initialization The Arrays Class: Searching and Sorting Arrays as Arguments The.
1 © 2002, Cisco Systems, Inc. All rights reserved. Arrays Chapter 7.
Arrays BCIS 3680 Enterprise Programming. Overview 2  Array terminology  Creating arrays  Declaring and instantiating an array  Assigning value to.
Introduction to Collections Arrays. Collections Collections allow us to treat a group of values as one collective entity. The array is a collection of.
CS 139-Programming Fundamentals Lecture 11B - Arrays Adapted from a presentation by Dr. Rahman Fall 2014.
Structuring Data: Arrays ANSI-C. Representing multiple homogenous data Problem: Input: Desired output:
Arrays and ArrayLists Topic 6. One Dimensional Arrays Homogeneous – all of the same type Contiguous – all elements are stored sequentially in memory For.
COMPUTER PROGRAMMING. Array C++ provides a data structure, the array, which stores a fixed-size sequential collection of elements of the same type. An.
Arrays An array is an indexed data structure which is used to store data elements of the same data type. An array is an indexed data structure which is.
1 Chapter 9 Arrays Java Programming from Thomson Course Tech, adopted by kcluk.
1 Chapter 9 Arrays Java Programming from Thomson Course Tech, adopted by kcluk.
Chapter 9 Arrays. Chapter Objectives Learn about arrays Explore how to declare and manipulate data into arrays Understand the meaning of “array index.
Chapter 9 Arrays. Chapter Objectives Learn about arrays Explore how to declare and manipulate data into arrays Understand the meaning of “array index.
ARRAYS Multidimensional realities Image courtesy of
CS 115 OBJECT ORIENTED PROGRAMMING I LECTURE 11 GEORGE KOUTSOGIANNAKIS 1 Copyright: 2015 Illinois Institute of Technology_ George Koutsogiannakis.
CHAPTER 6 ARRAYS IN C 1 st semester King Saud University College of Applied studies and Community Service Csc 1101 F. Alakeel.
 2005 Pearson Education, Inc. All rights reserved Arrays.
Chapter 7 Arrays…. 7-2 Arrays An array is an ordered list of values An array of size N is indexed from.
Introduction to programming in java Lecture 21 Arrays – Part 1.
KUKUM-06/07 EKT120: Computer Programming 1 Week 6 Arrays-Part 1.
Data Structures & Algorithms CHAPTER 2 Arrays Ms. Manal Al-Asmari.
LESSON 8: INTRODUCTION TO ARRAYS. Lesson 8: Introduction To Arrays Objectives: Write programs that handle collections of similar items. Declare array.
Chapter 8 Arrays and the ArrayList Class Introduction to Arrays.
Arrays of Objects October 9, 2006 ComS 207: Programming I (in Java)
ARRAYS (Extra slides) Arrays are objects that help us organize large amounts of information.
Chapter VII: Arrays.
Sections 10.1 – 10.4 Introduction to Arrays
Array Review CS 139 – November 28, 2007.
Array, Strings and Vectors
Static Variables ICS 111: Introduction to Computer Science I
Set & Get Methods ICS 111: Introduction to Computer Science I
Command Line Arguments
Array Review CS 139 – November 28, 2007.
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.
Unit-1 Introduction to Java
EKT150 : Computer Programming
Declaration, assignment & accessing
Introduction To Programming Information Technology , 1’st Semester
Cs212: Data Structures Computer Science Department Lecture 2: Arrays.
Announcements Lab 7 due Wednesday Assignment 4 due Friday.
Arrays.
Announcements Lab 6 was due today Lab 7 assigned this Friday
CISC181 Introduction to Computer Science Dr
Building Java Programs
Single-Dimensional Arrays chapter6
OBJECT ORIENTED PROGRAMMING I LECTURE 11 GEORGE KOUTSOGIANNAKIS
Arrays Part 2.
Array Review CS 139 – November 28, 2007.
CS150 Introduction to Computer Science 1
CS150 Introduction to Computer Science 1
CS149D Elements of Computer Science
Arrays of Objects October 8, 2007 ComS 207: Programming I (in Java)
COS 151 Bootcamp – Week 4 Department of Computer Science
Presentation transcript:

Arrays ICS 111: Introduction to Computer Science I William Albritton Information and Computer Sciences Department at the University of Hawai‘i at Mānoa "Heavier-than-air flying machines are impossible." Lord Kelvin, president, Royal Society, 1895 11/16/2018 © 2007 William Albritton

Terminology Array Array index (subscript) Array element A list of data of the same type Array index (subscript) A number corresponding to the position of an element in an array Numbered from 0, 1, 2, . . . N-1, where N is the size of the array Array element The value that is stored in an array 11/16/2018 © 2007 William Albritton

Array Examples Array of Integers Array of Doubles finalGrades examScores 0 80 1 77 2 92 3 55 4 80 5 95 index address element Array of Doubles finalGrades 0 83.33 1 73.56 2 91.29 3 55.55 4 67.02 5 83.33 indices addresses elements 11/16/2018 © 2007 William Albritton

Example Code See InitializingArrays.java This program shows how to: Declare arrays Instantiate arrays Initialize arrays Display arrays 11/16/2018 © 2007 William Albritton

Declaring Arrays Syntax for an array declaration Example Java code DataType [] arrayName; Example Java code Integer [] examScores; Double [] finalGrades; A declaration creates a variable with the address to an array, but not the array itself Since arrays are objects in Java, you must declare an array variable (which will later contain the address to the location of the array object) 11/16/2018 © 2007 William Albritton

Declaring Arrays These declarations create a reference variable with a null value (does not point to an object) Integer [] examScores; examScores null Double [] finalGrades; finalGrades null 11/16/2018 © 2007 William Albritton

Variables & Objects A variable stores a reference (address) to an object In the below example, name is a variable that will store the address to a String object String name; null In the below example, examScores is a variable that will store the address to an array of Integers Integer [] examScores; null 11/16/2018 © 2007 William Albritton

Instantiating Arrays Syntax for array instantiation (creation) arrayName = new DataType[SIZE]; SIZE is the number of elements in the array Example Java code final Integer SIZE = 6; //constant examScores = new Integer[SIZE]; finalGrades = new Double[SIZE]; This instantiates (creates) an array object & stores its address in a variable The array object will contain no addresses (null) 11/16/2018 © 2007 William Albritton

Example Array Instantiation Array of integers examScores 0 null 1 null 2 null 3 null index 4 null address 5 null Since no addresses have been assigned, the values stored in the array are “null” Array of doubles finalGrades 0 null 1 null 2 null 3 null 4 null 5 null indices addresses 11/16/2018 © 2007 William Albritton

Class Exercise 1 Declare an array that is used to store Student ID numbers The letters of the alphabet Declare and instantiate an array that is used to store All the high temperatures of this week 5 boolean values Draw the “box & arrow” representation of what this looks like in memory In your drawing, include variable labels, addresses (arrows), element values, and index values 11/16/2018 © 2007 William Albritton

Initializing Arrays Syntax for initializing arrays Example Java code arrayName[i] = value; Where i is the index of an element in the array Example Java code examScores[0] = 80; examScores[1] = 77; examScores[2] = 92; examScores[3] = 80; examScores[4] = 63; examScores[5] = 95; 11/16/2018 © 2007 William Albritton

Initializer List You can also declare, instantiate, & initialize an array with one statement (initializer list) Syntax DataType [] arrayName = {value0, value1, value2, . . ., valueN-1}; The size of the array is automatically calculated Example Java code Double [] finalGrades = {83.33, 73.56, 91.29, 55.55, 67.02,83.33}; 11/16/2018 © 2007 William Albritton

Bug for Initializer List Can only use initializer list when the array is declared Incorrect Java code Double [] finalGrades; finalGrades = {83.33, 73.56, 91.29, 55.55, 67.02,83.33}; //bug! 11/16/2018 © 2007 William Albritton

Array Initialization Array of Integers Array of Doubles finalGrades examScores 0 80 1 77 2 92 3 55 4 80 5 95 index address element Array of Doubles finalGrades 0 83.33 1 73.56 2 91.29 3 55.55 4 67.02 5 83.33 indices addresses elements 11/16/2018 © 2007 William Albritton

Class Exercise 2 Declare and instantiate and initialize an array that is used to store Vowels Even integers from 2 to 100 Draw the “box & arrow” representation of what this looks like in memory In your drawing, include variable labels, addresses (arrows), element values, and index values 11/16/2018 © 2007 William Albritton

Array Size Size of an array is stored in a public variable called length length is not a method! Bug: array.length() Useful in loops for(Integer i=0;i<examScores.length;i++){ System.out.print(examScores[i] + ", "); } 11/16/2018 © 2007 William Albritton

Out of Bounds Error What if an array index is greater or less than range of the array? An exception will be thrown & the program will end Example Java code for(Integer i=0;i<=examScores.length; i++){ System.out.print(examScores[i] + ", ");} Program will end and display a message about an ArrayIndexOutOfBoundsException Since arrays start their index numbering at 0, a common bug is to be “off by one” Range for arrays is 0 to SIZE-1 (length-1) 11/16/2018 © 2007 William Albritton

for-each Loop A for-each loop loops through all elements of an array from lowest to highest index Will loop array.length times Syntax for(DataType element: arrayName){ //use element in code } Java code example for(Double grade: finalGrades){ System.out.print(grade + ", "); } 11/16/2018 © 2007 William Albritton

Class Exercise 3 What is the output of the Java program? See Exercise3.java 11/16/2018 © 2007 William Albritton