Lecture 5 Arrays A way to organize data © MIT AITI 2003.

Slides:



Advertisements
Similar presentations
Arrays.
Advertisements

Single Variable and a Lot of Variables The declaration int k; float f; reserve one single integer variable called k and one single floating point variable.
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.
Arrays Chapter 6. Outline Array Basics Arrays in Classes and Methods Sorting Arrays Multidimensional Arrays.
Introduction to arrays Data in economy size packages.
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.
Programming with Collections Collections in Java Using Arrays Week 9.
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.
© 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.
1 Chapter 2 Introductory Programs. 2 Getting started To create and run a Java program –Create a text file with a.java extension for the source code. For.
PHYS 2020 Making Choices; Arrays. Arrays  An array is very much like a matrix.  In the C language, an array is a collection of variables, all of the.
1 CSCE 1030 Computer Science 1 Arrays Chapter 7 in Small Java.
Java Unit 9: Arrays Declaring and Processing Arrays.
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.
© The McGraw-Hill Companies, 2006 Chapter 4 Implementing methods.
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.
 2005 Pearson Education, Inc. All rights reserved. 1 Arrays.
 Pearson Education, Inc. All rights reserved 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.
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.
Lecture 5: Arrays A way to organize data MIT AITI April 9th, 2005.
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.
Arrays Array – Group of contiguous memory locations Each memory location has same name Each memory location has same type.
BUILDING JAVA PROGRAMS CHAPTER 7 Arrays. Exam #2: Chapters 1-6 Thursday Dec. 4th.
Review of ICS 102. Lecture Objectives To review the major topics covered in ICS 102 course Refresh the memory and get ready for the new adventure of ICS.
1 Mr. Muhammad Hanif Lecturer Information Technology MBBS Campus Dadu University of SIndh.
Fundamental Programming: Fundamental Programming Introduction to C++
1 Building Java Programs Chapter 7: Arrays These lecture notes are copyright (C) Marty Stepp and Stuart Reges, They may not be rehosted, sold, or.
19&20-2 Know how to declare pointer variables. Understand the & (address) and *(indirection) operators. Dynamic Memory Allocation Related Chapter: ABC.
Python Primer 1: Types and Operators © 2013 Goodrich, Tamassia, Goldwasser1Python Primer.
C Programming – Part 3 Arrays and Strings.  Collection of variables of the same type  Individual array elements are identified by an integer index 
Data Structure and c K.S.Prabhu Lecturer All Deaf Educational Technology.
Array Objectives To understand the concept of arrays To understand the purpose to which we use arrays. To be able to declare references to arrays. To be.
UniMAP Sem2-10/11 DKT121: Fundamental of Computer Programming1 Arrays.
Arrays-. An array is a way to hold more than one value at a time. It's like a list of items.
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’,
Arrays.
BIT115: Introduction to Programming
Java – An Object Oriented Language CS 307 Lecture Notes Lecture Weeks 5-6 Khalid Siddiqui.
Module 1: Array ITEI222 - Advance Programming Language.
Arrays Declaring arrays Passing arrays to functions Searching arrays with linear search Sorting arrays with insertion sort Multidimensional arrays Programming.
Arrays What is an array… –A data structure that holds a set of homogenous elements (of the same type) –Associate a set of numbers with a single variable.
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.
SEQUENTIAL AND OBJECT ORIENTED PROGRAMMING Arrays.
For Friday Read No quiz Program 6 due. Program 6 Any questions?
 2005 Pearson Education, Inc. All rights reserved Arrays.
Chapter 9 Introduction to Arrays Fundamentals of Java.
Introduction to programming in java Lecture 21 Arrays – Part 1.
Arrays Collections of data Winter 2004CS-1010 Dr. Mark L. Hornick 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.
Array in C# Array in C# RIHS Arshad Khan
Array, Strings and Vectors
A simple way to organize data
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.
Object Oriented Programming in java
MSIS 655 Advanced Business Applications Programming
Arrays in Java.
Presentation transcript:

Lecture 5 Arrays A way to organize data © MIT AITI 2003

What are Arrays Conceptually, Arrays are a series of compartments to hold data with each compartment sized appropriately for whatever data type the array as a whole is intended to hold. An array can holds only one type of data! E.g. only int, char, String, etc.

Declaring an Array Array declarations use square brackets. datatype[] label For example: int[] prices String[] names

Creating a new Array Here is the syntax: new int[20] The new keyword creates an array of the type int that has 20 compartments Combined with assignment: int[] prices = new int[20] When created as above, the array has the default “zero” in each of the compartments i.e. The items in the array are initialized to the zero-value for the particular type; { int:0, float:0.0, String: null }

Conceptual Overview of an Array

Accessing Arrays Every compartment in an array is assigned an integer value starting at zero. This number is called the index of an item In Java and most other languages, the index starts from 0 and ends at n-1, where n is the size of the array

Accessing Arrays cont… To access an item in an array, type the name of the array followed by the item’s index enclosed in square brackets. For example, the expression: names[0] may return the String: “Robert”

Example 1 String[] names = {“Conor”, “Dan”, “Evita”, “Sonia”, “Robert”, “Jehanzeb” }; }; for (int i = 0; i < names.length; i++) { System.out.println(“Hello “ + names[i] + “.“); } Which prints: Hello Conor. Hello Dan. Hello Evita. Hello Sonia. Hello Robert. Hello Jehanzeb.

Modifying Array Elements First, get the particular item as shown in the previous slide. Then, set that expression equal to (assignment) the new value. names[0] = “Sonia” Now this item has been changed from one name to another. So the expression, names[0] returns “Sonia”. Important: The size of an array cannot be changed after it is created as items in the array can only be changed but not added.

Example 2 int[] fibs = new int[10]; fibs[0] = 1; fibs[1] = 1; for(int i=2; i<fibs.length; i++) { fibs[i] = fibs[i-2]+fibs[i-1]; } After running this code, the array fibs contains the first ten Fibonacci numbers.

Alternative Way to Construct Arrays Another way to make an array is to specify all of the items at it’s creation. To do this, use curly brackets to surround the array and separate items with commas. Here’s an example of creating an array of names: String[] names = { “Conor”, “Dan”, “Evita”, “Sonia”, “Robert”, “Jehanzeb”}; This creates an array with six compartments containing the specified items. Note that all the items must be of the same type. Here they are all strings.

Alternative Way to Construct Arrays  Yet another way of creating our Array: String[] names = new String[6]; names[0] = “Conor”; names[1] = “Dan”; names[2] = “Evita”; names[3] = “Sonia”; names[4] = “Robert”; names[5] = “Jehanzeb”;

2-Dimensional Arrays There are instances where it is useful to work with arrays that relate two indices to a single value. e.g. The temperature (value) on each date (index2) at several locations (index 1) in Ghana. This is accomplished with 2-Dimensional Arrays; An Array of an Arrays. In Java this is done using two sets of square brackets. For example: double[][] temps = new double[10][365]; Here there are 10 location arrays with 365 compartments each (1 for each day).

Accessing 2-D Array Elements To access all the temperatures from one location use temps[index1]. The above expression returns an array of doubles i.e. double[] To get the temperature on date (index 2) at a particular location use temps[index1][index2] The above expression returns a double