ARRAYS Multidimensional realities Image courtesy of

Slides:



Advertisements
Similar presentations
Introduction to C Programming
Advertisements

Arrays.
Some containers are called linear because their content (items) are stored in a single row (line). Which of the following are linear?  the rungs of a.
Arrays Chapter 6. Outline Array Basics Arrays in Classes and Methods Sorting Arrays Multidimensional Arrays.
Strings and Arrays The objectives of this chapter are:  To discuss the String class and some of its methods  To discuss the creation and use of Arrays.
For use of IST410 Students only Arrays-1 Arrays. For use of IST410 Students only Arrays-2 Objectives l Declaring arrays l Instantiating arrays l Using.
Arrays Chapter 6 Chapter 6.
ECE122 L14: Two Dimensional Arrays March 27, 2007 ECE 122 Engineering Problem Solving with Java Lecture 14 Two Dimensional Arrays.
Arrays  Writing a program that uses a large amount of information.  Such as a list of 100 elements.  It is not practical to declare.
Computer Science 1620 Multi-Dimensional Arrays. we used arrays to store a set of data of the same type e.g. store the assignment grades for a particular.
©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.
Chapter 8 Arrays and Strings
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.
Chapter 6Java: an Introduction to Computer Science & Programming - Walter Savitch 1 l Array Basics l Arrays in Classes and Methods l Programming with Arrays.
Chapter 7 Arrays. © 2004 Pearson Addison-Wesley. All rights reserved11-2 Arrays Arrays are objects that help us organize large amounts of information.
Chapter 8 Arrays and Strings
Arrays Chapter 7. 2 "All students to receive arrays!" reports Dr. Austin. Declaring arrays scores : Inspecting.
 2008 Pearson Education, Inc. All rights reserved JavaScript: Arrays.
Introduction to Arrays in Java Corresponds with Chapter 6 of textbook.
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.
Object-Oriented Program Development Using Java: A Class-Centered Approach, Enhanced Edition.
1 © 2002, Cisco Systems, Inc. All rights reserved. Arrays Chapter 7.
ARRAYS Computer Engineering Department Java Course Asst. Prof. Dr. Ahmet Sayar Kocaeli University - Fall
Arrays An array is a data structure that consists of an ordered collection of similar items (where “similar items” means items of the same type.) An array.
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.
C++ Programming: From Problem Analysis to Program Design, Fifth Edition Arrays.
M180: Data Structures & Algorithms in Java Arrays in Java Arab Open University 1.
Two-Dimensional Arrays That’s 2-D Arrays Girls & Boys! One-Dimensional Arrays on Steroids!
Section 5 - Arrays. Problem solving often requires information be viewed as a “list” List may be one-dimensional or multidimensional List is implemented.
CS 139-Programming Fundamentals Lecture 11B - Arrays Adapted from a presentation by Dr. Rahman Fall 2014.
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.
 2008 Pearson Education, Inc. All rights reserved. 1 Arrays and Vectors.
Arrays and ArrayLists Topic 6. One Dimensional Arrays Homogeneous – all of the same type Contiguous – all elements are stored sequentially in memory For.
Chapter 6Java: an Introduction to Computer Science & Programming - Walter Savitch 1 Chapter 6 l Array Basics l Arrays in Classes and Methods l Programming.
Arrays.
IT259 Foundation of Programming Using Java Unit 9 Seminar : (Chapter 8 ) Instructor : Vladimir Gubanov, PhD
JAVA: An Introduction to Problem Solving & Programming, 5 th Ed. By Walter Savitch and Frank Carrano. ISBN © 2008 Pearson Education, Inc., Upper.
1 Arrays of Arrays Quick review … arrays Arrays of arrays ≡ multidimensional array Example: times table Representation in memory Ragged arrays Example:
CS 180 Recitation 7 Arrays. Used to store similar values or objects. An array is an indexed collection of data values of the same type. Arrays are the.
Arrays.
Arrays Chapter 7. 2 Declaring and Creating Arrays Recall that an array is a collection of elements all of the _____________ Array objects in Java must.
Arrays Chapter 7. MIS Object Oriented Systems Arrays UTD, SOM 2 Objectives Nature and purpose of an array Using arrays in Java programs Methods.
Two Dimensional Arrays. Students will be able to: code algorithms to solve two- dimensional array problems. use 2-D arrays in programs. pass two-use 2-D.
Arrays in java Unit-1 Introduction to Java. Array There are situations where we might wish to store a group of similar type of values in a variable. Array.
 Introducing Arrays  Declaring Array Variables, Creating Arrays, and Initializing Arrays  Copying Arrays  Multidimensional Arrays  Search and Sorting.
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.
SEQUENTIAL AND OBJECT ORIENTED PROGRAMMING Arrays.
Chapter 5: Arrays in Java. The objectives of this chapter are:  1. To discuss the creation and use of Arrays.   2. To continue to use the String class.
 2005 Pearson Education, Inc. All rights reserved Arrays.
Asserting Java © Rick Mercer Chapter 7 The Java Array Object.
© Rick Mercer Chapter 7 The Java Array Object.  Some variables store precisely one value: a double stores one floating-point number a double stores one.
ARRAY AND LOOPS REVIEW Mr. Crone. What will the code below print? int[] arry = {2, 5, 2, 1, 3}; for(int i = 0; i < 5; i ++) System.out.print(arry[i]);
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.
LESSON 8: INTRODUCTION TO ARRAYS. Lesson 8: Introduction To Arrays Objectives: Write programs that handle collections of similar items. Declare array.
Arrays Chapter 7.
Chapter 6: Using Arrays.
Computer Programming BCT 1113
Array, Strings and Vectors
Introduction To Programming Information Technology , 1’st Semester
Object Oriented Programming in java
Arrays Chapter 7.
Arrays.
Presentation transcript:

ARRAYS Multidimensional realities Image courtesy of

Arrays  An object that stores elements. Each element is accessible by an index.  The indices are numbered from 0 upwards  The number of elements in the array (the length) must be known when making the array  The length of the array cannot ever change.  Example:  Length is 9  Indices are in [0..8] Index Value

Arrays  A contiguous chunk of elements in memory. While an array is an object (of sorts) there are language-level specifics when dealing with arrays.  Declare: T[] array_name; T array_name[];  Create: array_name = new T[number];  Read/Write: array_name[index] = array_name[index]; When an array is indexed on the left of an assignment, this represents the name of the element When an array is indexed on the right, this represents the value of the element

Arrays  Examples that declare array type variables  int[] xs;  String words[];  SimpleList [] lines;  boolean[] checked;  Examples that create array type objects  xs = new int[30];  words = new String[100];  lines = new SimpleList [10];  checked = new boolean[25];  Details about array creation:  The length of the array must be given when creating  The contents of the array are: zero for an array of numeric primitive type null for an array of object types false for an array of boolean

To Initialize when Declared  Arrays can be created with content when they are declared by using curly-bracket notation.  int[] xs = {1,2,3};  boolean checked[] = {false, true, false, false};  String[] words = {"fat", "cat", "hat"};  SimpleList lines = {new SimpleList (), new SimpleList ()};

int[] data1 = {4, 3, 2, 1, 0}; int[] data2 = new int[3]; for(int i=0; i<data2.length; i++){ data2[i] = i*2; } data2[0] = data1[0] * 2; data2[1] = data1[1] * 2; data2[data1[4]] = data2[data1[3]]; data1[0] = data1[0] * 2; int[] data1 = {4, 3, 2, 1, 0}; int[] data2 = new int[3]; for(int i=0; i<data2.length; i++){ data2[i] = i*2; } data2[0] = data1[0] * 2; data2[1] = data1[1] * 2; data2[data1[4]] = data2[data1[3]]; data1[0] = data1[0] * 2; Example Name Value

Arrays are Objects  An array is an object. This implies that an array may have attributes and behavior.  The only attribute is "length". This attribute is 'public' Can be accessed by any code anywhere This attribute is 'final' The value of this attribute cannot change  Examples:  int[] x = new int[30];  int z = x.length;  x.length = 52; // is an error since length can't change

Array methods  Arrays do not support any useful behaviors.  Arrays are objects and hence have all methods defined by the Object class.  The only two commonly used are: equals toString

Arrays  What do the ‘=‘, ‘==‘ and “.equals(..)” mean? int[] data1 = new int[10]; int[] data2 = new int[10]; for(int i=0; i<data1.length; i++) { data1[i] = i; } for(int i=0; i<data2.length; i++) { data2[i] = data1[i]; } System.out.println(data1.equals(data2) + “\t” + (data1==data2)); data2[3] = 123; System.out.println(data1.equals(data2) + “\t” + (data1==data2)); data1 = data2; System.out.println(data1.equals(data2) + “\t” + (data1==data2)); int[] data1 = new int[10]; int[] data2 = new int[10]; for(int i=0; i<data1.length; i++) { data1[i] = i; } for(int i=0; i<data2.length; i++) { data2[i] = data1[i]; } System.out.println(data1.equals(data2) + “\t” + (data1==data2)); data2[3] = 123; System.out.println(data1.equals(data2) + “\t” + (data1==data2)); data1 = data2; System.out.println(data1.equals(data2) + “\t” + (data1==data2));

Example problems  Write a method that accepts an array of Strings and returns the longest of the Strings.  Write a method that accepts an array of Strings and a character. Count the number of times the character occurs in the array of strings.  Write a method that accepts an array of integers and compute an array of cumulative sums.  Write a method that accepts an array of SimpleList. The method should return the sum of all integers in the lists of the array.

Multidimensional arrays  Arrays with more than one index  number of dimensions = number of indexes  Arrays with more than two dimensions are a simple extension of 2D arrays  A 2D array corresponds to a table or grid or matrix  the first dimension is the row  the second dimension is the column  a cell or element is the intersection of a row and column

2D Arrays  Syntax for declaring a 2D array:  T name[ ][ ];  T[][] name;  Examples:  String names[][];  double angles[][];  int grades[][];  Syntax for creating a 2D array object:  new T[rows][cols]  Examples:  new String[3][3]  new double[2][10]  new int[100][2]

Creating a 2D Array int table[][] = new int[3][4]; for(int row=0; row<3; row++) { for(int col=0; col<4; col++) { table[row][col] = row+col; } int table[][] = new int[3][4]; for(int row=0; row<3; row++) { for(int col=0; col<4; col++) { table[row][col] = row+col; } } Indices

Computing with 2D Arrays  Problem: Write a method to compute the sum of the MAJOR DIAGONAL of a square 2D array (the diagonal elements from top-left to lower-right).  Assume that a 4x4 array is passed into the method public int diagonalSum(int[][] table) { int diagonalSum = 0; for(int row=0; row<4; row++) { diagonalSum = diagonalSum + table[row][row]; } return diagonalSum; } public int diagonalSum(int[][] table) { int diagonalSum = 0; for(int row=0; row<4; row++) { diagonalSum = diagonalSum + table[row][row]; } return diagonalSum; } Indices

Computing with 2D Arrays  Write a method to randomly generate a 4x4 table of characters. A Boggle Board!  Assume that we can use a method named "randomCharacter()" that returns a single randomly selected character in [a-z].

Computing with 2D Arrays  Multi-dimensional arrays in Java are just one- dimensional arrays that contain one-dimensional array elements.

Computing with 2D Arrays  Problem: Write a method to compute the sum of all elements of a 2D array of ints. public int diagonalSum(int[][] table) { int sum = 0; for(int row=0; row<table.length; row++) { for(int col=0; col<table[0].length; col++) { diagonalSum = diagonalSum + table[row][col]; } return sum; } public int diagonalSum(int[][] table) { int sum = 0; for(int row=0; row<table.length; row++) { for(int col=0; col<table[0].length; col++) { diagonalSum = diagonalSum + table[row][col]; } } return sum; }

Computing with 2D Arrays  Problem: Write a method to return the major diagonal elements of any square 2D array (the diagonal elements from top-left to lower-right) int[] diagonalElements(int[][] table) { int[] diagonalElements = new int[table.length]; for(int row=0; row < table.length; row++) { diagonalElements[row] = table[row][row]; } return diagonalElements; }

Computing with 2D Arrays  Problem: write a method to return an identity matrix. The dimensionality is given as a formal parameter  An identify matrix is a square matrix with zeros at every cell except that the major diagonal elements have a value of 1 public int[][] identity(int x) { int[][] result = new int[x][x]; for(int i=0; i<x; i++) { result[i][i] = 1; } return result; }

Ragged arrays  Recall that a 2D array is a 1D array of 1D arrays.  Ragged arrays have rows of unequal length  rows may have a different number of columns  Ragged arrays are allowed in Java  Example: create a 2-D int array named b with 5 elements in the first row, 7 in the second row, and 4 in the third row: int[][] b; b = new int[3][]; b[0] = new int[5]; b[1] = new int[7]; b[2] = new int[4];

Pop Quiz!  Write a Java declaration for each type of array.  Write a Java statement that instantiates each object.  Write an expression for each red element.