 2003 Prentice Hall, Inc. All rights reserved. 1 Arrays Outline 1 Introduction 2 Arrays 3Declaring Arrays 4Processing Array Contents 5 Multiple-Subscripted.

Slides:



Advertisements
Similar presentations
2000 Prentice Hall, Inc. All rights reserved. 1 Capitolo 4 - Arrays Outline 4.1Introduction 4.2Arrays 4.3Declaring Arrays 4.4Examples Using Arrays 4.5Passing.
Advertisements

CS 141 Computer Programming 1 1 Arrays. Outline  Introduction  Arrays  Declaring Arrays  Examples Using Arrays  Sorting Arrays  Multiple-Subscripted.
 2000 Prentice Hall, Inc. All rights reserved Fundamentals of Strings and Characters String declarations –Declare as a character array or a variable.
Arrays. Introduction Arrays –Structures of related data items –Static entity - same size throughout program A few types –C-like, pointer-based arrays.
4.1Introduction Arrays –Structures of related data items –Static entity (same size throughout program) A few types –Pointer-based arrays (C-like) –Arrays.
 2003 Prentice Hall, Inc. All rights reserved. 7.1 Introduction Arrays –Data structures which reference one or more value –All items must have same data.
 2000 Prentice Hall, Inc. All rights reserved. 1 Chapter 4 - Arrays Outline 4.1Introduction 4.2Arrays 4.3Declaring Arrays 4.4Examples Using Arrays 4.5Passing.
 2003 Prentice Hall, Inc. All rights reserved Introduction Arrays –Structures of related data items –Static entity (same size throughout program)
© Copyright 1992–2004 by Deitel & Associates, Inc. and Pearson Education Inc. All Rights Reserved. 1 Chapter 6 - Arrays Outline 6.1Introduction 6.2Arrays.
 2000 Prentice Hall, Inc. All rights reserved. Chapter 6 - Arrays Outline 6.1Introduction 6.2Arrays 6.3Declaring Arrays 6.4Examples Using Arrays 6.5Passing.
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.
 2003 Prentice Hall, Inc. All rights reserved. 1 Arrays –Structures of related data items –Static entity (same size throughout program) A few types –Pointer-based.
Chapter 6 C Arrays Acknowledgment The notes are adapted from those provided by Deitel & Associates, Inc. and Pearson Education Inc. Arrays are data structures.
Multiple-Subscripted Array
 2007 Pearson Education, Inc. All rights reserved. 1 C Arrays.
1 CSCE 1030 Computer Science 1 Arrays Chapter 7 in Small Java.
 2007 Pearson Education, Inc. All rights reserved C Arrays.
1 JavaScript/Jscript: Arrays. 2 Introduction Arrays –Data structures consisting of related data items (collections of data items) JavaScript arrays are.
Programming Languages -1 (Introduction to C) arrays Instructor: M.Fatih AMASYALI
 2004 Prentice Hall, Inc. All rights reserved. 1 Chapter 11 - JavaScript: Arrays Outline 11.1 Introduction 11.2 Arrays 11.3 Declaring and Allocating Arrays.
© Copyright 1992–2004 by Deitel & Associates, Inc. and Pearson Education Inc. All Rights Reserved. 1 Chapter 6 - Arrays Outline 6.1Introduction 6.2Arrays.
 2006 Pearson Education, Inc. All rights reserved Arrays.
Session 7 JavaScript/Jscript: Arrays Matakuliah: M0114/Web Based Programming Tahun: 2005 Versi: 5.
Arrays Chapter 7. 2 "All students to receive arrays!" reports Dr. Austin. Declaring arrays scores : Inspecting.
Chapter 6 Arrays Associate Prof. Yuh-Shyan Chen Dept. of Computer Science and Information Engineering National Chung-Cheng University.
Introduction to Arrays in Java Corresponds with Chapter 6 of textbook.
Algorithm and Programming Array Dr. Ir. Riri Fitri Sari MM MSc International Class Electrical Engineering Dept University of Indonesia 15 March 2009.
JAVA: An Introduction to Problem Solving & Programming, 5 th Ed. By Walter Savitch and Frank Carrano. ISBN © 2009 Pearson Education, Inc., Upper.
 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.
Java Script: Arrays (Chapter 11 in [2]). 2 Outline Introduction Introduction Arrays Arrays Declaring and Allocating Arrays Declaring and Allocating Arrays.
 2000 Prentice Hall, Inc. All rights reserved Arrays Array –Group of consecutive memory locations –Same name and type To refer to an element, specify.
C Lecture Notes 1 Arrays Lecture 6. C Lecture Notes 2 6.1Introduction Arrays –Structures of related data items –Static entity – same size throughout program.
 2003 Prentice Hall, Inc. All rights reserved. 1 Arrays Outline Introduction Arrays Declaring Arrays Examples Using Arrays.
1 Chapter 4 - Arrays Outline 4.1Introduction 4.2Arrays 4.3Declaring Arrays 4.4Examples Using Arrays 4.5Passing Arrays to Functions 4.6Sorting Arrays 4.7Case.
 2000 Deitel & Associates, Inc. All rights reserved. Chapter 4 - Arrays Outline 4.1Introduction 4.2Arrays 4.3Declaring Arrays 4.4Examples Using Arrays.
 2003 Prentice Hall, Inc. All rights reserved. 1 Vectors.
 2007 Pearson Education, Inc. All rights reserved C Arrays.
JAVA: An Introduction to Problem Solving & Programming, 6 th Ed. By Walter Savitch ISBN © 2012 Pearson Education, Inc., Upper Saddle River,
 2008 Pearson Education, Inc. All rights reserved. 1 Arrays and Vectors.
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: An Introduction to Problem Solving & Programming, 5 th Ed. By Walter Savitch and Frank Carrano. ISBN © 2008 Pearson Education, Inc., Upper.
C++ Programming Lecture 14 Arrays – Part I The Hashemite University Computer Engineering Department (Adapted from the textbook slides)
Chapter 8 Slides from GaddisText Arrays of more than 1 dimension.
C++ Programming Lecture 16 Arrays – Part III The Hashemite University Computer Engineering Department (Adapted from the textbook slides)
 2000 Prentice Hall, Inc. All rights reserved. 1 Chapter 4 - Arrays Outline 4.1Introduction 4.2Arrays 4.3Declaring Arrays 4.4Examples Using Arrays 4.5Passing.
SEQUENTIAL AND OBJECT ORIENTED PROGRAMMING Arrays.
1 Lecture 4: Part1 Arrays Introduction Arrays  Structures of related data items  Static entity (same size throughout program)
© Copyright 1992–2004 by Deitel & Associates, Inc. and Pearson Education Inc. All Rights Reserved. 1 Arrays Outline 6.1Introduction 6.2Arrays 6.3Declaring.
CHAPTER 3 ARRAYS Dr. Shady Yehia Elmashad. Outline 1.Introduction 2.Arrays 3.Declaring Arrays 4.Examples Using Arrays 5.Multidimensional Arrays 6.Multidimensional.
 2005 Pearson Education, Inc. All rights reserved Arrays.
 2000 Prentice Hall, Inc. All rights reserved Arrays Array –Consecutive group of memory locations –Same name and type To refer to an element, specify.
1 Two-Dimensional Arrays. 2 Terminology Two-dimensional arrays represent matrices A matrix contains a number of values of the same data type The values.
Arrays Chapter 7.
Two Dimensional Array Mr. Jacobs.
Arrays Outline 1 Introduction 2 Arrays 3 Declaring Arrays
Java How to Program, Late Objects Version, 10/e
Chapter 6 - Arrays Outline 6.1 Introduction 6.2 Arrays
Chapter 8 Slides from GaddisText
Arrays Kingdom of Saudi Arabia
EKT150 : Computer Programming
Arrays Outline Introduction Arrays Declaring Arrays
Chapter 6 - Arrays Outline 6.1 Introduction 6.2 Arrays
Introduction To Programming Information Technology , 1’st Semester
Arrays Chapter 7.
Arrays Arrays A few types Structures of related data items
Data Structure(s) A way of storing and organizing data in a computer so that it can be used efficiently. e.g. Arrays Linked Lists stacks Queues Trees.
Presentation transcript:

 2003 Prentice Hall, Inc. All rights reserved. 1 Arrays Outline 1 Introduction 2 Arrays 3Declaring Arrays 4Processing Array Contents 5 Multiple-Subscripted Arrays 6 ArrayList

 2003 Prentice Hall, Inc. All rights reserved. 2 1Introduction Array –Consecutive group of values (i.e., 3x3 caption value of buttons in q6 in project 3) –Same name and type ( int, char, etc.) To refer to an element –Specify array name and position number (index) –Format: arrayname[ position number ] –First element at position 0 (off-by-one error, p442) N-element array c (Figure 8-2, page 436) c[ 0 ], c[ 1 ] … c[ n - 1 ] –Nth element as position N-1

 2003 Prentice Hall, Inc. All rights reserved. 3 2Arrays Array elements like other variables –Figure 8-4, page 438 (Concept) Assignment, printing for an integer array c c[ 0 ] = 3; System.out.print (c[ 0 ]); –Code 8-1, page 438 (access of any element) –Code 8-2, page 440 (loop based access) Can perform operations inside subscript c[ 5 – 2 ] same as c[3]

 2003 Prentice Hall, Inc. All rights reserved. 4 2Arrays c[6] Name of array (Note that all elements of this array have the same name, c) c[0] c[1] c[2] c[3] c[11] c[10] c[9] c[8] c[7] c[5] c[4] Position number of the element within array c

 2003 Prentice Hall, Inc. All rights reserved. 5 3Declaring Arrays When declaring arrays, specify –Name –Type of array Any data type –Number of elements –type [] arrayName = new type [ arraySize ]; int [] c = new int [ 10 ]; // array of 10 integers float [] d = new float [ 3284 ]; // array of 3284 floats

 2003 Prentice Hall, Inc. All rights reserved. 6 3Declaring Arrays Initializing arrays (p443) –For loop Set each element –Initializer list Specify each element when array declared int [] n = { 1, 2, 3, 4, 5 }; –If array size omitted, initializers determine size –int [] n = { 1, 2, 3, 4, 5 }; 5 initializers, therefore 5 element array Alternative declaration (p444) –int [] n –int n []

 2003 Prentice Hall, Inc. All rights reserved. 7 4Processing Array Contents Array length –int [] n = new int [5] size = n.length for(int j = 0; j<size; j++) –Enhanced for loop for (dataType elementVal : array) statement; for (int x: n) System.out.println(x); Or for (dataType elementVal : array) { statement1; statement2; } for (int x: n) { System.out.print(“The next value is ”); System.out.println(x); }

 2003 Prentice Hall, Inc. All rights reserved. 8 4Processing Array Contents Dynamic data storage (user defined size of array) –Code 8-6, page 450 –int [] n; n= new int [n_size]; Copy –Code 8-7, page 453 Pass array in argument –Bring back all the change on array, multiple return –Code 8-9, page 456 –Code 8-12, page 468

 2003 Prentice Hall, Inc. All rights reserved. 9 4Processing Array Contents Summing the values in an array Finding the highest and lowest value in an array String array, code 8-13, page 470 Search –Unsorted array: linear search –Sorted array: binary search (Code 8-23, page 505) Array of objects (such as buttons)

 2003 Prentice Hall, Inc. All rights reserved. 10 5Multiple-Subscripted Arrays Multiple subscripts –a[ i ][ j ] –dType [][] aName = new dType [n_row][n_column] –Tables with rows and columns –Specify row, then column –“Array of arrays” a[0] is an array of 4 elements a[0][0] is the first element of that array Row 0 Row 1 Row 2 Column 0Column 1Column 2Column 3 a[ 0 ][ 0 ] a[ 1 ][ 0 ] a[ 2 ][ 0 ] a[ 0 ][ 1 ] a[ 1 ][ 1 ] a[ 2 ][ 1 ] a[ 0 ][ 2 ] a[ 1 ][ 2 ] a[ 2 ][ 2 ] a[ 0 ][ 3 ] a[ 1 ][ 3 ] a[ 2 ][ 3 ] Row subscript Array name Column subscript

 2003 Prentice Hall, Inc. All rights reserved. 11 5Multiple-Subscripted Arrays To initialize –Default of 0 –Initializers grouped by row in braces int [][] b = { { 1, 2 }, { 3, 4 } }; Length, code 8-17, page 484 Ragged array (dynamic storage must) –int [] [] n = new int [4] [] n[2]=new int [5] n[3]=new int [6] –Page Row 0Row 1

 2003 Prentice Hall, Inc. All rights reserved. 12 5Multiple-Subscripted Arrays Command Line Argument, code 8-19, page 497

 2003 Prentice Hall, Inc. All rights reserved. 13 Implement a program to simulate a Hollywood square: Input three different numbers, to check if they are in the same column, row or diagonal. For example: 1) Input three positions: Answer: Yes 2) Input three positions: Answer: No 3) Input three positions: Answer: Yes Sample program Complete program Better Version program

 2003 Prentice Hall, Inc. All rights reserved. 14 Implement a program to simulate a Hollywood square: Best Version program

 2003 Prentice Hall, Inc. All rights reserved. 15 6Array List Page ArrayList nameList = new ArrayList ( ); Import java.util.ArrayList; nameList.add( content ); nameList.size( ); nameList.get(index); //casting nameList.remove(index); nameList.add(index, content); nameList.set(index, new_content);