A simple way to organize data

Slides:



Advertisements
Similar presentations
Introduction to C Programming
Advertisements

Outline IS400: Development of Business Applications on the Internet Fall 2004 Instructor: Dr. Boris Jukic JavaScript: 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.
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.
Chapter 9 Introduction to Arrays
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.
© 2011 Pearson Education, publishing as Addison-Wesley 1 Arrays  Arrays are objects that help us organize large amounts of information  Chapter 6 focuses.
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.
Programming with Java © 2002 The McGraw-Hill Companies, Inc. All rights reserved. 1 McGraw-Hill/Irwin Chapter 9 Arrays.
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.
Lecture 5: Arrays A way to organize data MIT AITI April 9th, 2005.
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.
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.
Java Software Solutions Lewis and Loftus Chapter 6 1 Copyright 1997 by John Lewis and William Loftus. All rights reserved. Objects for Organizing Data.
Arrays.
1 Objects for Organizing Data -- Introduction zAs our programs get more sophisticated, we need assistance organizing large amounts of data zChapter 6 focuses.
REEM ALMOTIRI Information Technology Department Majmaah University.
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.
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.
Unit 2 Technology Systems
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.
Array in C# Array in C# RIHS Arshad Khan
Two-Dimensional Arrays
Computer Programming BCT 1113
Array, Strings and Vectors
EKT120 : Computer Programming
Cell Arrays Definition Creating Cell Arrays Referencing Cell Arrays
Lecture 7 – Arrays (1) PGT 106 : C PROGRAMMING.
JavaScript: Functions.
Java Arrays. Array Object An array :a container object holds a fixed number of values of a single type. The length of an array is established when the.
Two-Dimensional Arrays
Arrays, For loop While loop Do while loop
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
Arrays November 8, 2017.
Coding Concepts (Data Structures)
BIT115: Introduction to Programming
EKT120: Computer Programming
Arrays Chapter 8 Copyright © 2008 W. W. Norton & Company.
MSIS 655 Advanced Business Applications Programming
Multidimensional array
Arrays Week 2.
Arrays An array is a grouping of elements of the same type that share a common base name Can have any number of elements in the array Individual elements.
Arrays ICS2O.
CIS16 Application Development and Programming using Visual Basic.net
Arrays in Java.
Java SE 7 One and Multi Dimensional Arrays Module 6
Arrays An array is a grouping of elements of the same type that share a common base name Can have any number of elements in the array Individual elements.
Visual Programming COMP-315
Comparing Python and Java
Arrays of Objects October 8, 2007 ComS 207: Programming I (in Java)
Arrays.
Presentation transcript:

A simple way to organize data Lecture 4: Arrays A simple way to organize data

What are Arrays? An array is a series of compartments to store data. Essentially a block of variables. In Most Programming Languages, arrays can only hold one type. For example, int arrays can hold only integers and char arrays can only hold characters.

Array Visualization and Terms Arrays have a type, name, and size. Array of three integers named prices : prices : Array of four Strings named people: people : (Indices) 0 1 2 3 We refer to each item in an array as an element. The position of each element is known as its index. int String

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[];

Allocating an Array Unlike variables, we need to allocate memory to store arrays. (malloc() in C.) Use the new keyword to allocate memory in java, C# and others: name = new type[size]; prices = new int[3]; people = new String[5]; This allocates an integer array of size 3 and a String array of size 5. Can combine declaration and allocation: int[] prices = new int[3];

Array Operations Creating Arrays (Declaration) Accessing elements Adding elements Removing elements Destroying arrays

Array Indices (Accessing Elements) Every element in an array is referenced by its index. In Most programming languages, 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.

Assigning an Array (Adding Elements) 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;

Assigning an Array (Adding Elements) We assign values to elements of String arrays in a similar fashion: String[] people; people = new String[5]; people[0] = ”Gleb”; people[1] = ”Lawrence”; people[2] = ”Michael”; people[3] = ”Stephanie”; people[4] = ”Zawadi”;

Initializing Arrays (Adding Elements) 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 = {“Gleb”, “Lawrence”, “Michael”, “Stephanie”, “Zawadi”}; 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.

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.

Lengths of Array Each array has a default field called length Access an array’s length using the format: arrayName.length; Example: String[] people = {“Gleb”, “Lawrence”, “Michael”, “Stephanie”, “Zawadi”}; int numPeople = people.length; The value of numPeople is now 5. Arrays are always of the same size. Their lengths cannot be changed once they are created.

Example 1 Sample Code: String[] people = {“Gleb”, “Lawrence”, “Michael”, “Stephanie”, “Zawadi”}; for(int i=0; i<names.length; i++) System.out.println(names[i]+”!"); Output: Gleb! Lawrence! Michael! Stephanie! Zawadi!

Review 2 Given this code fragment: Which are legal values of j? int[] data = new int[10]; System.out.println(data[j]); Which are legal values of j? -1 3.5 10

Review 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];

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]; }

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 6 3 2 7 9 1 4 8 value at row index 2, column index 0 is 3

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]