Download presentation
Presentation is loading. Please wait.
Published byΆργος Δάβης Modified over 5 years ago
1
Ps Module 7 – Part II 2D Arrays and LISTS 5/26/2019 CSE 1321 Module 7
2
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
3
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
4
Ps Defining a 2D array CREATE array nums [numRows][numColumns]
5/26/2019 CSE 1321
5
Java- Define a 2D Array <type> [ ][ ] <name> = new <type>[<rowSize>][<columnSize>]; For example: int[ ][ ] grid = new int [10][20]; 5/26/2019 CSE 1321
6
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
7
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
8
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
9
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
10
CREATE ARRAY grid [4][4]
11
CREATE ARRAY grid [4][4] 4 4
12
CREATE ARRAY grid [4][4] How do we initialize this?
13
FOR each element in a row
CREATE ARRAY grid [4][4] FOR each element in a row END FOR row
14
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
15
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
16
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
17
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
18
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
19
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
20
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
21
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
22
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
23
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
24
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
25
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
26
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
27
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
28
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
29
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
30
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
31
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
32
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
33
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
34
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
35
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
36
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
37
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
38
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
39
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
40
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
41
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
42
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
43
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
44
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
45
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
46
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
47
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
48
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
49
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
50
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
51
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
52
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
53
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
54
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
55
Back to your regular lecture…
5/26/2019 CSE 1321 Module 4
56
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
57
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
58
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
59
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
60
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
61
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
62
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
63
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
64
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
65
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
66
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
Similar presentations
© 2024 SlidePlayer.com. Inc.
All rights reserved.