Ps Module 7 – Part II 2D Arrays and LISTS 5/26/2019 CSE 1321 Module 7.

Slides:



Advertisements
Similar presentations
Why not just use Arrays? Java ArrayLists.
Advertisements

Two Dimensional Arrays and ArrayList
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.
An Array-Based Implementation of the ADT List public class ListArrayBased implements ListInterface { private static final int MAX_LIST = 50; private Object.
Chapter 7 – Arrays.
Java: How to Program Arrays and ArrayLists Summary Yingcai Xiao.
Arrays. A group of data with same type stored under one variable. It is assumed that elements in that group are ordered in series. In C# language arrays.
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.
Building Java Programs Chapter 7.5
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.
ARRAYLIST.. Hazen High School. Vocabulary to Know ArrayList Generic Class ArrayList Operations ArrayList Methods ArrayList Searching For-Each Wrapper.
French Territory of St. Pierre CSE 114 – Computer Science I Arrays.
ArrayList, Multidimensional Arrays
Arrays Part 9 dbg. Arrays An array is a fixed number of contiguous memory locations, all containing data of the same type, identified by one variable.
Java SE 8 for Programmers, Third Edition
ARRAYS Computer Engineering Department Java Course Asst. Prof. Dr. Ahmet Sayar Kocaeli University - Fall
OBJECTS FOR ORGANIZING DATA -- As our programs get more sophisticated, we need assistance organizing large amounts of data. : array declaration and use.
Chapter overview This chapter focuses on Array declaration and use Bounds checking and capacity Arrays storing object references Variable length parameter.
ArrayList Class An ArrayList is an object that contains a sequence of elements that are ordered by position. An ArrayList is an object that contains a.
Aug 9, CMSC 202 ArrayList. Aug 9, What’s an Array List ArrayList is  a class in the standard Java libraries that can hold any type of object.
Dynamic Array. An Array-Based Implementation - Summary Good things:  Fast, random access of elements  Very memory efficient, very little memory is required.
Arrays and ArrayLists Topic 6. One Dimensional Arrays Homogeneous – all of the same type Contiguous – all elements are stored sequentially in memory For.
CSE 1201 Object Oriented Programming ArrayList 1.
COMP More About Arrays Yi Hong June 05, 2015.
Chapter 8 Slides from GaddisText Arrays of more than 1 dimension.
CS 116: Object Oriented Programming II. Topics Vectors Multidimensional Arrays ArrayList.
Arrays (part 2) 1 -Based on slides from Deitel & Associates, Inc. - Revised by T. A. Yang.
VISUAL C++ PROGRAMMING: CONCEPTS AND PROJECTS Chapter 7A Arrays (Concepts)
Chapter 9 Introduction to Arrays Fundamentals of Java.
An Array-Based Implementation of the ADT List
Lecture 18: Nested Loops and Two-Dimensional Arrays
CMSC 202 ArrayList Aug 9, 2007.
Sixth Lecture ArrayList Abstract Class and Interface
Write code to prompt for 5 grades, read them in, print “Thank you”, then reprint the 5 grades and their average. System.out.println(“Please enter grade.
Two Dimensional Array Mr. Jacobs.
Lecture 5 D&D Chapter 6 Arrays and ArrayLists Date.
Array, Strings and Vectors
Inheritance & Polymorphism
ARRAYLIST AND VECTOR.
Chapter-7 part3 Arrays Two-Dimensional Arrays The ArrayList Class.
Nested Loop Review and Two-Dimensional Arrays
Can store many of the same kind of data together
An Introduction to Java – Part I, language basics
Chapter 8 Slides from GaddisText
Arrays We often want to organize objects or primitive data in a way that makes them easy to access and change. An array is simple but powerful way to.
CMSC 202 ArrayList Aug 9, 2007.
ArrayLists.
Data Structures and Database Applications Arrays And Lists
Can store many of the same kind of data together
CMSC 202 ArrayList Aug 9, 2007.
CS2011 Introduction to Programming I Arrays (I)
Lecture 13: Two-Dimensional Arrays
Lecture 4 2d Arrays CSE /26/2018.
slides created by Ethan Apter
OBJECT ORIENTED PROGRAMMING II LECTURE 13_2 GEORGE KOUTSOGIANNAKIS
Can store many of the same kind of data together
1D Arrays and Lots of Brackets
1D Arrays and Lots of Brackets
1D Arrays and Lots of Brackets
Arrays in Java.
Lecture 14 2D Arrays Richard Gesick.
Dr. Sampath Jayarathna Cal Poly Pomona
EET 2259 Unit 9 Arrays Read Bishop, Sections 6.1 to 6.3.
Lecture 6: References and linked nodes reading: 16.1
1D Arrays and Lots of Brackets
CSCI 3328 Object Oriented Programming in C# Chapter 8: LINQ and Generic Collections – Exercises UTPA – Fall 2012 This set of slides is revised from lecture.
CS 200 Objects and ArrayList
1D Arrays and Lots of Brackets
Ps Module 7 – Part II 2D Arrays and LISTS 8/29/2019 CSE 1321 Module 7.
Arrays.
Presentation transcript:

Ps Module 7 – Part II 2D Arrays and LISTS 5/26/2019 CSE 1321 Module 7

2D vs 1D Arrays Still hold several values of the same type (homogeneous) Still based on a slot number (the index number), but has two now Still have instant access Still Static Typically use nested loops to traverse array 5/26/2019 CSE 1321

What 2D Arrays look like . . . . . . 1 2 3 4 5 6 myArray 1 2 3 4 5 6 1 2 3 4 5 6 [0,0] [0,1] [0,2] [0,3] [0,4] [0,5] [0,6] myArray 1 [1,0] [1,1] [1,2] [1,3] [1,4] [1,5] [1,6] 2 [2,0] [2,1] [2,2] [2,3] [2,4] [2,5] [2,6] . . . 3 [3,0] [3,1] [3,2] [3,3] [3,4] [3,5] [3,6] 4 [4,0] [4,1] [4,2] [4,3] [4,4] [4,5] [4,6] 5 [5,0] [5,1] [5,2] [5,3] [5,4] [5,5] [5,6] 6 [6,0] [6,1] [6,2] [6,3] [6,4] [6,5] [6,6] . . . 5/26/2019 CSE 1321

Ps Defining a 2D array CREATE array nums [numRows][numColumns] 5/26/2019 CSE 1321

Java- Define a 2D Array <type> [ ][ ] <name> = new <type>[<rowSize>][<columnSize>]; For example: int[ ][ ] grid = new int [10][20]; 5/26/2019 CSE 1321

C# - Define a 2D Array <type>[ , ]<name> = new <type>[<row_size>,<column_size>]; For example: int[ , ] grid = new int [10,20]; 5/26/2019 CSE 1321

Python – Define a 2D Array In Python, 2D Arrays must be initialized when they are created. numbers = [[1,2,3,4,5], [6,7,8,9,10], [11,12,13,14,15], [16,17,18,19,20]] 5/26/2019 CSE 1321

Modifying a 2D Array myArray[2,3] = 42; 1 2 3 4 5 6 myArray 1 2 3 4 5 1 2 3 4 5 6 [0,0] [0,1] [0,2] [0,3] [0,4] [0,5] [0,6] myArray 1 [1,0] [1,1] [1,2] [1,3] [1,4] [1,5] [1,6] 2 [2,0] [2,1] [2,2] [2,3] [2,4] [2,5] [2,6] 3 [3,0] [3,1] [3,2] [3,3] [3,4] [3,5] [3,6] 4 [4,0] [4,1] [4,2] [4,3] [4,4] [4,5] [4,6] 5 [5,0] [5,1] [5,2] [5,3] [5,4] [5,5] [5,6] 6 [6,0] [6,1] [6,2] [6,3] [6,4] [6,5] [6,6] myArray[2,3] = 42; 5/26/2019 CSE 1321

Time for a secret… You use nested loops any time you have to visit every element Find the min Find the max Find the average Print every element etc. It sounds like a test question… Let’s make a multiplication table

CREATE ARRAY grid [4][4]

CREATE ARRAY grid [4][4] 4 4

CREATE ARRAY grid [4][4] How do we initialize this?

FOR each element in a row CREATE ARRAY grid [4][4] FOR each element in a row END FOR row

FOR each element in a row FOR each element in a col CREATE ARRAY grid [4][4] FOR each element in a row FOR each element in a col grid[row, col] = row * col END FOR col row

FOR each element in a row FOR each element in a col CREATE ARRAY grid [4][4] FOR each element in a row FOR each element in a col grid[row, col] = row * col END FOR col col row row

FOR each element in a row FOR each element in a col CREATE ARRAY grid [4][4] FOR each element in a row FOR each element in a col grid[row, col] = row * col END FOR col col row 1 row

FOR each element in a row FOR each element in a col CREATE ARRAY grid [4][4] FOR each element in a row FOR each element in a col grid[row, col] = row * col END FOR col col row 1 row

FOR each element in a row FOR each element in a col CREATE ARRAY grid [4][4] FOR each element in a row FOR each element in a col grid[row, col] = row * col END FOR col col row 2 row

FOR each element in a row FOR each element in a col CREATE ARRAY grid [4][4] FOR each element in a row FOR each element in a col grid[row, col] = row * col END FOR col col row 2 row

FOR each element in a row FOR each element in a col CREATE ARRAY grid [4][4] FOR each element in a row FOR each element in a col grid[row, col] = row * col END FOR col col row 3 row

FOR each element in a row FOR each element in a col CREATE ARRAY grid [4][4] FOR each element in a row FOR each element in a col grid[row, col] = row * col END FOR col col row 3 row

FOR each element in a row FOR each element in a col CREATE ARRAY grid [4][4] FOR each element in a row FOR each element in a col grid[row, col] = row * col END FOR col row 4 row

FOR each element in a row FOR each element in a col CREATE ARRAY grid [4][4] FOR each element in a row FOR each element in a col grid[row, col] = row * col END FOR row 1 row

FOR each element in a row FOR each element in a col CREATE ARRAY grid [4][4] FOR each element in a row FOR each element in a col grid[row, col] = row * col END FOR col col row 1 row

FOR each element in a row FOR each element in a col CREATE ARRAY grid [4][4] FOR each element in a row FOR each element in a col grid[row, col] = row * col END FOR col col row 1 row

FOR each element in a row FOR each element in a col CREATE ARRAY grid [4][4] FOR each element in a row FOR each element in a col grid[row, col] = row * col END FOR col col row 1 1 row

FOR each element in a row FOR each element in a col CREATE ARRAY grid [4][4] FOR each element in a row FOR each element in a col grid[row, col] = row * col END FOR col col row 1 1 row 1

FOR each element in a row FOR each element in a col CREATE ARRAY grid [4][4] FOR each element in a row FOR each element in a col grid[row, col] = row * col END FOR col col row 2 1 row 1

FOR each element in a row FOR each element in a col CREATE ARRAY grid [4][4] FOR each element in a row FOR each element in a col grid[row, col] = row * col END FOR col col row 2 1 row 1 2

FOR each element in a row FOR each element in a col CREATE ARRAY grid [4][4] FOR each element in a row FOR each element in a col grid[row, col] = row * col END FOR col col row 3 1 row 1 2

FOR each element in a row FOR each element in a col CREATE ARRAY grid [4][4] FOR each element in a row FOR each element in a col grid[row, col] = row * col END FOR col col row 3 1 row 1 2 3

FOR each element in a row FOR each element in a col CREATE ARRAY grid [4][4] FOR each element in a row FOR each element in a col grid[row, col] = row * col END FOR col row 4 1 row 1 2 3

FOR each element in a row FOR each element in a col CREATE ARRAY grid [4][4] FOR each element in a row FOR each element in a col grid[row, col] = row * col END FOR row 2 1 2 3 row

FOR each element in a row FOR each element in a col CREATE ARRAY grid [4][4] FOR each element in a row FOR each element in a col grid[row, col] = row * col END FOR col col row 2 1 2 3 row

FOR each element in a row FOR each element in a col CREATE ARRAY grid [4][4] FOR each element in a row FOR each element in a col grid[row, col] = row * col END FOR col col row 2 1 2 3 row

FOR each element in a row FOR each element in a col CREATE ARRAY grid [4][4] FOR each element in a row FOR each element in a col grid[row, col] = row * col END FOR col col row 1 2 1 2 3 row

FOR each element in a row FOR each element in a col CREATE ARRAY grid [4][4] FOR each element in a row FOR each element in a col grid[row, col] = row * col END FOR col col row 1 2 1 2 3 row 2

FOR each element in a row FOR each element in a col CREATE ARRAY grid [4][4] FOR each element in a row FOR each element in a col grid[row, col] = row * col END FOR col col row 2 2 1 2 3 row 2

FOR each element in a row FOR each element in a col CREATE ARRAY grid [4][4] FOR each element in a row FOR each element in a col grid[row, col] = row * col END FOR col col row 2 2 1 2 3 row 2 4

FOR each element in a row FOR each element in a col CREATE ARRAY grid [4][4] FOR each element in a row FOR each element in a col grid[row, col] = row * col END FOR col col row 3 2 1 2 3 row 2 4

FOR each element in a row FOR each element in a col CREATE ARRAY grid [4][4] FOR each element in a row FOR each element in a col grid[row, col] = row * col END FOR col col row 3 2 1 2 3 row 2 4 6

FOR each element in a row FOR each element in a col CREATE ARRAY grid [4][4] FOR each element in a row FOR each element in a col grid[row, col] = row * col END FOR col row 4 2 1 2 3 row 2 4 6

FOR each element in a row FOR each element in a col CREATE ARRAY grid [4][4] FOR each element in a row FOR each element in a col grid[row, col] = row * col END FOR row 3 1 2 3 2 4 6 row

FOR each element in a row FOR each element in a col CREATE ARRAY grid [4][4] FOR each element in a row FOR each element in a col grid[row, col] = row * col END FOR col col row 3 1 2 3 2 4 6 row

FOR each element in a row FOR each element in a col CREATE ARRAY grid [4][4] FOR each element in a row FOR each element in a col grid[row, col] = row * col END FOR col col row 3 1 2 3 2 4 6 row

FOR each element in a row FOR each element in a col CREATE ARRAY grid [4][4] FOR each element in a row FOR each element in a col grid[row, col] = row * col END FOR col col row 1 3 1 2 3 2 4 6 row

FOR each element in a row FOR each element in a col CREATE ARRAY grid [4][4] FOR each element in a row FOR each element in a col grid[row, col] = row * col END FOR col col row 1 3 1 2 3 2 4 6 row 3

FOR each element in a row FOR each element in a col CREATE ARRAY grid [4][4] FOR each element in a row FOR each element in a col grid[row, col] = row * col END FOR col col row 2 3 1 2 3 2 4 6 row 3

FOR each element in a row FOR each element in a col CREATE ARRAY grid [4][4] FOR each element in a row FOR each element in a col grid[row, col] = row * col END FOR col col row 2 3 1 2 3 2 4 6 row 3 6

FOR each element in a row FOR each element in a col CREATE ARRAY grid [4][4] FOR each element in a row FOR each element in a col grid[row, col] = row * col END FOR col col row 3 3 1 2 3 2 4 6 row 3 6

FOR each element in a row FOR each element in a col CREATE ARRAY grid [4][4] FOR each element in a row FOR each element in a col grid[row, col] = row * col END FOR col col row 3 3 1 2 3 2 4 6 row 3 6 9

FOR each element in a row FOR each element in a col CREATE ARRAY grid [4][4] FOR each element in a row FOR each element in a col grid[row, col] = row * col END FOR col row 4 3 1 2 3 2 4 6 row 3 6 9

FOR each element in a row FOR each element in a col CREATE ARRAY grid [4][4] FOR each element in a row FOR each element in a col grid[row, col] = row * col END FOR row 4 1 2 3 2 4 6 3 6 9

CREATE ARRAY grid [4][4] FOR each element in a row FOR each element in a col grid[row, col] = row * col END FOR // Continue processing 1 2 3 2 4 6 3 6 9

Back to your regular lecture… 5/26/2019 CSE 1321 Module 4

Working with 2D arrays Usually involves nested loops, as shown below. Problem Statement: Create an array of 4 rows and 5 columns. Populate the array with the numbers 1-20. --------------------------------------------------------------------- Create array grid[4][5] count ← 1 FOR each element in a row FOR each element in a column     grid[row][col] = count count ← count + 1 END INNER FOR END FOR 5/26/2019 CSE 1321

Working with 2D arrays class Main { public static void main(String[] args) { int count = 1; //declare a 2D array with 4 rows and 5 columns int[][] grid = new int[4][5]; //getting the rows in the array for (int row = 0; row < 4; row++) { //populate the values in the array (row) we're currently on from the outer loop for (int column = 0; column < 5; column++){ grid[row][column] = count; count++; } //for each array in the grid (represented by rows) for(int item[] : grid) //for each element in the array (row) we're currently on from the outer loop for (int number : item) { System.out.print(number + ","); if(number % 5 == 0) { System.out.println(" "); } 5/26/2019 CSE 1321

Working with 2D arrays using System; class MainClass { public static void Main (string[] args) { int count = 1; //declare a 2D array with 4 rows and 5 columns int[,] grid = new int[4, 5]; //getting the rows in the array for (int row = 0; row < 4; row++) { //populate the values in the array (row) we're currently on from the outer loop for (int column = 0; column < 5; column++) grid[row, column] = count; count++; } //for each array in the grid (represented by rows) foreach(int number in grid) //for each element in the array (row) we're currently on Console.Write(number + ","); if(number % 5 == 0) { Console.WriteLine(" "); } 5/26/2019 CSE 1321

Working with 2D arrays from array import * numbers = [[1,2,3,4,5], [6,7,8,9,10], [11,12,13,14,15], [16,17,18,19,20]] for r in numbers: for c in r: print(c, end = " ") print() #what value does this line print? print(numbers[3][4]) 5/26/2019 CSE 1321

List Basics Most languages provide dynamic structures that can be used to store data. C# and Java provide the ArrayList, while Python provides the List. Following are a few of the differences between the two: Array List Array Can increase and decrease in size dynamically; auto-resize slows performance Capacity is fixed Can have only one dimension Can have multiple dimensions Provides methods to add, insert, or remove a range Can work with only one element at a time Elements of ArrayList are of type Object; therefore, boxing and unboxing typically occur when you store or retrieve a value type Can store primitives or objects Length is provided by the size() method Array uses the length field to determine size Add elements is accomplished through add method Can add elements using the index and assignment operator References to objects are contiguous, but objects are not Elements are stored contiguously 5/26/2019 CSE 1321

Working with Array Lists import java.util.ArrayList; class Main { public static void main(String[] args) { //declare variables int n = 2; //create list ArrayList<String> colors = new ArrayList<String>(n); //add colors colors.add("purple"); colors.add("pink"); colors.add("green"); System.out.println("Does the arraylist contain the color pink?"); if (colors.contains("pink")) { System.out.println("Yes"); System.out.println("\n"); } else { System.out.println("No"); } 5/26/2019 CSE 1321

Working with Array Lists CONTINUED FROM PREVIOUS SLIDE //indexOf returns the position of the argument System.out.println("Which position is pink in?"); System.out.println(colors.indexOf("pink")); System.out.println("\n"); //remove removes the argument from the list colors.remove("purple"); //get() references the position in the arraylist System.out.println("The item in index 1 of the list is:"); System.out.println(colors.get(1)); //items are stored as objects and can be referenced as such System.out.println("The Colors Arraylist:"); for(Object o : colors) { System.out.println(o); } 5/26/2019 CSE 1321

Working with Array Lists using System; using System.Collections; class MainClass { public static void Main (string[] args) { //create list ArrayList colors = new ArrayList(); //add colors colors.Add("purple"); colors.Add("pink"); colors.Add("green"); //contains returns a boolean if list contains argument Console.WriteLine("Does the arraylist contain the color pink?"); if (colors.Contains("pink")) { Console.WriteLine("Yes"); Console.WriteLine("\n"); } else { Console.WriteLine("No"); } 5/26/2019 CSE 1321

C# - Working with Array Lists CONTINUED FROM PREVIOUS SLIDE //indexOf returns the position of the argument Console.WriteLine("Which position is pink in?"); Console.WriteLine(colors.IndexOf("pink")); Console.WriteLine("\n"); //remove removes the argument from the list colors.Remove("purple"); //can reference by position in the array Console.WriteLine("The item in index 1 of the list is:"); Console.WriteLine(colors[1]); //items are stored as objects and can be referenced as such Console.WriteLine("The Colors Arraylist:"); foreach (Object o in colors) { Console.WriteLine(o); } 5/26/2019 CSE 1321

Python - Working with Lists colors = list() #add colors colors.append('purple') colors.append('pink') colors.append('green') #find if list contains an element if('pink' in colors): print('pink is in the list') print('\n') #show the index of pink print(colors.index('pink')) #remove a color colors.remove('green') #output list for x in range(len(colors)): print (colors[x]) 5/26/2019 CSE 1321

Summary 2D Arrays Lists Are like 1D arrays in nature Use nested loops Have only 1 dimension Are dynamic: They can grow and shrink Come with a host of function (add, delete, etc) 5/26/2019 CSE 1321