2D Arrays A multi dimensional array is one that can hold all the values above. You set them up like this: int[ ][ ] gradeTable = new int[7][5]; With the.

Slides:



Advertisements
Similar presentations
Continuation of chapter 6…. Nested while loop A while loop used within another while loop is called nested while loop. Q. An illustration to generate.
Advertisements

Introduction to Java 2 Programming Lecture 5 Array and Collections.
Chapter 10 – Arrays and ArrayLists
Two Dimensional Arrays and ArrayList
Two-Dimensional Arrays Chapter What is a two-dimensional array? A two-dimensional array has “rows” and “columns,” and can be thought of as a series.
CS324e - Elements of Graphics and Visualization A Little Java A Little Python.
Senem KUMOVA METİN CS FALL 1 ARRAYS && SORTING && STRINGS CHAPTER 6 cont.
Arrays in JAVA CS 340 Software Design Muhammad Talha.
Warm up Determine the value stored in each of the following: – gradeTable[ 0 ][ 0 ] – gradeTable[ 1 ][ 1 ] – gradeTable[ 3 ][ 4 ] – gradeTable[ 5 ][ 2.
Arrays. What is an array An array is used to store a collection of data It is a collection of variables of the same type.
Mock test review Revision of Activity Diagrams for Loops,
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.
11-1 Chapter 11 2D Arrays Asserting Java Rick Mercer.
Arrays. Example Write a program to keep track of all students’ scores on exam 1. Need a list of everyone’s score Declare 14 double variables? What about.
Multiple-Subscripted Array
One Dimensional Arrays. Declaring references to array objects How would you declare a variable somearray that is an array of ints? int[] somearray;
11-1 Chapter 11 2D Arrays Asserting Java Rick Mercer.
03/16/ What is an Array?... An array is an object that stores list of items. Each slot of an array holds an individual element. Characteristics.
By Nicholas Policelli An Introduction to Java. Basic Program Structure public class ClassName { public static void main(String[] args) { program statements.
French Territory of St. Pierre CSE 114 – Computer Science I Arrays.
Problem Solving using the Java Programming Language May 2010 Mok Heng Ngee Day 5: Arrays.
AP Comp Sci A Chapter 12 - Arrays. Ch 12 Goals: Goals: Declare and create arrays Declare and create arrays Access elements in arrays Access elements in.
Chapter 8: Collections: Arrays. 2 Objectives One-Dimensional Arrays Array Initialization The Arrays Class: Searching and Sorting Arrays as Arguments The.
BUILDING JAVA PROGRAMS CHAPTER 2 Days of For Loops Past.
Two –Dimensional Arrays Mrs. C. Furman Java Programming November 19, 2008.
October 28, 2015ICS102: For Loop1 The for-loop and Nested loops.
Computer Programming 12 Mr. Jean April 24, The plan: Video clip of the day Upcoming Quiz Sample arrays Using arrays More about arrays.
Types in Java 8 Primitive Types –byte, short, int, long –float, double –boolean –Char Also some Object types: e.g. “String” But only single items. What.
Two –Dimensional Arrays Mrs. C. Furman September 18, 2008.
OBJECTS FOR ORGANIZING DATA -- As our programs get more sophisticated, we need assistance organizing large amounts of data. : array declaration and use.
Data Structure CS 322. What is an array? Initializing arrays Accessing the values of an array Multidimensional arrays LAB#1 : Arrays.
FOR LOOP WALK THROUGH public class NestedFor { public static void main(String [] args) { for (int i = 1; i
The median again The steps of our algorithm: Read the size of the list, N. Declare and instantiate an array of integers, "list". Read the elements of list.
Spring 2008 Mark Fontenot CSE 1341 Principles of Computer Science I Note Set 7.
Get Longest Run Index (FR) public int getLongestRunIndex(int []values) { int maxRunStart = -1, maxRunLength = 1; int runStart = 0, runLength = 1; for(int.
Arrays and ArrayLists Topic 6. One Dimensional Arrays Homogeneous – all of the same type Contiguous – all elements are stored sequentially in memory For.
Lec 13 Oct 21, 02. Array Initialization in the declaration statement ► int temp[5] = {98, 87, 92, 79,85}; ► char codes[6] = { ‘s’, ’a’, ‘m’, ‘p’, ‘l’,
Lecture 07. Do not have to create an array while declaring array variable [] variable_name; int [] prime; int prime[]; Both syntaxes are equivalent No.
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.
CiS 260: App Dev I. 2 Introduction to Arrays n An array is an object that contains a collection of components (_________) of the same data type. n For.
Chapter 8 Slides from GaddisText Arrays of more than 1 dimension.
CiS 260: App Dev I. 2 Introduction to Arrays n An array is an object that contains a collection of components (_________) of the same data type. n For.
CSC 1051 – Data Structures and Algorithms I Dr. Mary-Angela Papalaskari Department of Computing Sciences Villanova University Course website:
 Introducing Arrays  Declaring Array Variables, Creating Arrays, and Initializing Arrays  Copying Arrays  Multidimensional Arrays  Search and Sorting.
Introduction to programming in java Lecture 22 Arrays – Part 2 and Assignment No. 3.
Fixed-sized collections Introduction to arrays 6.0.
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 23 Two dimensional (2D) Arrays – Part 3.
Introduction to programming in java Lecture 21 Arrays – Part 1.
Methods. Creating your own methods Java allows you to create custom methods inside its main body. public class Test { // insert your own methods right.
Chapter 8 Arrays and the ArrayList Class Multi-Dimensional Arrays.
Topic Dimensional Arrays
Two Dimensional Array Mr. Jacobs.
Fifth Lecture Arrays and Lists
Chapter-7 part3 Arrays Two-Dimensional Arrays The ArrayList Class.
Case Study 2 – Marking a Multiple-choice Test
Object Oriented Programming
Yong Choi School of Business CSU, Bakersfield
Nested Loop Review and Two-Dimensional Arrays
The for-loop and Nested loops
Chapter 8 Slides from GaddisText
CS2011 Introduction to Programming I Methods (II)
Lecture 13: Two-Dimensional Arrays
Lecture 4 2d Arrays CSE /26/2018.
1D Arrays and Lots of Brackets
Module 4 Loops and Repetition 4/7/2019 CSE 1321 Module 4.
Multi-Dimensional Arrays
Peer Instruction 4 Control Loops.
Ps Module 7 – Part II 2D Arrays and LISTS 5/26/2019 CSE 1321 Module 7.
Ps Module 7 – Part II 2D Arrays and LISTS 8/29/2019 CSE 1321 Module 7.
Presentation transcript:

2D Arrays A multi dimensional array is one that can hold all the values above. You set them up like this: int[ ][ ] gradeTable = new int[7][5]; With the example array, will the following statement work? gradeTable[7][2] = 82 ;

public class gradeExample { public static void main( String[] arg ) { // declare and construct a 2D array int[][] gradeTable = { {99, 42, 74, 83, 100}, {90, 91, 72, 88, 95}, {88, 61, 74, 89, 96}, {61, 89, 82, 98, 93}, {93, 73, 75, 78, 99}, {50, 65, 92, 87, 94}, {43, 98, 78, 56, 99} }; System.out.println("grade 0,0: " + gradeTable[0][0]); System.out.println("grade 2,4: " + gradeTable[2][4]); gradeTable[5][3] = 99 ; int sum = gradeTable[0][1] + gradeTable[0][2] ; System.out.println( "sum: " + sum ); }

More Important Stuff: Rows are numbered from 0 to N-1, where N is the number of rows Columns are numbered from 0 to M-1, where M is the number of columns. A 2D array with N rows and M columns per row will have N times M number of cells. To find the number of rows in an arrary: int numRows = gradeTable.length; To find the number of columns in a particular row you must access the row first int numCols = grade.Table[0].length;

Printing 2D Arrays To print a 2D array you must loop through the row then the columns or you could loop through a column then the row. for ( int row=0; row < gradeTable.length; row++ ) { System.out.print("Row " + row + ": "); for ( int col=0; col < gradeTable[row].length; col++ ) System.out.print( gradeTable[row][col] + " "); System.out.println(); }

Different lengths public class UnevenExample2 { public static void main( String[] arg ) { // declare and construct a 2D array int[][] uneven = { { 1, 9, 4 }, { 0, 2}, { 0, 1, 2, 3, 4 } }; System.out.println("Length is: " + uneven.length ); for(int row =0; row < uneven.length; row++){ System.out.println("Row: " + row + " Column Length is: " + uneven[row].length); } To access the column length: uneven[0].length; uneven[1].length; uneven[2].length;

Tutorial h49C_1.html h49C_1.html Go through the tutorial on 2D Arrays Observe the programs – GradeExample.java – UnevenExample.java – Take the quiz: /chap49C/chap49Cquiz.html /chap49C/chap49Cquiz.html

2D Arrays: PowerPoint: Java Programs: GradeExample.java UnevenExample2.javaGradeExample.javaUnevenExample2.java Go through the Tutorial on 2D Arrays at Chortle Website. Take the Quiz: ArrayList Practicehttp://practiceit.cs.washington.edu/ Do the following: Choose any 6: 3 of the problems cannot be multiple choice. Must type the method. University of Washington CSE 143 (CS2)  ArrayList (9) ArrayList maxLength (id #708)maxLength range (id #709)range minToFront (id #710)minToFront removeEvenLength (id #711)removeEvenLength stutter (id #712)stutter removeShorterStrings (id #713)removeShorterStrings switchPairs (id #714)switchPairs removeDuplicates (id #715)removeDuplicates markLength4 (id #716)markLength4  ArrayIntList (9) ArrayIntList removeAll (id #717)removeAll printInversions (id #718)printInversions maxCount (id #719)maxCount mirror (id #720)mirror stretch (id #721)stretch longestSortedSequence (id #722)longestSortedSequence runningTotal (id #723)runningTotal isPairwiseSorted (id #724)isPairwiseSorted removeFront (id #725)removeFront