CS 139-Programming Fundamentals Lecture 11B - Arrays Adapted from a presentation by Dr. Rahman Fall 2014.

Slides:



Advertisements
Similar presentations
Chapter 21 Implementing lists: array implementation.
Advertisements

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.
Introduction to Arrays Chapter What is an array? An array is an ordered collection that stores many elements of the same type within one variable.
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.
Chapter 10.
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.
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.
General Computer Science for Engineers CISC 106 Lecture 19 Dr. John Cavazos Computer and Information Sciences 04/06/2009.
Arrays  Writing a program that uses a large amount of information.  Such as a list of 100 elements.  It is not practical to declare.
General Computer Science for Engineers CISC 106 Lecture 20 Dr. John Cavazos Computer and Information Sciences 04/08/2009.
Introduction to Computers and Programming Lecture 15: Arrays Professor: Evan Korth New York University.
Chapter 7 Arrays. © 2004 Pearson Addison-Wesley. All rights reserved7-2 Arrays Arrays are objects that help us organize large amounts of information Chapter.
©2004 Brooks/Cole Chapter 8 Arrays. Figures ©2004 Brooks/Cole CS 119: Intro to JavaFall 2005 Sometimes we have lists of data values that all need to be.
Loops – While, Do, For Repetition Statements Introduction to Arrays
© The McGraw-Hill Companies, 2006 Chapter 5 Arrays.
ECE122 L11: For loops and Arrays March 8, 2007 ECE 122 Engineering Problem Solving with Java Lecture 11 For Loops and Arrays.
1 Arrays  Arrays are objects that help us organize large amounts of information  Chapter 8 focuses on: array declaration and use passing arrays and array.
Aalborg Media Lab 28-Jun-15 Software Design Lecture 8 “Arrays”
Arrays in Java Selim Aksoy Bilkent University Department of Computer Engineering
Slides prepared by Rose Williams, Binghamton University Chapter 6 Arrays.
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.
Sanjay Goel, School of Business, University at Albany, SUNY 1 MSI 692: Special Topics in Information Technology Lecture 4: Strings & Arrays Sanjay Goel.
© 2011 Pearson Education, publishing as Addison-Wesley 1 Arrays  Arrays are objects that help us organize large amounts of information  Chapter 6 focuses.
Instructor: Alexander Stoytchev CprE 185: Intro to Problem Solving (using C)
1 Dr. Seuss again: "Too Many Daves"  Did I ever tell you that Mrs. McCave Had twenty-three sons, and she named them all Dave?  Well, she did. And that.
What is an Array? An array is a collection of variables. Arrays have three important properties: –group of related items(for example, temperature for.
ARRAYS Computer Engineering Department Java Course Asst. Prof. Dr. Ahmet Sayar Kocaeli University - Fall
ECE 264 Object-Oriented Software Development Instructor: Dr. Honggang Wang Fall 2012 Lecture 26: Exam 2 Preview.
CompSci Arrays  Aggregate data type  Deal with items of same type  Lists of words  Numbers  Analogies  Mailboxes in post office  CD racks.
CSE 501N Fall ‘09 08: Arrays 22 September 2009 Nicholas Leidenfrost.
ArrayList Class An ArrayList is an object that contains a sequence of elements that are ordered by position. An ArrayList is an object that contains a.
Structuring Data: Arrays ANSI-C. Representing multiple homogenous data Problem: Input: Desired output:
UniMAP Sem2-10/11 DKT121: Fundamental of Computer Programming1 Arrays.
Pointers in C++. Topics Covered  Introduction to Pointers  Pointers and arrays  Character Pointers, Arrays and Strings  Examples.
Java Software Solutions Lewis and Loftus Chapter 6 1 Copyright 1997 by John Lewis and William Loftus. All rights reserved. Objects for Organizing Data.
11 PART 2 ARRAYS. 22 PROCESSING ARRAY ELEMENTS Reassigning Array Reference Variables The third statement in the segment below copies the address stored.
Arrays. Topics to be Covered... Arrays ◦ Declaration ◦ Assigning values ◦ Array manipulation using loops Multi-dimensional arrays ◦ 2D arrays ◦ Declaration.
Arrays Chapter 12. Overview Arrays and their properties Creating arrays Accessing array elements Modifying array elements Loops and arrays.
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.
Introducing Arrays. Too Many Variables?  Remember, a variable is a data structure that can hold a single value at any given time.  What if I want to.
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];
CSC 1051 – Data Structures and Algorithms I Dr. Mary-Angela Papalaskari Department of Computing Sciences Villanova University Course website:
Instructor: Alexander Stoytchev CprE 185: Intro to Problem Solving (using C)
Liang, Introduction to Java Programming, Fifth Edition, (c) 2005 Pearson Education, Inc. All rights reserved Chapter 6 Arrays.
Chapter 5: Arrays in Java. The objectives of this chapter are:  1. To discuss the creation and use of Arrays.   2. To continue to use the String class.
1 CSC103: Introduction to Computer and Programming Lecture No 17.
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.
Copyright © 2013 Pearson Education, Inc. Publishing as Pearson Addison-Wesley Starting Out with Programming Logic & Design Third Edition by Tony Gaddis.
Chapter 9 Introduction to Arrays Fundamentals of Java.
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.
1 Principles of Computer Science I Honors Section Note Set 3 CSE 1341.
Data Structures & Algorithms CHAPTER 2 Arrays Ms. Manal Al-Asmari.
Data Storage So far variables have been able to store only one value at a time. What do you do if you have many similar values that all need to be stored?
© 2004 Pearson Addison-Wesley. All rights reserved October 5, 2007 Arrays ComS 207: Programming I (in Java) Iowa State University, FALL 2007 Instructor:
Chapter 8 Arrays and the ArrayList Class Introduction to Arrays.
Lecture 5 array declaration and instantiation array reference
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
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.
CISC181 Introduction to Computer Science Dr
Single-Dimensional Arrays chapter6
Dr. Sampath Jayarathna Cal Poly Pomona
Arrays in Java.
Suggested self-checks: Section 7.11 #1-11
Arrays of Objects October 8, 2007 ComS 207: Programming I (in Java)
Presentation transcript:

CS 139-Programming Fundamentals Lecture 11B - Arrays Adapted from a presentation by Dr. Rahman Fall 2014

Arrays

Declaring and Creating Arrays Variables can hold one single value Arrays can hold multiple values of the same data type. Syntax: int[] num; num = new int [5]; Results: – 5 integer locations are allocated – They are initialized to 0 n=5 num Index/subscript

Declaring Arrays Some examples of array declarations: double[] prices; prices = new double[500]; boolean[] flags; flags = new boolean[20]; char[] codes; char = new char[1750];

Initializer Lists int[] num; num = new int [5]; Initializing an array: num[0] = 50; num[1] = 100; num[2] = 120; num[3] = 150; num[4] = 200; num

Using initializer to initialize An initializer list can be used to instantiate and initialize an array in one step: Examples: int[] num; num = new int []{50, 100, 120, 150, 200}; char[] grade; char = new char[] {'A', 'B', 'C‘}; num grade A B C

Using loop to initialize array with values int[] num; num = new int [5]; for (int i = 0; i <= num.length - 1; i++) { num[i]=keyboard.nextInt(); } num i = 0 i = 1 i = 2 i = 3 i = i = 5 error array.length = how many elements is the array The following reference will cause an exception to be thrown: System.out.println (num[5]);

Bounds Checking For example, if the array num can hold 100 values, it can be indexed using only the numbers 0 to 99 It’s common to introduce off-by-one errors when using arrays for (int index=0; index <= 100; index++) num[index] = index + 10; problem Solution = always use index < length or index <= (length-1)

Processing Array Elements Displaying array elements To display elements of the array referenced by values, we could write: int count; int[ ] values; values = {13, 21, 201, 3, 43}; for (count = 0; count < values.length; count++) { System.out.println(“Value ” + (count+1) + " in the list of values is “ + values[count]); }

int[ ] oldValues; oldValues = {10, 100, 200, 300}; int[ ] newValues; newValues = new int[4]; newValues = oldValues; // Copies the address in oldValues into newValues Processing Array Elements reassigning array reference variables oldValues newValues [0][1][2][3] address [0][1][2][3] [0][1][2][3] address oldValues address newValues

This assignment operation does not copy the content of oldValues array to the newValues array To copy the contents of one array to another you must copy the individual array elements. Processing Array Elements (Cont.) reassigning array reference variables

To copy the contents of oldValues to newValues we could write: int count; int[ ] oldValues; oldValues = {10, 100, 200, 300}; int[ ] newValues; newValues = new int[4]; // If newValues is large enough to hold the values in oldValues if (newValues.length >= oldValues.length) { for (count = 0; count < oldValues.length; count++) { newValues[count] = oldValues[count]; } Processing Array Elements reassigning array reference variables

Some Useful Array Operations comparing arrays The two arrays are not stored in the same memory location, so their equality testing returns false char[ ] array1; array1 = {'A', 'B', 'C', 'D', 'A'}; char[ ] array2; array2 = {'A', 'B', 'C', 'D', 'A'}; boolean equal = false; if (array1 == array2) // This is false - the addresses are not equal { equal = true; } To compare the contents of two arrays, you must compare the individual elements of the arrays.