Comp 248 Introduction to Programming Chapter 6 Arrays Part A Dr. Aiman Hanna Department of Computer Science & Software Engineering Concordia University,

Slides:



Advertisements
Similar presentations
Copyright © 2003 Pearson Education, Inc. Slide 1.
Advertisements

Copyright © 2002 Pearson Education, Inc. Slide 1.
Chapter 9 – One-Dimensional Numeric Arrays. Array u Data structure u Grouping of like-type data u Indicated with brackets containing positive integer.
Arrays.
Arrays I Savitch Chapter 6.1: Introduction to Arrays.
Generics and the ArrayList Class
Chapter 10 Introduction to Arrays
Chapter 6: Arrays Java Software Solutions for AP* Computer Science
Comp 249 Programming Methodology Chapter 7 - Inheritance – Part A Dr. Aiman Hanna Department of Computer Science & Software Engineering Concordia University,
Slides prepared by Rose Williams, Binghamton University Chapter 6 Arrays.
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.
Slides prepared by Rose Williams, Binghamton University Chapter 14 Generics and the ArrayList Class.
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.
Slides prepared by Rose Williams, Binghamton University Chapter 5 Defining Classes II.
© The McGraw-Hill Companies, 2006 Chapter 5 Arrays.
Slides prepared by Rose Williams, Binghamton University Chapter 14 Generics and the ArrayList Class.
Chapter 14 Generics and the ArrayList Class Copyright © 2010 Pearson Addison-Wesley. All rights reserved.
Slides prepared by Rose Williams, Binghamton University Chapter 6 Arrays.
 Pearson Education, Inc. All rights reserved Arrays.
CS0007: Introduction to Computer Programming Introduction to Arrays.
© 2011 Pearson Education, publishing as Addison-Wesley 1 Arrays  Arrays are objects that help us organize large amounts of information  Chapter 6 focuses.
Chapter 6Java: an Introduction to Computer Science & Programming - Walter Savitch 1 l Array Basics l Arrays in Classes and Methods l Programming with Arrays.
Comp 248 Introduction to Programming Chapter 4 - Defining Classes Part A Dr. Aiman Hanna Department of Computer Science & Software Engineering Concordia.
Comp 249 Programming Methodology Chapter 8 - Polymorphism Dr. Aiman Hanna Department of Computer Science & Software Engineering Concordia University, Montreal,
CPS120: Introduction to Computer Science Arrays. Arrays: A Definition A list of variables accessed using a single identifier May be of any data type Can.
Comp 249 Programming Methodology Chapter 10 – Recursion Prof. Aiman Hanna Department of Computer Science & Software Engineering Concordia University, Montreal,
Comp 248 Introduction to Programming Chapter 2 - Console Input & Output Dr. Aiman Hanna Department of Computer Science & Software Engineering Concordia.
Comp 248 Introduction to Programming Chapter 6 Arrays Part B Dr. Aiman Hanna Department of Computer Science & Software Engineering Concordia University,
 2005 Pearson Education, Inc. All rights reserved. 1 Arrays Part 4.
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: An Introduction to Problem Solving & Programming, 5 th Ed. By Walter Savitch and Frank Carrano. ISBN © 2009 Pearson Education, Inc., Upper.
 2005 Pearson Education, Inc. All rights reserved. 1 Arrays.
What is an Array? An array is a collection of variables. Arrays have three important properties: –group of related items(for example, temperature for.
Chapter 8: Arrays.
ARRAYS 1 TOPIC 8 l Array Basics l Arrays and Methods l Programming with Arrays Arrays.
1 © 2002, Cisco Systems, Inc. All rights reserved. Arrays Chapter 7.
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.
Java™ How to Program, 9/e © Copyright by Pearson Education, Inc. All Rights Reserved.
Introduction to Collections Arrays. Collections Collections allow us to treat a group of values as one collective entity. The array is a collection of.
Arrays An array is a data structure that consists of an ordered collection of similar items (where “similar items” means items of the same type.) An array.
Chapter 6Java: an Introduction to Computer Science & Programming - Walter Savitch 1 Chapter 6 l Array Basics l Arrays and Methods l Programming with Arrays.
Chapter 14 Generics and the ArrayList Class Slides prepared by Rose Williams, Binghamton University Copyright © 2008 Pearson Addison-Wesley. All rights.
8-1 Chapter 8: Arrays Arrays are objects that help us organize large amounts of information Today we will focuses on: –array declaration and use –bounds.
Comp 249 Programming Methodology Chapter 13 Interfaces & Inner Classes Dr. Aiman Hanna Department of Computer Science & Software Engineering Concordia.
Comp 248 Introduction to Programming Chapter 4 & 5 Defining Classes Part C Dr. Aiman Hanna Department of Computer Science & Software Engineering Concordia.
CPS120: Introduction to Computer Science Lecture 15 Arrays.
Comp 248 Introduction to Programming Chapter 4 & 5 Defining Classes Part B Dr. Aiman Hanna Department of Computer Science & Software Engineering Concordia.
Copyright © 2010 Pearson Education, Inc. Publishing as Pearson Addison-Wesley Chapter 5: Looping.
Starting Out With Java 5 Control Structures to Objects By Tony Gaddis Copyright © 2005, Pearson Addison-Wesley. All rights reserved. Chapter 4 Slide #1.
Arrays.
Array Size Arrays use static allocation of space. That is, when the array is created, we must specify the size of the array, e.g., int[] grades = new int[100];
Arrays Declaring arrays Passing arrays to functions Searching arrays with linear search Sorting arrays with insertion sort Multidimensional arrays Programming.
Liang, Introduction to Java Programming, Fifth Edition, (c) 2005 Pearson Education, Inc. All rights reserved Chapter 6 Arrays.
1 Arrays Chapter 8. Objectives You will be able to Use arrays in your Java programs to hold a large number of data items of the same type. Initialize.
CHAPTER 6 ARRAYS IN C 1 st semester King Saud University College of Applied studies and Community Service Csc 1101 F. Alakeel.
Sections 10.1 – 10.4 Introduction to Arrays
Chapter 3 Loops Section 3.3 Slides prepared by Rose Williams, Binghamton University Kenrick Mock, University of Alaska Anchorage.
Comp 249 Programming Methodology
Comp 249 Programming Methodology
Comp 249 Programming Methodology
Comp 249 Programming Methodology
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.
Comp 249 Programming Methodology
Copyright © Aiman Hanna All rights reserved
Comp 249 Programming Methodology
Copyright © Aiman Hanna All rights reserved
Comp 249 Programming Methodology
Multidimensional Arrays Section 6.4
Presentation transcript:

Comp 248 Introduction to Programming Chapter 6 Arrays Part A Dr. Aiman Hanna Department of Computer Science & Software Engineering Concordia University, Montreal, Canada These slides has been extracted, modified and updated from original slides of Absolute Java 3 rd Edition by Savitch; which has originally been prepared by Rose Williams of Binghamton University. Absolute Java is published by Pearson Education / Addison-Wesley. Copyright © 2007 Pearson Addison-Wesley Copyright © Aiman Hanna All rights reserved

Introduction to Arrays An array is a data structure used to process a collection of data that is all of the same type An array is a data structure used to process a collection of data that is all of the same type An array of size N is indexed from zero to N-1 scores The entire array has a single name Each value has a numeric index The above array holds 10 values that are indexed from 0 to 9

Creating and Accessing Arrays An array is declared and created in almost the same way that objects are declared and created An array is declared and created in almost the same way that objects are declared and created An array of double values can be created using one statement as follows: An array of double values can be created using one statement as follows: double[] score = new double[5]; Or using two statements: Or using two statements: double[] score; score = new double[5]; The first statement declares the variable score to be of the array type double[] The first statement declares the variable score to be of the array type double[] The second statement creates an array with five numbered variables of type double and makes the variable score a name for the array The second statement creates an array with five numbered variables of type double and makes the variable score a name for the array 6-3

Creating and Accessing Arrays 6-4 ArrayOperations1.java (MS-Word file) ArrayOperations1.java (MS-Word file) ArrayOperations1.javaMS-Word file ArrayOperations1.javaMS-Word file ArrayOperations2.java (MS-Word file) ArrayOperations2.java (MS-Word file) ArrayOperations2.javaMS-Word file ArrayOperations2.javaMS-Word file ArrayOperations3.java (MS-Word file) ArrayOperations3.java (MS-Word file) ArrayOperations3.javaMS-Word file ArrayOperations3.javaMS-Word file

Creating and Accessing Arrays double[] score = new double[5]; A variable may be used in place of the integer (i.e., in place of the integer 5 above ) A variable may be used in place of the integer (i.e., in place of the integer 5 above ) The value of this variable can then be read from the keyboard The value of this variable can then be read from the keyboard This enables the size of the array to be determined when the program is run This enables the size of the array to be determined when the program is run double[] score = new double[count]; An array can have indexed variables of any type, including any class type An array can have indexed variables of any type, including any class type Car[] arr1 = new Car[10]; // array of 10 cars // MUST BE CAREFUL HERE. DO WE REALLY HAVE 10 Cars? All of the indexed variables in a single array must be of the same type, called the base type of the array All of the indexed variables in a single array must be of the same type, called the base type of the array 6-5

Example: Using the score Array in a Program The for loop is ideally suited for performing array manipulations: The for loop is ideally suited for performing array manipulations: for (index = 0; index < 5; index++) System.out.println(score[index] + System.out.println(score[index] + " differs from max by " + " differs from max by " + (max-score[index]) ); (max-score[index]) ); 6-6

The length Instance Variable An array is considered to be an object An array is considered to be an object Since other objects can have instance variables (attributes), so can arrays Since other objects can have instance variables (attributes), so can arrays Every array has one instance variable named length Every array has one instance variable named length When an array is created, the instance variable length is automatically set equal to its size When an array is created, the instance variable length is automatically set equal to its size The value of length cannot be changed (other than by creating an entirely new array with new ) The value of length cannot be changed (other than by creating an entirely new array with new ) double[] score = new double[5]; Given score above, score.length has a value of 5 Given score above, score.length has a value of ArrayOperations4.java (MS-Word file) ArrayOperations4.java (MS-Word file) ArrayOperations4.javaMS-Word file ArrayOperations4.javaMS-Word file

Initializing Arrays An array can be initialized when it is declared An array can be initialized when it is declared Values for the indexed variables are enclosed in braces, and separated by commas Values for the indexed variables are enclosed in braces, and separated by commas The array size is automatically set to the number of values in the braces The array size is automatically set to the number of values in the braces int[] age = {2, 12, 1}; Given age above, age.length has a value of 3 Given age above, age.length has a value of 3 6-8

Initializing Arrays Another way of initializing an array is by using a for loop Another way of initializing an array is by using a for loop double[] reading = new double[100]; int index; for (index = 0; index < reading.length; index++) index < reading.length; index++) reading[index] = 42.0; reading[index] = 42.0; If the elements of an array are not initialized explicitly, they will automatically be initialized to the default value for their base type If the elements of an array are not initialized explicitly, they will automatically be initialized to the default value for their base type 6-9

Copying Arrays 6-10 ArrayOperations5.java (MS-Word file) ArrayOperations5.java (MS-Word file) ArrayOperations5.javaMS-Word file ArrayOperations5.javaMS-Word file ArrayOperations6.java (MS-Word file) ArrayOperations6.java (MS-Word file) ArrayOperations6.javaMS-Word file ArrayOperations6.javaMS-Word file Arrays are object. Hence, “=“ cannot be used to copy one array to another For example, arr2 = arr1; would result in the following Arrays are object. Hence, “=“ cannot be used to copy one array to another For example, arr2 = arr1; would result in the following Before arr1arr2 After arr1arr2

Pitfall: Array Index Out of Bounds Array indices always start with 0, and always end with the integer that is one less than the size of the array Array indices always start with 0, and always end with the integer that is one less than the size of the array The most common programming error made when using arrays is attempting to use a nonexistent array index The most common programming error made when using arrays is attempting to use a nonexistent array index When an index expression evaluates to some value other than those allowed by the array declaration, the index is said to be out of bounds When an index expression evaluates to some value other than those allowed by the array declaration, the index is said to be out of bounds An out of bounds index will cause a program to terminate with a run-time error message An out of bounds index will cause a program to terminate with a run-time error message Array indices get out of bounds most commonly at the first or last iteration of a loop that processes the array: Be sure to test for this! Array indices get out of bounds most commonly at the first or last iteration of a loop that processes the array: Be sure to test for this! 6-11

Pitfall: Array Index Out of Bounds 6-12 ArrayOperations7.java (MS-Word file) ArrayOperations7.java (MS-Word file) ArrayOperations7.javaMS-Word file ArrayOperations7.javaMS-Word file ArrayOperations8.java (MS-Word file) ArrayOperations8.java (MS-Word file) ArrayOperations8.javaMS-Word file ArrayOperations8.javaMS-Word file