© 2017 Ascende Learning/Leana Wen All Rights Reserved www.ascendelearning.com
ArrayList and 2-D Array
Ascende LearningLeana Wen
© 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
© 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
© 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
© 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
© 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()
© 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
© 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
© 2017 Ascende Learning/Leana Wen All Rights Reserved www.ascendelearning.com
Dec Crossword Sample Solution
© 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
© 2017 Ascende Learning/Leana Wen All Rights Reserved www.ascendelearning.com
2014-2015 Feb Censoring (Bronze)
© 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
© 2017 Ascende Learning/Leana Wen All Rights Reserved www.ascendelearning.com
2014-2015 Feb Censoring (Bronze)
© 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
© 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
© 2017 Ascende Learning/Leana Wen All Rights Reserved www.ascendelearning.com
2016-2017 Jan Cow Tipping