Lecture 5: Arrays A way to organize data MIT AITI April 9th, 2005.

Slides:



Advertisements
Similar presentations
Introduction to C Programming
Advertisements

Arrays.
Outline IS400: Development of Business Applications on the Internet Fall 2004 Instructor: Dr. Boris Jukic JavaScript: Arrays.
Arrays, A1 COMP 401, Fall 2014 Lecture 4 8/28/2014.
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.
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.
Arrays  Writing a program that uses a large amount of information.  Such as a list of 100 elements.  It is not practical to declare.
Introduction to Computers and Programming Lecture 15: Arrays Professor: Evan Korth New York University.
©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.
Arrays CS Feb Announcements Exam 1 Grades on Blackboard Project 2 scores: end of Class Project 4, due date:20 th Feb –Snakes & Ladders Game.
© The McGraw-Hill Companies, 2006 Chapter 5 Arrays.
Engineering H192 - Computer Programming The Ohio State University Gateway Engineering Education Coalition Lect 11P. 1Winter Quarter Arrays Lecture 11.
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”
Lecture 5 Arrays A way to organize data © MIT AITI 2003.
Multi-Dimensional Arrays in Java "If debugging is the process of removing software bugs, then programming must be the process of putting them in." -- Edsger.
Java Unit 9: Arrays Declaring and Processing Arrays.
© 2011 Pearson Education, publishing as Addison-Wesley 1 Arrays  Arrays are objects that help us organize large amounts of information  Chapter 6 focuses.
03/16/ What is an Array?... An array is an object that stores list of items. Each slot of an array holds an individual element. Characteristics.
Lecture 12 Instructor: Craig Duckett ARRAYS. Announcements Assignment 3 Assignment 3 Revision Assignment 4 (and Final Exam) GRADED! RETURNED! Woot! NEXT.
Introduction to Arrays in Java Corresponds with Chapter 6 of textbook.
Arrays Part 9 dbg. Arrays An array is a fixed number of contiguous memory locations, all containing data of the same type, identified by one variable.
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.
Programming with Java © 2002 The McGraw-Hill Companies, Inc. All rights reserved. 1 McGraw-Hill/Irwin Chapter 9 Arrays.
JAVA: An Introduction to Problem Solving & Programming, 5 th Ed. By Walter Savitch and Frank Carrano. ISBN © 2009 Pearson Education, Inc., Upper.
Arrays Module 6. Objectives Nature and purpose of an array Using arrays in Java programs Methods with array parameter Methods that return an array Array.
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.
1 © 2002, Cisco Systems, Inc. All rights reserved. Arrays Chapter 7.
Two dimensional arrays in Java Computer Science 3 Gerb Objective: Use matrices in Java.
CHAPTER: 12. Array is a collection of variables of the same data type that are referenced by a common name. An Array of 10 Elements of type double.
UNIT II. -set of homogeneous data items. Eg: int arrays can hold only integers and char arrays can only hold characters. Arrays have a type, name, and.
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.
Computer Programming 12 Mr. Jean April 24, The plan: Video clip of the day Upcoming Quiz Sample arrays Using arrays More about arrays.
Arrays  Array is a collection of same type elements under the same variable identifier referenced by index number.  Arrays are widely used within programming.
UniMAP Sem2-10/11 DKT121: Fundamental of Computer Programming1 Arrays.
Chapter 8 Arrays. A First Book of ANSI C, Fourth Edition2 Introduction Atomic variable: variable whose value cannot be further subdivided into a built-in.
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.
Lec 13 Oct 21, 02. Array Initialization in the declaration statement ► int temp[5] = {98, 87, 92, 79,85}; ► char codes[6] = { ‘s’, ’a’, ‘m’, ‘p’, ‘l’,
BIT115: Introduction to Programming
Arrays.
1 Objects for Organizing Data -- Introduction zAs our programs get more sophisticated, we need assistance organizing large amounts of data zChapter 6 focuses.
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];
REEM ALMOTIRI Information Technology Department Majmaah University.
Arrays Declaring arrays Passing arrays to functions Searching arrays with linear search Sorting arrays with insertion sort Multidimensional arrays Programming.
CSI 3125, Preliminaries, page 1 Arrays. CSI 3125, Preliminaries, page 2 Arrays Group of related typed variables that referred to a common name Each data.
For Friday Read No quiz Program 6 due. Program 6 Any questions?
© 2004 Pearson Addison-Wesley. All rights reserved7-1 Array review Array of primitives int [] count; count = new int[10]; Array of objects Grade [] cs239;
Arrays. Arrays are objects that help us organize large amounts of information.
Lecture #15 ARRAYS By Shahid Naseem (Lecturer). 2 ARRAYS DEFINITION An array is a sequence of objects of same data type. The objects in an array are also.
VISUAL C++ PROGRAMMING: CONCEPTS AND PROJECTS Chapter 7A Arrays (Concepts)
Chapter 9 Introduction to Arrays Fundamentals of Java.
KUKUM-06/07 EKT120: Computer Programming 1 Week 6 Arrays-Part 1.
Lecture 5 Arrays A way to organize data. What are Arrays? An array is a named list of data values that all have the same type An array is a collection.
ARRAYS (Extra slides) Arrays are objects that help us organize large amounts of information.
Chapter VII: Arrays.
Array in C# Array in C# RIHS Arshad Khan
Two-Dimensional Arrays
Array, Strings and Vectors
A simple way to organize data
Cell Arrays Definition Creating Cell Arrays Referencing Cell 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.
EKT150 : Computer Programming
MSIS 655 Advanced Business Applications Programming
Visual Programming COMP-315
Arrays.
Presentation transcript:

Lecture 5: Arrays A way to organize data MIT AITI April 9th, 2005

© 2005 MIT-Africa Internet Technology Initiative What are Arrays? An array is a series of compartments to store data. Essentially a block of variables. In Java, arrays can only hold one type. For example, int arrays can hold only integers and char arrays can only hold characters.

© 2005 MIT-Africa Internet Technology Initiative Arrays have a type, name, and size. Array of three integers named prices : – prices : Array of four Strings named people : – people : (Indices) We refer to each item in an array as an element. The position of each element is known as its index. Array Visualization and Terms int String

© 2005 MIT-Africa Internet Technology Initiative Declaring an Array Array declarations similar to variables, but use square brackets: – datatype[] name; For example: – int[] prices; – String[] people; Can alternatively use the form: – datatype name[]; – int prices[];

© 2005 MIT-Africa Internet Technology Initiative Allocating an Array Unlike variables, we need to allocate memory to store arrays. (malloc() in C.) Use the new keyword to allocate memory: – name = new type[size]; – prices = new int[3]; – people = new String[4]; This allocates an integer array of size 20 and a String array of size 10. Can combine declaration and allocation: – int[] prices = new int[3];

© 2005 MIT-Africa Internet Technology Initiative Array Indices Every element in an array is referenced by its index. In Java, the index starts at 0 and ends at n-1, where n is the size of the array. If the array prices has size 3, its valid indices are 0, 1, and 2. Beware “Array out of Bounds” errors.

© 2005 MIT-Africa Internet Technology Initiative Using an Array We access an element of an array using square brackets [] : – name[index] Treat array elements just like a variable. Example assigning values to each element of prices : – prices[0] = 6; – prices[1] = 80; – prices[2] = 10;

© 2005 MIT-Africa Internet Technology Initiative Using an Array We assign values to elements of String arrays in a similar fashion: – String[] people; – people = new String[4]; – people[0] = ”Alice”; – people[1] = ”Bilha”; – people[2] = ”Chris”; – people[3] = ”David”;

© 2005 MIT-Africa Internet Technology Initiative Initializing Arrays You can also specify all of the items in an array at its creation. Use curly brackets to surround the array’s data and separate the values with commas: – String[] people = {“Alice”, “Bilha”, “Chris”, “David”}; – int[] prices = {6, 80, 10}; All the items must be of the same type. Note: Curly brackets are overloaded because they also designate blocks of code.

© 2005 MIT-Africa Internet Technology Initiative Vocabulary Review Allocate - Create empty space that will contain the array. Initialize - Fill in a newly allocated array with initial values. Element - An item in the array. Index - Element’s position in the array. Size or Length - Number of elements.

© 2005 MIT-Africa Internet Technology Initiative Pop Quiz Which of the following sequences of statements does not create a new array? a) int[] arr = new int[4]; b) int[] arr; arr = new int[4]; c) int[] arr = { 1, 2, 3, 4}; d) int[] arr; 

© 2005 MIT-Africa Internet Technology Initiative Lengths of Array Each array has a default field called length Access an array’s length using the format: – arrayName.length ; Example: – String[] people = {“Alice”, “Bilha”, “Chris”, “David”}; – int numPeople = people.length; The value of numPeople is now 4. Arrays are always of the same size. Their lengths cannot be changed once they are created.

© 2005 MIT-Africa Internet Technology Initiative Example 1 Sample Code: String[] people = {“Alice”, “Bilha”, “Chris”, “David”}; for(int i=0; i<names.length; i++) System.out.println(names[i]+”!"); Output: Alice! Bilha! Chris! David!

© 2005 MIT-Africa Internet Technology Initiative Pop Quiz 2 Given this code fragment: – int[] data = new int[10]; – System.out.println(data[j]); Which are legal values of j ? a) -1 b) 0 c) 3.5 d) 10 

© 2005 MIT-Africa Internet Technology Initiative Pop Quiz 3 Decide what type and size of array (if any) to store each data set: – Score in each quarter of a football game. – Your name, date of birth, and height. – Hourly temperature readings for a week. – Your daily expenses for a year. int[] quarterScore = new int[4]; Not appropriate. Different types. double[] tempReadings = new double[168]; float[] dailyExpenses = new float[365];

© 2005 MIT-Africa Internet Technology Initiative Exercise 2 What are the contents of c after the following code segment? int [] a = {1, 2, 3, 4, 5}; int [] b = {11, 12, 13}; int [] c = new int[4]; for (int j = 0; j < 3; j++) { c[j] = a[j] + b[j]; }

© 2005 MIT-Africa Internet Technology Initiative value at row index 2, column index 0 is 3 2-Dimensional Arrays The arrays we've used so far can be thought of as a single row of values. A 2-dimensional array can be thought of as a grid (or matrix) of values. Each element of the 2-D array is accessed by providing two indices: a row index and a column index. A 2-D array is actually just an array of arrays

© 2005 MIT-Africa Internet Technology Initiative 2-D Array Example Example: A landscape grid of a 20 x 55 acre piece of land. We want to store the height of the land at each row and each column of the grid. We declare a 2-D array two sets of square brackets: – double[][] heights; – heights = new double[20][55]; This 2-D array has 20 rows and 55 columns To access the acre at row index 11 and column index 23: heights[11][23]

© 2005 MIT-Africa Internet Technology Initiative More on Dimensionality Can have unequal sized sub-arrays: int[][] a = new int[3][]; int[] b = {1,2,3}; int[] c = {4,5,6,7}; int[] d = {8}; a[0]= b; a[1] = c; a[2] = d; Can have higher dimensions: int[][][][] a; // 4-D Array