1D Arrays and Lots of Brackets

Slides:



Advertisements
Similar presentations
Why not just use Arrays? Java ArrayLists.
Advertisements

Computer Science 1620 Multi-Dimensional Arrays. we used arrays to store a set of data of the same type e.g. store the assignment grades for a particular.
Java: How to Program Arrays and ArrayLists Summary Yingcai Xiao.
1 Lecture Today’s topic Arrays Reading for this Lecture: –Chaper 11.
Building Java Programs Chapter 7.5
By Nicholas Policelli An Introduction to Java. Basic Program Structure public class ClassName { public static void main(String[] args) { program statements.
French Territory of St. Pierre CSE 114 – Computer Science I Arrays.
M180: Data Structures & Algorithms in Java Arrays in Java Arab Open University 1.
OBJECTS FOR ORGANIZING DATA -- As our programs get more sophisticated, we need assistance organizing large amounts of data. : array declaration and use.
CMSC 202 Arrays 2 nd Lecture. Aug 6, Array Parameters Both array indexed variables and entire arrays can be used as arguments to methods –An indexed.
CSE 1201 Object Oriented Programming ArrayList 1.
CS 180 Recitation 7 Arrays. Used to store similar values or objects. An array is an indexed collection of data values of the same type. Arrays are the.
Arrays Chapter 12. Overview Arrays and their properties Creating arrays Accessing array elements Modifying array elements Loops and arrays.
COMP More About Arrays Yi Hong June 05, 2015.
int [] scores = new int [10];
Array Size Arrays use static allocation of space. That is, when the array is created, we must specify the size of the array, e.g., int[] grades = new int[100];
Chapter 8 Slides from GaddisText Arrays of more than 1 dimension.
Arrays in java Unit-1 Introduction to Java. Array There are situations where we might wish to store a group of similar type of values in a variable. Array.
Arrays (part 2) 1 -Based on slides from Deitel & Associates, Inc. - Revised by T. A. Yang.
VISUAL C++ PROGRAMMING: CONCEPTS AND PROJECTS Chapter 7A Arrays (Concepts)
Chapter 9 Introduction to Arrays Fundamentals of Java.
CSE 1301 Lecture 12 Arrays Figures from Lewis, “C# Software Solutions”, Addison Wesley Richard Gesick.
CMSC 202 ArrayList Aug 9, 2007.
Write code to prompt for 5 grades, read them in, print “Thank you”, then reprint the 5 grades and their average. System.out.println(“Please enter grade.
Arrays 3/4 By Pius Nyaanga.
Chapter 10 Arrays.
Lecture 5 D&D Chapter 6 Arrays and ArrayLists Date.
Arrays 2/4 By Pius Nyaanga.
Chapter-7 part3 Arrays Two-Dimensional Arrays The ArrayList Class.
Repetition-Counter control Loop
Counted Loops.
Objects First with Java CITS1001
Lecture 9 Arrays Richard Gesick.
Chapter 10 Arrays.
Arrays An Array is an ordered collection of variables
Chapter 6 Arrays.
Nested Loop Review and Two-Dimensional Arrays
An Introduction to Java – Part I, language basics
Chapter 8 Slides from GaddisText
Review of Classes and Arrays
CMSC 202 ArrayList Aug 9, 2007.
Defining methods and more arrays
برنامه نویسی پیشرفته اصول جاوا.
CMSC 202 ArrayList Aug 9, 2007.
CS2011 Introduction to Programming I Arrays (I)
Module 4 Loops and Repetition 2/1/2019 CSE 1321 Module 4.
int [] scores = new int [10];
Lecture 4 2d Arrays CSE /26/2018.
Arrays and Array Lists CS 21a.
Code Refresher Test #1 Topics:
1D Arrays and Lots of Brackets
1D Arrays and Lots of Brackets
Review of Classes and Arrays
INC 161 , CPE 100 Computer Programming
Arrays in Java.
C++ Array 1.
1D Arrays and Lots of Brackets
Arrays 3/4 June 3, 2019 ICS102: The course.
Chapter 6 Arrays.
Ps Module 7 – Part II 2D Arrays and LISTS 5/26/2019 CSE 1321 Module 7.
Module 8 – Searching & Sorting Algorithms
Review of Classes and Arrays
Arrays, Part 1 of 2 Topics Definition of a Data Structure
Arrays Introduction to Arrays Reading for this Lecture:
First Semester Review.
Module 8 – Searching & Sorting Algorithms
1D Arrays and Lots of Brackets
Ps Module 7 – Part II 2D Arrays and LISTS 8/29/2019 CSE 1321 Module 7.
MIS 222 – Lecture 12 10/9/2003.
Presentation transcript:

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. 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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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