+ All Categories
Home > Documents > ArrayList and 2-D Array - Ascende Learning · 2018-12-19 · Initialize two empty ArrayLists to...

ArrayList and 2-D Array - Ascende Learning · 2018-12-19 · Initialize two empty ArrayLists to...

Date post: 06-May-2020
Category:
Upload: others
View: 15 times
Download: 0 times
Share this document with a friend
16
© 2017 Ascende Learning/Leana Wen All Rights Reserved www.ascendelearning.com ArrayList and 2-D Array Ascende Learning Leana Wen
Transcript
Page 1: ArrayList and 2-D Array - Ascende Learning · 2018-12-19 · Initialize two empty ArrayLists to hold all qualified clues’s beginning positions X , and Y, respectively Loop through

© 2017 Ascende Learning/Leana Wen All Rights Reserved www.ascendelearning.com

ArrayList and 2-D Array

Ascende LearningLeana Wen

Page 2: ArrayList and 2-D Array - Ascende Learning · 2018-12-19 · Initialize two empty ArrayLists to hold all qualified clues’s beginning positions X , and Y, respectively Loop through

© 2017 Ascende Learning/Leana Wen All Rights Reserved www.ascendelearning.com

2-D Array2-Dimensional Array

Each element itself is an array : int [ ] // ← each element

● Declare a 2-D array : int [ ] [ ] arr5; ● Initialize a 2-D array :

○ arr1 = new int [3] [4]; // arr5 will contain 3 arrays as its elements○ int [ ] [ ] arr2 = { {1,2,3,4}, {5,6,7,8}, {9,10,11,12} }

● Also often called Matrix● Length of 2-D array: # of rows

Page 3: ArrayList and 2-D Array - Ascende Learning · 2018-12-19 · Initialize two empty ArrayLists to hold all qualified clues’s beginning positions X , and Y, respectively Loop through

© 2017 Ascende Learning/Leana Wen All Rights Reserved www.ascendelearning.com

MatrixExample: int [ ] [ ] arr = { {1,2,3,4}, {5,6,7,8}, {9,10,11,12} }

● Commonly represented as a matrix (2-D grid)● Length of 2-D array : num of rows

● Rows and Columns

1 2 3 4

5 6 7 8

9 10 11 12

Page 4: ArrayList and 2-D Array - Ascende Learning · 2018-12-19 · Initialize two empty ArrayLists to hold all qualified clues’s beginning positions X , and Y, respectively Loop through

© 2017 Ascende Learning/Leana Wen All Rights Reserved www.ascendelearning.com

MatrixExample: int [ ] [ ] arr = { {1,2,3,4}, {5,6,7,8}, {9,10,11,12} }

What are the values of the following?

● arr [1]● arr [0][2]● arr [2][3]● arr.length● arr[2].length

1 2 3 4

5 6 7 8

9 10 11 12

Page 5: ArrayList and 2-D Array - Ascende Learning · 2018-12-19 · Initialize two empty ArrayLists to hold all qualified clues’s beginning positions X , and Y, respectively Loop through

© 2017 Ascende Learning/Leana Wen All Rights Reserved www.ascendelearning.com

TraversalExample: int [ ] [ ] arr = { {1,2,3,4}, {5,6,7,8}, {9,10,11,12} }

● Traversal○ Iterate through all elements

● Usually involves nested loop (2-D)

for (int i=0; i<arr.length; i++)for (int j=0; j<arr[0].length; j++)

System.out.print (arr [i] [j]);

1 2 3 4

5 6 7 8

9 10 11 12

Page 6: ArrayList and 2-D Array - Ascende Learning · 2018-12-19 · Initialize two empty ArrayLists to hold all qualified clues’s beginning positions X , and Y, respectively Loop through

© 2017 Ascende Learning/Leana Wen All Rights Reserved www.ascendelearning.com

ArrayList● Useful for a ordered list of unknown length

● Can handle duplicate elements

● Want to do append/remove easily

● Syntax:

○ ArrayList<Integer> myList = new ArrayList<>();

○ myList.add(3)

○ myList.remove(5)

○ int firstNum = myList.get(0)

○ int size = myList.size()

Page 7: ArrayList and 2-D Array - Ascende Learning · 2018-12-19 · Initialize two empty ArrayLists to hold all qualified clues’s beginning positions X , and Y, respectively Loop through

© 2017 Ascende Learning/Leana Wen All Rights Reserved www.ascendelearning.com

2014-2015 Dec Crossword

- Variable length: We do not know how many clues there are,

not ideal to use array

=> ArrayList is the ideal choice

Page 8: ArrayList and 2-D Array - Ascende Learning · 2018-12-19 · Initialize two empty ArrayLists to hold all qualified clues’s beginning positions X , and Y, respectively Loop through

© 2017 Ascende Learning/Leana Wen All Rights Reserved www.ascendelearning.com

2014-2015 Dec Crossword PseudoCode

create a N*M 2-D array of characters and take input

Initialize two empty ArrayLists to hold all qualified clues’s beginning positions X ,

and Y, respectively

Loop through all rows in the 2-d array

Loop through all columns in the 2-d array

Check each character in the cell

if it qualifies as a beginning char of a horizontal clue

put its x and y values into the two arraylists

else if it qualifies as a beginning char of a vertical clue

put its x and y values into the two arraylists

Page 9: ArrayList and 2-D Array - Ascende Learning · 2018-12-19 · Initialize two empty ArrayLists to hold all qualified clues’s beginning positions X , and Y, respectively Loop through

© 2017 Ascende Learning/Leana Wen All Rights Reserved www.ascendelearning.com

Dec Crossword Sample Solution

Page 10: ArrayList and 2-D Array - Ascende Learning · 2018-12-19 · Initialize two empty ArrayLists to hold all qualified clues’s beginning positions X , and Y, respectively Loop through

© 2017 Ascende Learning/Leana Wen All Rights Reserved www.ascendelearning.com

Automation Review ● How to toggle between submission and local automation

● How to compare test results with multiple lines

Page 11: ArrayList and 2-D Array - Ascende Learning · 2018-12-19 · Initialize two empty ArrayLists to hold all qualified clues’s beginning positions X , and Y, respectively Loop through

© 2017 Ascende Learning/Leana Wen All Rights Reserved www.ascendelearning.com

2014-2015 Feb Censoring (Bronze)

Page 12: ArrayList and 2-D Array - Ascende Learning · 2018-12-19 · Initialize two empty ArrayLists to hold all qualified clues’s beginning positions X , and Y, respectively Loop through

© 2017 Ascende Learning/Leana Wen All Rights Reserved www.ascendelearning.com

2014 - 2015 Feb Censoring (Bronze)- String processing will be too slow

- String is *Immutable*- Use ArrayList to speed up character operations

- Efficient insert/remove with ArrayList

Page 13: ArrayList and 2-D Array - Ascende Learning · 2018-12-19 · Initialize two empty ArrayLists to hold all qualified clues’s beginning positions X , and Y, respectively Loop through

© 2017 Ascende Learning/Leana Wen All Rights Reserved www.ascendelearning.com

2014-2015 Feb Censoring (Bronze)

Page 14: ArrayList and 2-D Array - Ascende Learning · 2018-12-19 · Initialize two empty ArrayLists to hold all qualified clues’s beginning positions X , and Y, respectively Loop through

© 2017 Ascende Learning/Leana Wen All Rights Reserved www.ascendelearning.com

(Optional) 2016-2017 Jan Cow Tipping- Working with a N x N grid- Define the grid: int [ ] [ ] grid = new int [N][N]- Access each position in the grid: grid[i][j]- Work our way bottom -> up and right -> left

Page 15: ArrayList and 2-D Array - Ascende Learning · 2018-12-19 · Initialize two empty ArrayLists to hold all qualified clues’s beginning positions X , and Y, respectively Loop through

© 2017 Ascende Learning/Leana Wen All Rights Reserved www.ascendelearning.com

2016-2017 Jan Cow TippingPsuodoCode

Loop over all rows from bottom -> top {Loop over all columns from right -> left {

check if the cow is 1 or 0if it is 1 then {

flip all cows in the following range:row [0..current row] and column [0..current column]

increase counter}

} } output the final counter

Page 16: ArrayList and 2-D Array - Ascende Learning · 2018-12-19 · Initialize two empty ArrayLists to hold all qualified clues’s beginning positions X , and Y, respectively Loop through

© 2017 Ascende Learning/Leana Wen All Rights Reserved www.ascendelearning.com

2016-2017 Jan Cow Tipping


Recommended