Learn 2D Arrays in Java with Practical Examples

cse 121 lesson 15 2d arrays n.w
1 / 37
Embed
Share

Explore the world of 2D arrays in Java with detailed lessons on nested data structures, array traversals, utility methods, and more. Get ready to dive deeper into array manipulation and improve your programming skills. Join us in this Autumn 2024 session for a comprehensive understanding of 2D arrays.

  • Java Programming
  • 2D Arrays
  • Array Manipulation
  • Nested Data
  • Array Traversals

Uploaded on | 0 Views


Download Presentation

Please find below an Image/Link to download the presentation.

The content on the website is provided AS IS for your information and personal use only. It may not be sold, licensed, or shared on other websites without obtaining consent from the author. If you encounter any issues during the download, it is possible that the publisher has removed the file from their server.

You are allowed to download the files provided on this website for personal or commercial use, subject to the condition that they are used lawfully. All files are the property of their respective owners.

The content on the website is provided AS IS for your information and personal use only. It may not be sold, licensed, or shared on other websites without obtaining consent from the author.

E N D

Presentation Transcript


  1. CSE 121 Lesson 15: 2D Arrays Matt Wang & Brett Wortzman Autumn 2024 Abby Afifah Ailsa Alice Aliyan Arohan Chlo Christopher Dalton Derek Elizabeth Ethan TAs: Hanna Hannah Heather Hibbah Janvi Jasmine Judy Julia Kelsey Lucas Luke Mahima Maitreyi Maria Merav Minh Neha Ronald Ruslana Sahej Sam Samrutha Sushma Vivian sli.do #cse121 Today s playlist: 121 24au lecture tunes Yijia Zachary

  2. Announcements, Reminders C3 released Wednesday, due Tuesday, November 19th R5 released yesterday, due Thursday, November 21st last chance for P1 Quiz 2 next Thursday, November 21st topics: everything up until Wednesday (i.e. not today s material) next Wednesday s PCM + class is secretly quiz prep :) practice quizzes :) Ed post on post-section work s extra resub today! more logistics on final next Friday (after Quiz 2)! Lesson 15 - Autumn 2024 2

  3. (PCM) 2D Arrays (1/3) An array of arrays! The ElementType of the array is another array itself! Your first example of nested data structures There will be more in CSE 122! String[][] int[][] double[][] char[][] boolean[][] Lesson 15 - Autumn 2024 3

  4. (PCM) 2D Arrays (2/3) An array of arrays! 0 1 2 The two dimensions are rows and columns 0 int[] a = new int[4][3]; 1 2 3 Lesson 15 - Autumn 2024 4

  5. (PCM) 2D Arrays (3/3) 0 1 2 A slightly more accurate view 0 int[] a = new int[4][3]; 1 reference semantics 2 3 Lesson 15 - Autumn 2024 5

  6. (PCM) 2D Array Traversals for each row for (int i = 0; i < list.length; i++) { for (int j = 0; j < list[i].length; j++) { // do something with list[i][j] } } for each element within a row Lesson 15 - Autumn 2024 6

  7. Arrays Utility Class Method Description Returns a String representing the array, such as "[10, 30, -25, 17]" Arrays.toString(array); Returns true if the two arrays contain the same elements in the same order Arrays.equals(array1, array2); Returns a String representing the array; if the array contains other arrays as elements, the String represents their contents, and so on. For example, "[[99, 151], [30, 5]]" Arrays.deepToString(array); Returns true if the two arrays contain the same elements in the same order; if the array(s) contain other arrays as elements, their contents are tested for equality, and so on. Arrays.deepEquals(array1, array2); Lesson 15 - Autumn 2024 7

  8. Applications of 2D Arrays Matrices Useful in various applications requiring complex math! Fundamental to machine learning & AI P3 is a real-life application of this! Board games e.g., chess/checkerboard, tic tac toe, sudoku Representing information in a grid or table e.g., scorekeeping, gradebook, census data Image processing Lesson 15 - Autumn 2024 8

  9. matrixAdd 23 96 18 4 64 45 40 18 44 34 92 13 77 71 12 93 169 84 83 103 136 115 91 143 81 119 77 98 105 13 70 73 66 79 39 91 75 73 99 47 27 64 21 34 1 Lesson 15 - Autumn 2024 9

  10. matrixAdd i: 0 j: 0 23 96 18 4 64 45 40 18 44 34 92 13 77 71 12 93 169 84 83 103 136 115 91 143 81 119 77 98 105 13 70 73 66 79 39 91 75 73 99 47 27 64 21 34 1 Lesson 15 - Autumn 2024 10

  11. matrixAdd i: 0 j: 0 23 96 18 4 64 45 40 18 44 34 92 13 77 71 12 93 169 84 83 103 136 115 91 143 81 119 77 98 105 13 70 73 66 79 39 91 75 73 99 47 27 64 21 34 1 Lesson 15 - Autumn 2024 11

  12. matrixAdd i: 0 j: 1 23 96 18 4 64 45 40 18 44 34 92 13 77 71 12 93 169 84 83 103 136 115 91 143 81 119 77 98 105 13 70 73 66 79 39 91 75 73 99 47 27 64 21 34 1 Lesson 15 - Autumn 2024 12

  13. matrixAdd i: 0 j: 2 23 96 18 4 64 45 40 18 44 34 92 13 77 71 12 93 169 84 83 103 136 115 91 143 81 119 77 98 105 13 70 73 66 79 39 91 75 73 99 47 27 64 21 34 1 Lesson 15 - Autumn 2024 13

  14. matrixAdd i: 0 j: 3 23 96 18 4 64 45 40 18 44 34 92 13 77 71 12 93 169 84 83 103 136 115 91 143 81 119 77 98 105 13 70 73 66 79 39 91 75 73 99 47 27 64 21 34 1 Lesson 15 - Autumn 2024 14

  15. matrixAdd i: 0 j: 4 23 96 18 4 64 45 40 18 44 34 92 13 77 71 12 93 169 84 83 103 136 115 91 143 81 119 77 98 105 13 70 73 66 79 39 91 75 73 99 47 27 64 21 34 1 Lesson 15 - Autumn 2024 15

  16. matrixAdd i: 1 j: 0 23 96 18 4 64 45 40 18 44 34 92 13 77 71 12 93 169 84 83 103 136 115 91 143 81 119 77 98 105 13 70 73 66 79 39 91 75 73 99 47 27 64 21 34 1 Lesson 15 - Autumn 2024 16

  17. matrixAdd i: 1 j: 1 23 96 18 4 64 45 40 18 44 34 92 13 77 71 12 93 169 84 83 103 136 115 91 143 81 119 77 98 105 13 70 73 66 79 39 91 75 73 99 47 27 64 21 34 1 Lesson 15 - Autumn 2024 17

  18. matrixAdd i: 1 j: 2 23 96 18 4 64 45 40 18 44 34 92 13 77 71 12 93 169 84 83 103 136 115 91 143 81 119 77 98 105 13 70 73 66 79 39 91 75 73 99 47 27 64 21 34 1 Lesson 15 - Autumn 2024 18

  19. matrixAdd i: 1 j: 3 23 96 18 4 64 45 40 18 44 34 92 13 77 71 12 93 169 84 83 103 136 115 91 143 81 119 77 98 105 13 70 73 66 79 39 91 75 73 99 47 27 64 21 34 1 Lesson 15 - Autumn 2024 19

  20. matrixAdd i: 1 j: 4 23 96 18 4 64 45 40 18 44 34 92 13 77 71 12 93 169 84 83 103 136 115 91 143 81 119 77 98 105 13 70 73 66 79 39 91 75 73 99 47 27 64 21 34 1 Lesson 15 - Autumn 2024 20

  21. matrixAdd i: 2 j: 0 23 96 18 4 64 45 40 18 44 34 92 13 77 71 12 93 169 84 83 103 136 115 91 143 81 119 77 98 105 13 70 73 66 79 39 91 75 73 99 47 27 64 21 34 1 Lesson 15 - Autumn 2024 21

  22. matrixAdd i: 2 j: 1 23 96 18 4 64 45 40 18 44 34 92 13 77 71 12 93 169 84 83 103 136 115 91 143 81 119 77 98 105 13 70 73 66 79 39 91 75 73 99 47 27 64 21 34 1 Lesson 15 - Autumn 2024 22

  23. matrixAdd i: 2 j: 2 23 96 18 4 64 45 40 18 44 34 92 13 77 71 12 93 169 84 83 103 136 115 91 143 81 119 77 98 105 13 70 73 66 79 39 91 75 73 99 47 27 64 21 34 1 Lesson 15 - Autumn 2024 23

  24. matrixAdd i: 2 j: 3 23 96 18 4 64 45 40 18 44 34 92 13 77 71 12 93 169 84 83 103 136 115 91 143 81 119 77 98 105 13 70 73 66 79 39 91 75 73 99 47 27 64 21 34 1 Lesson 15 - Autumn 2024 24

  25. matrixAdd i: 2 j: 4 23 96 18 4 64 45 40 18 44 34 92 13 77 71 12 93 169 84 83 103 136 115 91 143 81 119 77 98 105 13 70 73 66 79 39 91 75 73 99 47 27 64 21 34 1 Lesson 15 - Autumn 2024 25

  26. matrixAdd 23 96 18 4 64 45 40 18 44 34 92 13 77 71 12 93 169 84 83 103 136 115 91 143 81 119 77 98 105 13 70 73 66 79 39 91 75 73 99 47 27 64 21 34 1 Lesson 15 - Autumn 2024 26

  27. (2D)ays Above Average: readData() How many days' data would you like to input? 3 Next day's data: Temperature in Seattle? 44 Temperature in Tacoma? 40 Temperature in Bothell? 43 Next day's data: Temperature in Seattle? 42 Temperature in Tacoma? 40 Temperature in Bothell? 44 Next day's data: Temperature in Seattle? 42 Temperature in Tacoma? 41 Temperature in Bothell? 43 Seattle Tacoma Bothell 44 40 43 1 42 40 44 2 42 41 43 3 Lesson 15 - Autumn 2024 27

  28. (2D)ays Above Average: readData() How many days' data would you like to input? 3 Next day's data: Temperature in Seattle? 44 Temperature in Tacoma? 40 Temperature in Bothell? 43 Next day's data: Temperature in Seattle? 42 Temperature in Tacoma? 40 Temperature in Bothell? 44 Next day's data: Temperature in Seattle? 42 Temperature in Tacoma? 41 Temperature in Bothell? 43 Seattle Tacoma Bothell 44 40 43 1 42 40 44 2 42 41 43 3 Lesson 15 - Autumn 2024 28

  29. (2D)ays Above Average: readData() How many days' data would you like to input? 3 Next day's data: Temperature in Seattle? 44 Temperature in Tacoma? 40 Temperature in Bothell? 43 Next day's data: Temperature in Seattle? 42 Temperature in Tacoma? 40 Temperature in Bothell? 44 Next day's data: Temperature in Seattle? 42 Temperature in Tacoma? 41 Temperature in Bothell? 43 Seattle Tacoma Bothell 44 40 43 1 42 40 44 2 42 41 43 3 Lesson 15 - Autumn 2024 29

  30. (2D)ays Above Average: readData() How many days' data would you like to input? 3 Next day's data: Temperature in Seattle? 44 Temperature in Tacoma? 40 Temperature in Bothell? 43 Next day's data: Temperature in Seattle? 42 Temperature in Tacoma? 40 Temperature in Bothell? 44 Next day's data: Temperature in Seattle? 42 Temperature in Tacoma? 41 Temperature in Bothell? 43 Seattle Tacoma Bothell 44 40 43 1 42 40 44 2 42 41 43 3 Lesson 15 - Autumn 2024 30

  31. (2D)ays Above Average: readData() How many days' data would you like to input? 3 Next day's data: Temperature in Seattle? 44 Temperature in Tacoma? 40 Temperature in Bothell? 43 Next day's data: Temperature in Seattle? 42 Temperature in Tacoma? 40 Temperature in Bothell? 44 Next day's data: Temperature in Seattle? 42 Temperature in Tacoma? 41 Temperature in Bothell? 43 Seattle Tacoma Bothell 44 40 43 1 42 40 44 2 42 41 43 3 Lesson 15 - Autumn 2024 31

  32. (2D)ays Above Average: readData() How many days' data would you like to input? 3 Next day's data: Temperature in Seattle? 44 Temperature in Tacoma? 40 Temperature in Bothell? 43 Next day's data: Temperature in Seattle? 42 Temperature in Tacoma? 40 Temperature in Bothell? 44 Next day's data: Temperature in Seattle? 42 Temperature in Tacoma? 41 Temperature in Bothell? 43 Seattle Tacoma Bothell 44 40 43 1 42 40 44 2 42 41 43 3 Lesson 15 - Autumn 2024 32

  33. (2D)ays Above Average: readData() How many days' data would you like to input? 3 Next day's data: Temperature in Seattle? 44 Temperature in Tacoma? 40 Temperature in Bothell? 43 Next day's data: Temperature in Seattle? 42 Temperature in Tacoma? 40 Temperature in Bothell? 44 Next day's data: Temperature in Seattle? 42 Temperature in Tacoma? 41 Temperature in Bothell? 43 Seattle Tacoma Bothell 44 40 43 1 42 40 44 2 42 41 43 3 Lesson 15 - Autumn 2024 33

  34. (2D)ays Above Average: readData() How many days' data would you like to input? 3 Next day's data: Temperature in Seattle? 44 Temperature in Tacoma? 40 Temperature in Bothell? 43 Next day's data: Temperature in Seattle? 42 Temperature in Tacoma? 40 Temperature in Bothell? 44 Next day's data: Temperature in Seattle? 42 Temperature in Tacoma? 41 Temperature in Bothell? 43 Seattle Tacoma Bothell 44 40 43 1 42 40 44 2 42 41 43 3 Lesson 15 - Autumn 2024 34

  35. (2D)ays Above Average: readData() How many days' data would you like to input? 3 Next day's data: Temperature in Seattle? 44 Temperature in Tacoma? 40 Temperature in Bothell? 43 Next day's data: Temperature in Seattle? 42 Temperature in Tacoma? 40 Temperature in Bothell? 44 Next day's data: Temperature in Seattle? 42 Temperature in Tacoma? 41 Temperature in Bothell? 43 Seattle Tacoma Bothell 44 40 43 1 42 40 44 2 42 41 43 3 Lesson 15 - Autumn 2024 35

  36. (2D)ays Above Average: readData() How many days' data would you like to input? 3 Next day's data: Temperature in Seattle? 44 Temperature in Tacoma? 40 Temperature in Bothell? 43 Next day's data: Temperature in Seattle? 42 Temperature in Tacoma? 40 Temperature in Bothell? 44 Next day's data: Temperature in Seattle? 42 Temperature in Tacoma? 41 Temperature in Bothell? 43 Seattle Tacoma Bothell 44 40 43 1 42 40 44 2 42 41 43 3 Lesson 15 - Autumn 2024 36

  37. (2D)ays Above Average: computeAverages() How many days' data would you like to input? 3 The average values for each location were [42.666666666666664, 40.333333333333336, 43.333333333333336] Seattle Tacoma Bothell 44 40 43 1 42 40 44 42.667 40.333 43.333 2 42 41 43 3 Average of Seattle temperatures (44 + 42 + 42) / 3 Lesson 15 - Autumn 2024 37

Related


More Related Content