1 / 8

CSC 1051 – Data Structures and Algorithms I

CSC 1051 – Data Structures and Algorithms I. Two Dimensional Arrays. Dr. Mary-Angela Papalaskari Department of Computing Sciences Villanova University Course website: www.csc.villanova.edu/~map/1051/

flavio
Download Presentation

CSC 1051 – Data Structures and Algorithms I

An Image/Link below is provided (as is) to download presentation Download Policy: Content on the Website is provided to you AS IS for your information and personal use and may not be sold / licensed / shared on other websites without getting consent from its author. Content is provided to you AS IS for your information and personal use only. Download presentation by click this link. While downloading, if for some reason you are not able to download a presentation, the publisher may have deleted the file from their server. During download, if you can't get a presentation, the file might be deleted by the publisher.

E N D

Presentation Transcript


  1. CSC 1051 – Data Structures and Algorithms I Two DimensionalArrays Dr. Mary-Angela Papalaskari Department of Computing Sciences Villanova University Course website: www.csc.villanova.edu/~map/1051/ Some slides in this presentation are adapted from the slides accompanying Java Software Solutions by Lewis & Loftus CSC 1051 M.A. Papalaskari, Villanova University

  2. one dimension two dimensions Two-Dimensional Arrays • A one-dimensional array stores a list of elements • A two-dimensional array can be thought of as a table of elements, with rows and columns CSC 1051 M.A. Papalaskari, Villanova University

  3. Two-Dimensional Arrays • To be precise, in Java a two-dimensional array is an array of arrays • A two-dimensional array is declared by specifying the size of each dimension separately: int[][] table = new int[12][50]; • A array element is referenced using two index values: value = table[3][6] • The array stored in one row can be specified using one index CSC 1051 M.A. Papalaskari, Villanova University

  4. 9.3 5.8 6.9 5.5 9.0 8.3 7.7 9.2 9.8 8.2 8.9 8.0 8.4 6.2 7.7 7.3 9.6 6.1 7.8 7.3 7.9 8.7 9.4 8.2 6.7 9.8 8.7 8.1 7.4 9.1 declaration 2D Arrays - Overview double[][] courseGrade = new double[3][10]; 2D array element courseGrade[1][4] 0 1 2 3 4 5 6 7 8 9 courseGrade 0 1 2 3 4 5 6 7 8 9 0 1 2 0 1 2 3 4 5 6 7 8 9 array element (a row) courseGrade[2] CSC 1051 M.A. Papalaskari, Villanova University

  5. Two-Dimensional Arrays • SeeTwoDArray.java • SeeSodaSurvey.java CSC 1051 M.A. Papalaskari, Villanova University

  6. //********************************************************************//******************************************************************** // TwoDArray.java Author: Lewis/Loftus // // Demonstrates the use of a two-dimensional array. //******************************************************************** public class TwoDArray { //----------------------------------------------------------------- // Creates a 2D array of integers, fills it with increasing // integer values, then prints them out. //----------------------------------------------------------------- public static void main (String[] args) { int[][] table = new int[5][10]; // Load the table with values for (introw=0; row < table.length; row++) for (intcol=0; col < table[row].length; col++) table[row][col] = row * 10 + col; // Print the table for (introw=0; row < table.length; row++) { for (int col=0; col < table[row].length; col++) System.out.print (table[row][col] + "\t"); System.out.println(); } } } CSC 1051 M.A. Papalaskari, Villanova University

  7. //********************************************************************//******************************************************************** // TwoDArray.java Author: Lewis/Loftus // // Demonstrates the use of a two-dimensional array. //******************************************************************** public class TwoDArray { //----------------------------------------------------------------- // Creates a 2D array of integers, fills it with increasing // integer values, then prints them out. //----------------------------------------------------------------- public static void main (String[] args) { int[][] table = new int[5][10]; // Load the table with values for (int row=0; row < table.length; row++) for (int col=0; col < table[row].length; col++) table[row][col] = row * 10 + col; // Print the table for (int row=0; row < table.length; row++) { for (int col=0; col < table[row].length; col++) System.out.print (table[row][col] + "\t"); System.out.println(); } } } Output 0 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 CSC 1051 M.A. Papalaskari, Villanova University

  8. Multidimensional Arrays • An array can have many dimensions – if it has more than one dimension, it is called a multidimensional array • Each dimension subdivides the previous one into the specified number of elements • Each dimension has its own length constant • Because each dimension is an array of array references, the arrays within one dimension can be of different lengths • these are sometimes called ragged arrays CSC 1051 M.A. Papalaskari, Villanova University

More Related