1 Arrays. 2 Background  Programmer often need the ability to represent a group of values as a list List may be one-dimensional or multidimensional 

Slides:



Advertisements
Similar presentations
Liang, Introduction to Java Programming, Eighth Edition, (c) 2011 Pearson Education, Inc. All rights reserved Fall 2013.
Advertisements

Arrays and ArrayLists Ananda Gunawardena. Introduction Array is a useful and powerful aggregate data structure presence in modern programming languages.
Arrays Chapter 6. Outline Array Basics Arrays in Classes and Methods Sorting Arrays Multidimensional Arrays.
Multidimensional arrays Many problems require information be organized as a two- dimensional or multidimensional list Examples –Matrices –Graphical animation.
JAVA: An Introduction to Problem Solving & Programming, 5 th Ed. By Walter Savitch and Frank Carrano. ISBN © 2008 Pearson Education, Inc., Upper.
1 2-D Arrays Overview l Why do we need Multi-dimensional array l 2-D array declaration l Accessing elements of a 2-D array l Declaration using Initializer.
1 Arrays b An array is an ordered list of values An array of size N is indexed from zero to N-1 scores.
Arrays CS Feb Announcements Exam 1 Grades on Blackboard Project 2 scores: end of Class Project 4, due date:20 th Feb –Snakes & Ladders Game.
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.
CS102--Object Oriented Programming Lecture 6: – The Arrays class – Multi-dimensional arrays Copyright © 2008 Xiaoyan Li.
Chapter 9: Arrays and Strings
Arrays Data Structures - structured data are data organized to show the relationship among the individual elements. It usually requires a collecting mechanism.
Chapter 8 Arrays and Strings
1 CSCE 1030 Computer Science 1 Arrays Chapter 7 in Small Java.
1 Arrays Chapter 8 Spring 2007 CS 101 Aaron Bloomfield.
Chapter 6Java: an Introduction to Computer Science & Programming - Walter Savitch 1 l Array Basics l Arrays in Classes and Methods l Programming with Arrays.
CS1101: Programming Methodology Aaron Tan.
1 Week 9 l Array Basics l Arrays in Classes and Methods l Programming with Arrays and Classes l Sorting Arrays l Multidimensional Arrays Arrays.
Chapter 8 Arrays and Strings
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.
Programming Fundamentals I (COSC-1336), Lecture 8 (prepared after Chapter 7 of Liang’s 2011 textbook) Stefan Andrei 4/23/2017 COSC-1336, Lecture 8.
Object-Oriented Program Development Using Java: A Class-Centered Approach, Enhanced Edition.
Chapter 8: Collections: Arrays. 2 Objectives One-Dimensional Arrays Array Initialization The Arrays Class: Searching and Sorting Arrays as Arguments The.
Liang, Introduction to Java Programming, Eighth Edition, (c) 2011 Pearson Education, Inc. All rights reserved
ARRAYS Computer Engineering Department Java Course Asst. Prof. Dr. Ahmet Sayar Kocaeli University - Fall
Chapter 6Java: an Introduction to Computer Science & Programming - Walter Savitch 1 Chapter 6 l Array Basics l Arrays and Methods l Programming with Arrays.
CS1101: Programming Methodology Aaron Tan.
M180: Data Structures & Algorithms in Java Arrays in Java Arab Open University 1.
1 Arrays Chapter 8 Spring 2006 CS 101 Aaron Bloomfield.
Arrays  Array is a collection of same type elements under the same variable identifier referenced by index number.  Arrays are widely used within programming.
1 Representing Relations Rosen, section 7.3 CS/APMA 202 Aaron Bloomfield.
Department of Computer Engineering Computer Programming for International Engineers I NTERNATIONAL S CHOOL OF E NGINEERING C HULALONGKORN U NIVERSITY.
Week # 2: Arrays.  Data structure  A particular way of storing and organising data in a computer so that it can be used efficiently  Types of data.
Section 5 - Arrays. Problem solving often requires information be viewed as a “list” List may be one-dimensional or multidimensional List is implemented.
Arrays. Background  Programmer often need the ability to represent a group of values as a list List may be one-dimensional or multidimensional  Java.
Arrays. Background  Programmer often need the ability to represent a group of values as a list List may be one-dimensional or multidimensional  Java.
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.
1 Arrays Spring 2006 UVA - CS 101 Aaron Bloomfield Edited for WLCS by Paul Bui.
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.
CSIS 3401 Introduction to Data structures and algorithms.
JAVA: An Introduction to Problem Solving & Programming, 7 th Ed. By Walter Savitch ISBN © 2015 Pearson Education, Inc., Upper Saddle River,
Arrays Chapter 6. Objectives learn about arrays and how to use them in Java programs learn how to use array parameters and how to define methods that.
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.
Java – An Object Oriented Language CS 307 Lecture Notes Lecture Weeks 5-6 Khalid Siddiqui.
1 Arrays. 2 Background  Programmer often need the ability to represent a group of values as a list List may be one-dimensional or multidimensional 
Arrays Chapter 7. MIS Object Oriented Systems Arrays UTD, SOM 2 Objectives Nature and purpose of an array Using arrays in Java programs Methods.
Chapter 8 Slides from GaddisText Arrays of more than 1 dimension.
Arrays. Arrays as ADTs An array is an Abstract Data Type –The array type has a set of values The values are all the possible arrays –The array type has.
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.
Lecture #15 ARRAYS By Shahid Naseem (Lecturer). 2 ARRAYS DEFINITION An array is a sequence of objects of same data type. The objects in an array are also.
Chapter 9 Introduction to Arrays Fundamentals of Java.
Arrays Chapter 7.
Computer Programming BCT 1113
Chapter 7 Part 1 Edited by JJ Shepherd
Arrays.
Arrays Continued.
Chapter 8 Fall 2006 CS 101 Aaron Bloomfield
Chapter 8 Spring 2006 CS 101 Aaron Bloomfield
COMS 261 Computer Science I
Consider Write a program that prompts a user to enter the number of students and then, their names and grades. The program will then outputs the average.
COMS 261 Computer Science I
Review for Midterm 3.
Arrays.
Presentation transcript:

1 Arrays

2 Background  Programmer often need the ability to represent a group of values as a list List may be one-dimensional or multidimensional  Java provides arrays and the collection classes The Vector class is an example of a collection class  Consider arrays first

3 Basic terminology  List is composed of elements  Elements in a list have a common name  The list as a whole is referenced through the common name  List elements are of the same type — the base type  Elements of a list are referenced by subscripting (indexing) the common name

4 Java array features  Subscripts are denoted as expressions within brackets: [ ]  Base (element) type can be any type  Size of array can be specified at run time This is different that pure C! (for the most part, at least)  Index type is integer and the index range must be 0... n-1 Where n is the number of elements  Automatic bounds checking Ensures any reference to an array element is valid  Data field length specifies the number of elements in the list  Array is an object Has features common to all other objects

5 Array variable definition styles  Without initialization Type of values in list Name of list Brackets indicate array variable being defined ElementType [ ] id; int [] a; int a[];

6 Array variable definition styles  With initialization ElementType [] id = new ElementType [n]; Nonnegative integer expression specifying the number of elements in the array Reference to a new array of n elements

7 Example  Definitions char[] c; int[] value = new int[10];  Causes Array object variable c is un-initialized Array object variable v references a new ten element list of integers  Each of the integers is default initialized to 0 value c …

IOCCC winners  2004 winners: 2004 anonymousRendering of a stroked fontanonymous 2004 arachnidCurses maze displayer/navigator with only line-of-sight visibilityarachnid 2004 burley A Poker game 2004 gavareA ray tracergavare 2004 gavin Mini-OS 2004 hibachi A CGI capable HTTP server 2004 hoyleCurses based polynomial graphing with auto-scalehoyle 2004 jdalbec Conway's look'n'say sequence split into elements 2004 kopczynski OCR of 8, 9, 10 and newbern Renders arbitary bitmapped fontsnewbern 2004 omoikane A CRC inserter 2004 schnitzi Editor animation 2004 sds Space/tab/linefeed steganography 2004 vik1 X Windows car racing game 2004 vik2 Calculates prime numbers using only CPP  At

9 Consider int[] v = new int[10]; int i = 7; int j = 2; int k = 4; v[0] = 1; v[i] = 5; v[j] = v[i] + 3; v[j+1] = v[i] + v[0]; v[v[j]] = 12; System.out.println(v[2]); v[k] = stdin.nextInt();

10 Consider int[] v = new int[10]; int i = 7; int j = 2; int k = 4; v[0] = 1; v[i] = 5; v[j] = v[i] + 3; v[j+1] = v[i] + v[0]; v[v[j]] = 12; System.out.println(v[2]); v[k] = stdin.nextInt();

11 Consider int[] v = new int[10]; int i = 7; int j = 2; int k = 4; v[0] = 1; v[i] = 5; v[j] = v[i] + 3; v[j+1] = v[i] + v[0]; v[v[j]] = 12; System.out.println(v[2]); v[k] = stdin.nextInt();

12 Consider int[] v = new int[10]; int i = 7; int j = 2; int k = 4; v[0] = 1; v[i] = 5; v[j] = v[i] + 3; v[j+1] = v[i] + v[0]; v[v[j]] = 12; System.out.println(v[2]); v[k] = stdin.nextInt();

13 Consider int[] v = new int[10]; int i = 7; int j = 2; int k = 4; v[0] = 1; v[i] = 5; v[j] = v[i] + 3; v[j+1] = v[i] + v[0]; v[v[j]] = 12; System.out.println(v[2]); v[k] = stdin.nextInt();

14 Consider int[] v = new int[10]; int i = 7; int j = 2; int k = 4; v[0] = 1; v[i] = 5; v[j] = v[i] + 3; v[j+1] = v[i] + v[0]; v[v[j]] = 12; System.out.println(v[2]); v[k] = stdin.nextInt();

15 Consider int[] v = new int[10]; int i = 7; int j = 2; int k = 4; v[0] = 1; v[i] = 5; v[j] = v[i] + 3; v[j+1] = v[i] + v[0]; v[v[j]] = 12; System.out.println(v[2]); v[k] = stdin.nextInt();

16 Consider int[] v = new int[10]; int i = 7; int j = 2; int k = 4; v[0] = 1; v[i] = 5; v[j] = v[i] + 3; v[j+1] = v[i] + v[0]; v[v[j]] = 12; System.out.println(v[2]); v[k] = stdin.nextInt(); 8 is displayed

17 Consider int[] v = new int[10]; int i = 7; int j = 2; int k = 4; v[0] = 1; v[i] = 5; v[j] = v[i] + 3; v[j+1] = v[i] + v[0]; v[v[j]] = 12; System.out.println(v[2]); v[k] = stdin.nextInt(); Suppose 3 is extracted

18 Consider  Segment int[] b = new int[100]; b[-1] = 0; b[100] = 0;  Causes Array variable to reference a new list of 100 integers  Each element is initialized to 0 Two exceptions to be thrown  -1 is not a valid index – too small  100 is not a valid index – too large IndexOutOfBoundsException

19 Consider Point[] p = new Point[3]; p[0] = new Point(0, 0); p[1] = new Point(1, 1); p[2] = new Point(2, 2); p[0].setX(1); p[1].setY(p[2].getY()); Point vertex = new Point(4,4); p[1] = p[0]; p[2] = vertex;

20 Consider Point[] p = new Point[3]; p[0] = new Point(0, 0); p[1] = new Point(1, 1); p[2] = new Point(2, 2); p[0].setX(1); p[1].setY(p[2].getY()); Point vertex = new Point(4,4); p[1] = p[0]; p[2] = vertex;

21 Consider Point[] p = new Point[3]; p[0] = new Point(0, 0); p[1] = new Point(1, 1); p[2] = new Point(2, 2); p[0].setX(1); p[1].setY(p[2].getY()); Point vertex = new Point(4,4); p[1] = p[0]; p[2] = vertex;

22 Consider Point[] p = new Point[3]; p[0] = new Point(0, 0); p[1] = new Point(1, 1); p[2] = new Point(2, 2); p[0].setX(1); p[1].setY(p[2].getY()); Point vertex = new Point(4,4); p[1] = p[0]; p[2] = vertex;

23 Consider Point[] p = new Point[3]; p[0] = new Point(0, 0); p[1] = new Point(1, 1); p[2] = new Point(2, 2); p[0].setX(1); p[1].setY(p[2].getY()); Point vertex = new Point(4,4); p[1] = p[0]; p[2] = vertex;

24 Consider Point[] p = new Point[3]; p[0] = new Point(0, 0); p[1] = new Point(1, 1); p[2] = new Point(2, 2); p[0].setX(1); p[1].setY(p[2].getY()); Point vertex = new Point(4,4); p[1] = p[0]; p[2] = vertex;

25 Consider Point[] p = new Point[3]; p[0] = new Point(0, 0); p[1] = new Point(1, 1); p[2] = new Point(2, 2); p[0].setX(1); p[1].setY(p[2].getY()); Point vertex = new Point(4,4); p[1] = p[0]; p[2] = vertex;

26 Explicit initialization  Syntax ElementType [] id = { exp 0, 1,... exp n }; id references an array of n elements. id[0] has value exp 0, id[1] has value exp 1, and so on. Each exp i is an expression that evaluates to type ElementType

27 Explicit initialization  Example String[] puppy = { “pika“, “arlo“, “shadow", “lucia" }; int[] unit = { 1 };  Equivalent to String[] puppy = new String[4]; puppy[0] = “pika"; puppy[1] = “arlo"; puppy[2] = “shadow"; puppy[4] = “lucia"; int[] unit = new int[1]; unit[0] = 1;

28 Empty set: an example

29 Array members  Member length Size of the array for (int i = 0; i < puppy.length; ++i) { System.out.println(puppy[i]); }

30 Array members  Member clone() Produces a shallow copy Point[] u = { new Point(0, 0), new Point(1, 1)}; Point[] v = u.clone(); v[1] = new Point(4, 30);

31 Array members  Member clone() Produces a shallow copy Point[] u = { new Point(0, 0), new Point(1, 1)}; Point[] v = u.clone(); v[1] = new Point(4, 30);

32 Array members  Member clone() Produces a shallow copy Point[] u = { new Point(0, 0), new Point(1, 1)}; Point[] v = u.clone(); v[1] = new Point(4, 30);

33 Array members  Member clone() Produces a shallow copy Point[] u = { new Point(0, 0), new Point(1, 1)}; Point[] v = u.clone(); v[1] = new Point(4, 30);

34 Making a deep copy  Example Point[] w = new Point[u.length]; for (int i = 0; i < u.length; ++i) { w[i] = (Point) u[i].clone(); }

35 Making a deep copy

36 End of lecture on 10 November 2004

37 Searching for a value System.out.println("Enter search value (number): "); int key = stdin.nextInt(); int i; for (i = 0; i < data.length; ++i) { if (key == data[i]) { break; } } if (i != data.length) { System.out.println(key + " is the " + I + "-th element"); } else { System.out.println(key + " is not in the list"); }

38 Searching for a value System.out.println("Enter search value (number): "); int key = stdin.nextInt(); int i; for (i = 0; i < data.length; ++i) { if (key == data[i]) { break; } if (i != data.length) { System.out.println(key + " is the " + I + "-th element"); } else { System.out.println(key + " is not in the list"); }

39 Searching for a value System.out.println("Enter search value (number): "); int key = stdin.nextInt(); int i; for (i = 0; i < data.length; ++i) { if (key == data[i]) { break; } if (i != data.length) { System.out.println(key + " is the " + I + "-th element"); } else { System.out.println(key + " is not in the list"); }

40 Searching for a value System.out.println("Enter search value (number): "); int key = stdin.nextInt(); int i; for (i = 0; i < data.length; ++i) { if (key == data[i]) { break; } if (i != data.length) { System.out.println(key + " is the " + I + "-th element"); } else { System.out.println(key + " is not in the list"); }

41 Searching for a value System.out.println("Enter search value (number): "); int key = stdin.nextInt(); int i; for (i = 0; i < data.length; ++i) { if (key == data[i]) { break; } if (i != data.length) { System.out.println(key + " is the " + I + "-th element"); } else { System.out.println(key + " is not in the list"); }

42 Searching for a value System.out.println("Enter search value (number): "); int key = stdin.nextInt(); int i; for (i = 0; i < data.length; ++i) { if (key == data[i]) { break; } if (i != data.length) { System.out.println(key + " is the " + I + "-th element"); } else { System.out.println(key + " is not in the list"); }

43 Searching for a value System.out.println("Enter search value (number): "); int key = stdin.nextInt(); int i; for (i = 0; i < data.length; ++i) { if (key == data[i]) { break; } if (i != data.length) { System.out.println(key + " is the " + I + "-th element"); } else { System.out.println(key + " is not in the list"); }

44 Searching for a value System.out.println("Enter search value (number): "); int key = stdin.nextInt(); int i; for (i = 0; i < data.length; ++i) { if (key == data[i]) { break; } if (i != data.length) { System.out.println(key + " is the " + I + "-th element"); } else { System.out.println(key + " is not in the list"); }

45 Searching for a value System.out.println("Enter search value (number): "); int key = stdin.nextInt(); int i; for (i = 0; i < data.length; ++i) { if (key == data[i]) { break; } if (i != data.length) { System.out.println(key + " is the " + I + "-th element"); } else { System.out.println(key + " is not in the list"); }

46 Searching for a value System.out.println("Enter search value (number): "); int key = stdin.nextInt(); int i; for (i = 0; i < data.length; ++i) { if (key == data[i]) { break; } if (i != data.length) { System.out.println(key + " is the " + I + "-th element"); } else { System.out.println(key + " is not in the list"); }

47 Searching for a value System.out.println("Enter search value (number): "); int key = stdin.nextInt(); int i; for (i = 0; i < data.length; ++i) { if (key == data[i]) { break; } if (i != data.length) { System.out.println(key + " is the " + I + "-th element"); } else { System.out.println(key + " is not in the list"); }

48 Searching for a value System.out.println("Enter search value (number): "); int key = stdin.nextInt(); int i; for (i = 0; i < data.length; ++i) { if (key == data[i]) { break; } if (i != data.length) { System.out.println(key + " is the " + I + "-th element"); } else { System.out.println(key + " is not in the list"); }

49 Searching for a value System.out.println("Enter search value (number): "); int key = stdin.nextInt(); int i; for (i = 0; i < data.length; ++i) { if (key == data[i]) { break; } if (i != data.length) { System.out.println(key + " is the " + I + "-th element"); } else { System.out.println(key + " is not in the list"); }

50 Searching for the minimum value  Segment int minimumSoFar = sample[0]; for (int i = 1; i < sample.length; ++i) { if (sample[i] < minimumSoFar) { minimumSoFar = sample[i]; }

51 ArrayTools.java method sequentialSearch() public static int sequentialSearch(int[] data, int key) { for (int i = 0; i < data.length; ++i) { if (data[i] == key) { return i; } } return -1; }  Consider int[] score = { 6, 9, 82, 11, 29, 85, 11, 28, 91 }; int i1 = sequentialSearch(score, 11); int i2 = sequentialSearch(score, 30);

52 ArrayTools.java method putList() public static void putList(int[] data) { for (int i = 0; i < data.length; ++i) { System.out.println(data[i]); } }  Consider int[] score = { 6, 9, 82, 11, 29, 85, 11, 28, 91 }; putList(score);

public static int[] getList() { Scanner stdin = new Scanner (System.in); int[] buffer = new int[MAX_LIST_SIZE]; int listSize = 0; for (int i = 0; i < MAX_LIST_SIZE; ++i) { String v = stdin.nextLine(); if (v != null) { int number = Integer.parseInt(v); buffer[i] = number; ++listSize; } else { break; } } int[] data = new int[listSize]; for (int i = 0; i < listSize; ++i) { data[i] = buffer[i]; } return data; } ArrayTools.java method getList()

54 ArrayTools.java – outline  In java.util public class ArrayTools { // class constant private static final int MAX_LIST_SIZE = 1000; // sequentialSearch(): examine unsorted list for key public static int binarySearch(int[] data, int key) {... // valueOf(): produces a string representation public static void putList(int[] data) {... // getList(): extract and return up to MAX_LIST_SIZE values public static int[] getList() throws IOException {... // reverse(): reverses the order of the element values public static void reverse(int[] list) {... // binarySearch(): examine sorted list for a key public static int binarySearch(char[] data, char key) {... }

55 Demo.java import java.io.*; public class Demo { // main(): application entry point public static void main(String[] args) throws IOException { System.out.println(""); System.out.println("Enter list of integers:"); int[] numbers = ArrayTools.getList(); System.out.println(""); System.out.println("Your list"); ArrayTools.putList(numbers); ArrayTools.reverse(numbers); System.out.println(""); System.out.println("Your list in reverse"); ArrayTools.putList(numbers); System.out.println(); } }

57 Microsoft and patents ……

58 Sorting  Problem Arranging elements so that they are ordered according to some desired scheme  Standard is non-decreasing order Why don't we say increasing order?  Major tasks Comparisons of elements Updates or element movement

59 Selection sorting  Algorithm basis On iteration i, a selection sorting method  Finds the element containing the ith smallest value of its list v and exchanges that element with v[i]  Example – iteration 0 Swaps smallest element with v[0] This results in smallest element being in the correct place for a sorted result

60 Selection sorting  Algorithm basis On iteration i, a selection sorting method  Finds the element containing the ith smallest value of its list v and exchanges that element with v[i]  Example – iteration 0 Swaps smallest element with v[0] This results in smallest element being in the correct place for a sorted result

61 Selection sorting  Algorithm basis On iteration i, a selection sorting method  Finds the element containing the ith smallest value of its list v and exchanges that element with v[i]  Example – iteration 1 Swaps second smallest element with v[1] This results in second smallest element being in the correct place for a sorted result

62 Selection sorting  Algorithm basis On iteration i, a selection sorting method  Finds the element containing the ith smallest value of its list v and exchanges that element with v[i]  Example – iteration 1 Swaps second smallest element with v[1] This results in second smallest element being in the correct place for a sorted result

63 ArrayTools.java selection sorting public static void selectionSort(char[] v) { for (int i = 0; i < v.length-1; ++i) { // find the location of the ith smallest element int spot = i; for (int j = i+1; j < v.length; ++j) { if (v[j] < v[spot]) { // is current location ok? // update spot to index of smaller element spot = j; } } // spot is now correct, so swap elements char rmbr = v[i]; v[i] = v[spot]; v[spot] = rmbr; } }

64 Iteration i // find the location of the ith smallest element int spot = i; for (int j = i+1; j < v.length; ++j) { if (v[j] < v[spot]) // is spot ok? // update spot with index of smaller element spot = j; } // spot is now correct, swap elements v[spot] and v[0]

65 Multidimensional arrays  Many problems require information be organized as a two- dimensional or multidimensional list  Examples Matrices Graphical animation Economic forecast models Map representation Time studies of population change Microprocessor design

66 Example  Segment int[][] m = new int[3][]; m[0] = new int[4]; m[1] = new int[4]; m[2] = new int[4];  Produces When an array is created, each value is initialized!

67 Example  Alternative int[][] m = new int[3][4];  Produces

68 Multidimensional array visualization  A multi-dimensional array declaration (either one): int[][] m = new int[3][4];  Produces or

69 End of lecture on 15 November 2004

70 Example  Segment for (int r = 0; r < m.length; ++r) { for (int c = 0; c < m[r].length; ++c) { System.out.print("Enter a value: "); m[r][c] = stdin.nextInt(); }

71 Example  Segment String[][] s = new String[4][]; s[0] = new String[2]; s[1] = new String[2]; s[2] = new String[4]; s[3] = new String[3];  Produces

72 Multidimensional array visualization  Segment String[][] s = new String[4][]; s[0] = new String[2]; s[1] = new String[2]; s[2] = new String[4]; s[3] = new String[3];  Produces  Called a “ragged” array or 0 0 0

73 Example  Segment int c[][] = {{1, 2}, {3, 4}, {5, 6}, {7, 8, 9}};  Produces

74 Matrices  A two-dimensional array is sometimes known as a matrix because it resembles that mathematical concept  A matrix a with m rows and n columns is represented mathematically in the following manner

75 Matrix addition  Definition C = A + B c ij = a ij + b ij c ij is sum of the elements in the same row and column of A and B

76 Matrix addition public static double[][] add(double[][] a, double[][] b) { // determine number of rows in solution int m = a.length; // determine number of columns in solution int n = a[0].length; // create the array to hold the sum double[][] c = new double[m][n]; // compute the matrix sum row by row for (int i = 0; i < m; ++i) { // produce the current row for (int j = 0; j < n; ++j) { c[i][j] = a[i][j] + b[i][j]; } } return c; }

77 Today’s dose of demotivators