Presentation is loading. Please wait.

Presentation is loading. Please wait.

ARRAYS Multidimensional realities Image courtesy of

Similar presentations


Presentation on theme: "ARRAYS Multidimensional realities Image courtesy of"— Presentation transcript:

1 ARRAYS Multidimensional realities Image courtesy of http://www.flickr.com/photos/johnloo/5673232605/

2 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] Index012345678 Value183-315432920

3 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 http://www.flickr.com/photos/katerha/4484509431/sizes/z/in/photostream/

4 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

5 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 ()};

6 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

7 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

8 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

9 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));

10 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.

11 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

12 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]

13 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; } } 54322 43211 32100 3210Indices

14 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; } Indices0123 00123 11234 22345 33456

15 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]. http://www.flickr.com/photos/squeakywheel/505768192/sizes/l/in/photostream/

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

17 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; }

18 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; }

19 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; }

20 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];

21 Pop Quiz! 0 1 2 3 4 5 6 7 8 9 0101 0101 0 1 2  Write a Java declaration for each type of array.  Write a Java statement that instantiates each object.  Write an expression for each red element.


Download ppt "ARRAYS Multidimensional realities Image courtesy of"

Similar presentations


Ads by Google