Arrays.

Slides:



Advertisements
Similar presentations
UNIT IV.
Advertisements

CS0007: Introduction to Computer Programming Arrays: Higher Dimensional Arrays.
Arrays.
Outline IS400: Development of Business Applications on the Internet Fall 2004 Instructor: Dr. Boris Jukic JavaScript: Arrays.
Arrays. Topics Tables of Data Arrays – Single Dimensional Parsing a String into Multiple Tokens Arrays - Multi-dimensional.
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.
©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.
Engineering H192 - Computer Programming The Ohio State University Gateway Engineering Education Coalition Lect 11P. 1Winter Quarter Arrays Lecture 11.
Chapter 8 Arrays and Strings
Building Java Programs Chapter 7.5
1 CSCE 1030 Computer Science 1 Arrays Chapter 7 in Small Java.
Chapter 9 Introduction to Arrays
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.
COP2800 – Computer Programming Using JAVA University of Florida Department of CISE Spring 2013 Lecture 13 – Having Fun with Arrays in Java Webpage:
Chapter 8 Arrays and Strings
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.
 2005 Pearson Education, Inc. All rights reserved. 1 Arrays.
COMP102 Lab 081 COMP 102 Programming Fundamentals I Presented by : Timture Choi.
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.
Pemrograman Dasar Arrays PTIIK - UB. Arrays  An array is a container object that holds a fixed number of values of a single type.  The length of an.
1 © 2002, Cisco Systems, Inc. All rights reserved. Arrays Chapter 7.
Lecture 5: Arrays A way to organize data MIT AITI April 9th, 2005.
Two dimensional arrays in Java Computer Science 3 Gerb Objective: Use matrices in Java.
Java Script: Arrays (Chapter 11 in [2]). 2 Outline Introduction Introduction Arrays Arrays Declaring and Allocating Arrays Declaring and Allocating Arrays.
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.
Course Title Object Oriented Programming with C++ Course instructor ADEEL ANJUM Chapter No: 05 ARRAY 1 BY ADEEL ANJUM (MSc-cs, CCNA,WEB DEVELOPER) 1.
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.
Topic 26 Two Dimensional Arrays "Computer Science is a science of abstraction -creating the right model for a problem and devising the appropriate mechanizable.
© 2004 Pearson Addison-Wesley. All rights reserved October 13, D Arrays ComS 207: Programming I (in Java) Iowa State University, FALL 2006 Instructor:
Computer Programming 12 Mr. Jean April 24, The plan: Video clip of the day Upcoming Quiz Sample arrays Using arrays More about arrays.
Two-Dimensional Arrays That’s 2-D Arrays Girls & Boys! One-Dimensional Arrays on Steroids!
Computer Programming Lecture 8 Arrays. 2 switch-statement Example (3) If use press left arrowIf use press right arrow If use press up arrow If use press.
Arrays. Collections We would like to be able to keep lots of information at once Example: Keep all the students in the class Grade each one without writing.
Structuring Data: Arrays ANSI-C. Representing multiple homogenous data Problem: Input: Desired output:
Operator precedence.  Evaluate a + b * c –multiplication first? a + (b * c) –addition first? ( a + b) * c  Java solves this problem by assigning priorities.
How do you do the following? Find the number of scores within 3 points of the average of 10 scores? What kind of a tool do you need? Today’s notes: Include.
Arrays. Array: Sequence of values of the same type Construct array: Store in variable of type double[ ] new double[10] double[] data = new double[10];
Engineering H192 - Computer Programming The Ohio State University Gateway Engineering Education Coalition Lect 11P. 1Winter Quarter Arrays Lecture 11.
Arrays.
1 Arrays of Arrays Quick review … arrays Arrays of arrays ≡ multidimensional array Example: times table Representation in memory Ragged arrays Example:
Java – An Object Oriented Language CS 307 Lecture Notes Lecture Weeks 5-6 Khalid Siddiqui.
Multidimensional Arrays Computer and Programming.
Week 6 - Friday.  What did we talk about last time?  Loop examples.
Multidimensional Arrays tMyn1 Multidimensional Arrays It is possible to declare arrays that require two or more separate index values to access an element.
Visual C# 2005 Using Arrays. Visual C# Objectives Declare an array and assign values to array elements Initialize an array Use subscripts to access.
Arrays Declaring arrays Passing arrays to functions Searching arrays with linear search Sorting arrays with insertion sort Multidimensional arrays Programming.
C++ Array 1. C++ provides a data structure, the array, which stores a fixed-size sequential collection of elements of the same type. An array is used.
MULTI-DIMENSIONAL ARRAYS 1. Multi-dimensional Arrays The types of arrays discussed so far are all linear arrays. That is, they all dealt with a single.
SEQUENTIAL AND OBJECT ORIENTED PROGRAMMING Arrays.
ARRAYS Multidimensional realities Image courtesy of
1 ENERGY 211 / CME 211 Lecture 4 September 29, 2008.
© 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.
Two-Dimensional Data Class of 5 students Each student has 3 test scores Store this information in a two- dimensional array First dimension: which student.
Introduction to programming in java Lecture 21 Arrays – Part 1.
Two-Dimensional Arrays
A simple way to organize data
Two-Dimensional Arrays
2D Arrays October 12, 2007 ComS 207: Programming I (in Java)
Introduction To Programming Information Technology , 1’st Semester
Arrays .
MSIS 655 Advanced Business Applications Programming
C++ Array 1.
Presentation transcript:

Arrays

Arrays Aggregate data type stores data of the same type in a block of consecutive memory locations Individual locations in the array are called elements Elements in the array are accessed by index in Java, arrays are indexed starting at 0 The size of an array is the number of elements it is able to store arrays have fixed size not possible to change the size of an array once you have declared the array

Analogy Post office mailboxes Purpose is to store data of type “mail” One main unit divided into fixed number of individual locations mailboxes Mailboxes are indexed consecutively by number Contents of a mailbox are accessed by box number

An array of integers 3 16 -1 8 7 1 55 -3 All elements in the array are of type integer The length of the array is 10 The array is indexed from 0 to 9

An array of integers 3 16 -1 8 7 1 55 -3 index 0 All elements in the array are of type integer The length of the array is 10 The array is indexed from 0 to 9 The element at index 0 is 3

An array of integers 3 16 -1 8 7 1 55 -3 index 4 All elements in the array are of type integer The length of the array is 10 The array is indexed from 0 to 9 The element at index 4 is 8

An array of integers 3 16 -1 8 7 1 55 -3 index 7 All elements in the array are of type integer The length of the array is 10 The array is indexed from 0 to 9 The element at index 7 is 55

An array of integers 3 16 -1 8 7 1 55 -3 index 9 All elements in the array are of type integer The length of the array is 10 The array is indexed from 0 to 9 The element at index 9 is 1

Java arrays Arrays have names, types, and size Arrays must be declared and their size must be specified before you can use them in a program The Java statement int A; declares a single integer variable named A The Java statement int A[] = new int[10]; declares an array variable A that holds 10 integer values To declare an array named D to hold 20 double values: double D[] = new double[20];

Assigning array values Declare an array of 5 integers int[] A = new int[5]; Assign values to array elements A[0] = 4; A[1] = -6; A[2] = -2; A[3] = 11; A[4] = 12; Variable initializer syntax: when you know what values you want to store in the array int[] A = {4, -6, -2, 11, 12};

Array access Declare an array A containing the integers 2, 4, -9, 0, 2 int[] A = {2, 4, -9, 0, 2}; What is the value of A[0]? (2) What is the value of A[1]? (4) What is the value of A[3]? (0) What is the value of A[5]? (error) What is the value of A[10]? (error)

Array length property The number of elements in an array is given by the length property Length of array is determined when array is created either explicitly specified or comes from the length of the {…} initialization list. The length of an array named A is given by A.length For example, if we declare arrays A and B as follows: int[] A = {2, 4, -9, 0, 2}; int[] B = {-1, 7, 3, 3, 3, 6, 9}; then A.length returns 5 and B.length returns 7

Operations on array elements int[] A = {2, 4, -9, 0, 2}; int[] B = {-1, 7, 3, 3, 3, 6, 9}; All elements in arrays A and B are of type int int w = A[0]; (w = 2) int x = 5+B[2]; (x = 8) int y = A[1]*B[5]; (y = 24) int z = B[6]-A.length; (z = 4) int p = A+B; (error) int q = B[3]*1.5; (error) int r = 2*B; (error)

Manipulating array values Consider the following code fragment: What values will be stored in F? int[] F = new int[6]; F[0] = 1; F[1] = 1; F[2] = F[1]+F[0]; F[3] = F[2]+F[1]; F[4] = F[3]+F[2]; F[5] = F[4]+F[3]; ?

Manipulating array values Consider the following code fragment: What values will be stored in F? int[] F = new int[6]; F[0] = 1; F[1] = 1; F[2] = F[1]+F[0]; F[3] = F[2]+F[1]; F[4] = F[3]+F[2]; F[5] = F[4]+F[3]; 1 2 3 5 8 Answer:

Arrays and subroutines Subroutine sumArray computes the sum of the elements in an array of doubles double sumArray(double[] A) { double sum = 0.0; for(int k = 0; k < A.length; k++) sum = sum + A[k]; return sum; }

Exercise Write a Java subroutine minArray that takes an array of doubles as input and returns the minimum value in the array for example, if the input array is your subroutine should return -1.28 1.3 -0.16 -1.28 0.0 3.1 7.01 -1.11

Multiple dimension arrays Arrays with multiple dimensions may be declared and used int[][] A = new int[5][6]; int[][][] B = new int[4][2][2]; Number of square bracket pairs is dimension of array A is a two-dimensional array B is a three-dimensional array Two-dimensional (2D) arrays by Java convention, in a 2D array the first index indicates the row and the second the column we can visualize a 2D array as a grid or table of elements

2D arrays A 2D array is basically a 1D array of 1D arrays these are the rows of the array each row is stored in a separate block of consecutive memory locations If we declare array A as int[][] A = new int[5][6]; then A[k] is a 1D array, the kth row of A A.length is the number of rows in A A[k].length is the length of the kth row of A

Example Let A denote a 2D array of integers with 5 rows and 6 columns, and suppose A contains the elements as shown below What is A.length? What is A[2].length? What is A[2][4]? What is A[4][0]? What is A[3][5]? What is A[5][6]? 1 2 4 3 7 5 9 6 8

Applications 2D arrays are useful when data can be represented by a a grid of fixed dimensions Often used to represent tables, matrices, images, and game boards Examples of games include checkers, chess, tic-tac-toe, crosswords, and mazes