00:00

Working with 2D Arrays in Java: Creating and Manipulating Arrays

Learn how to define and work with 1D and 2D arrays in Java. See examples of creating arrays of specific sizes, populating them with values, and iterating through elements using nested loops. The provided pseudocode demonstrates how to create a 4x5 array, populate it with numbers from 1 to 20, and print the array elements. Additionally, a nested loop example is included to display a pattern of stars based on row numbers.

lahaba
Download Presentation

Working with 2D Arrays in Java: Creating and Manipulating Arrays

An Image/Link below is provided (as is) to download presentation Download Policy: Content on the Website is provided to you AS IS for your information and personal use and may not be sold / licensed / shared on other websites without getting consent from its author. Content is provided to you AS IS for your information and personal use only. Download presentation by click this link. While downloading, if for some reason you are not able to download a presentation, the publisher may have deleted the file from their server. During download, if you can't get a presentation, the file might be deleted by the publisher.

E N D

Presentation Transcript


  1. Arrays Arrays 1D & 2D Arrays

  2. Defining a 1D array Defining a 1D array CREATE array nums [size]

  3. Java Java- - Define a 1D Array Define a 1D Array <type> [ ] <name> = new <type>[size]; For example: int[ ] grid = new int [10];

  4. Defining a 2D array Defining a 2D array CREATE array nums [numRows][numColumns]

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

  6. Working with 2D arrays 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

  7. 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(" "); } } }

  8. Pseudocode – Nested for Loop Example BEGIN MAIN CREATE maxRows = 10, row, star FOR (row = 1, row < maxRows, row = row + 1) FOR (star = 1, star <= row, star = star + 1) PRINT “*” ENDinnerFOR PRINTLINE () ENDouterFOR END MAIN

  9. Nested for Loop Example int maxRows = 10; for (int row = 1; row <= maxRows; row++) { for (int star = 1; star <= row; star++) { System.out.print ("*"); } System.out.println(); }

More Related