Presentation is loading. Please wait.

Presentation is loading. Please wait.

1D Arrays and Lots of Brackets

Similar presentations


Presentation on theme: "1D Arrays and Lots of Brackets"— Presentation transcript:

1 1D Arrays and Lots of Brackets
Ps Module 7 – Part I 1D Arrays and Lots of Brackets 4/6/2019 CSE 1321 Module 7

2 2. Creating and Initializing Arrays
You must choose: 1) Create an empty arrays and assign initial values with a loop later on: CREATE myArray[5] OR 2) Create and initialize the array in one line. This is helpful when we already know those value (e.g. days of the week, etc.). 4/6/2019 CSE 1321

3 Array Creation Example
1) Create an empty array. By default, 0s are stored in the array. int[] myArray = new int[5]; 2) Creates and initializes in one line. The stored values are show in { }; int[] myArray = {10, 20, 30, 40, 50}; 4/6/2019 CSE 1321

4 3. Accessing Information
Copy information out of a particular slot Example: CREATE clientAge // Integer clientAge ← myArray[4] This copies information from the fifth slot (slot four) into the variable clientAge 4/6/2019 CSE 1321

5 Horrible, but works… // creates empty array with 5 slots, 0-4 CREATE myArray [5] //Assigns literal values to each index in the array. myArray[0] ← 10 myArray[1] ← 20 myArray[2] ← 30 myArray[3] ← 40 myArray[4] ← 50 Ps 4/6/2019 CSE 1321

6 Accessing and Modifying
//Accessing int secondNumber = myArray[1]; PRINT(secondNumber); //Modifying myArray[3] = 42; PRINT(myArray[3]); 4/6/2019 CSE 1321 Module 4

7 Ps Traversing the Array
You will use a loop to visit every cell of the array Problem: create an array of 5 bytes and fill each slot with the number 42 Solution: CREATE myArray [5] FOR i ← 0 to 4 myArray[i] ← 42 END FOR Ps 4/6/2019 CSE 1321

8 Traversing an Array Use a FOR, WHILE or DO-WHILE starting at index 0 and going thru .length-1 for (int i=0; i < data_storage.length; i++) { // do something } OR use a foreach loop foreach (int t : data_storage) { //do something as long as it is not assignment } 4/6/2019 CSE 1321

9 Ps Another Trace CREATE smallestSoFar smallestSoFar ← randomArray[0]
FOR counter ← 1 to 4 IF (smallestSoFar > randomArray[counter]) THEN smallestSoFar ← randomArray[counter] END IF END FOR // Done counter 5 1 2 3 4 smallestSoFar -8 42 17 42 -8 4

10 Finding the Minimum using a Method
private static int FindMin (int[] A) { int temp = A[0]; for (int i = 1; i < A.length(); i++) { if (temp > A[i]) { temp = A[i];} } return temp; Parameters match in position and type, so the name doesn't matter. Arrays are a reference type in Java. 4/6/2019 CSE 1321

11 Finding the sum or average using a method
METHOD FINDAVERAGE ( parameter: nums[]) BEGIN sum ← 0 FOR i ← 0 to nums.length - 1 // MOST IMPORTANT LINE IS HERE sum = sum + nums[i] ENDFOR average = sum / nums.length return average END FINDAVERAGE Ps 4/6/2019 CSE 1321

12 Finding a sum and or average using a method
private static float FindAverage (int [] B) { int sum = 0; for (int i = 0; i < B.length; i++) sum += B[i]; } return (float)sum / B.length; 4/6/2019 CSE 1321

13 Ps Defining a 2D array CREATE array nums [numRows][numColumns]
4/6/2019 CSE 1321

14 Java- Define a 2D Array <type> [ ][ ] <name> = new <type>[<rowSize>][<columnSize>]; For example: int[ ][ ] grid = new int [10][20]; 4/6/2019 CSE 1321

15 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 4/6/2019 CSE 1321

16 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(" "); } 4/6/2019 CSE 1321

17 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"); } 4/6/2019 CSE 1321

18 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); } 4/6/2019 CSE 1321


Download ppt "1D Arrays and Lots of Brackets"

Similar presentations


Ads by Google