CS 200 - Week 7 Jim Williams, PhD.

Slides:



Advertisements
Similar presentations
1 More on Arrays and Loops Reading for this Lecture: –Section 5.4, , Break and Continue in Loops Arrays and For-each Loops Arrays and Loops.
Advertisements

Lecture 05 - Arrays. Introduction useful and powerful aggregate data structure Arrays allow us to store arbitrary sized sequences of primitive values.
Loops – While, Do, For Repetition Statements Introduction to Arrays
1 CS 177 Week 12 Recitation Slides Running Time and Performance.
CS 139-Programming Fundamentals Lecture 11B - Arrays Adapted from a presentation by Dr. Rahman Fall 2014.
Review for Final Exam. Contents 5 questions (20 points each) + 1 bonus question (20 points) – Basic concepts in Chapters 1-4 – Chapters 5-9 – Bonus: Chapter.
Composition When one class contains an instance variable whose type is another class, this is called composition. Instead of inheritance, which is based.
Review :chapters What is an algorithm? A step by step description of how to accomplish a task. For example, Adding 3 numbers N1, N2 and N3 Add.
A: A: double “4” A: “34” 4.
Week 10 - Wednesday.  What did we talk about last time?  Method example  Roulette simulation  Types in Java.
Chapter 5: ARRAYS ARRAYS. Why Do We Need Arrays? Java Programming: From Problem Analysis to Program Design, 4e 2  We want to write a Java program that.
ARRAY AND LOOPS REVIEW Mr. Crone. What will the code below print? int[] arry = {2, 5, 2, 1, 3}; for(int i = 0; i < 5; i ++) System.out.print(arry[i]);
Lecture 2 D&D Chapter 2 & Intro to Eclipse IDE Date.
CS 160 – Summer 16 Exam 1 Prep.
Intro to ETEC Java.
Arrays (review) CSE 2011 Winter May 2018.
Arrays.
Chapter 7 Single-Dimensional Arrays
CS1010 Discussion Group 11 Week 7 – Two dimensional arrays.
CS Week 14 Jim Williams, PhD.
CS Week 2 Jim Williams, PhD.
CS 200 Arrays, Loops and Methods
CS 302 Week 15 Jim Williams, PhD.
CS 200 Branches Jim Williams, PhD.
CS Week 4 Jim Williams, PhD.
Comp Sci 302 Introduction to Programming
CS Week 11 Jim Williams, PhD.
Advanced Programming Behnam Hatami Fall 2017.
CS Week 13 Jim Williams, PhD.
CS 302 Week 10 Jim Williams.
CS Week 8 Jim Williams, PhD.
CS 200 Using Objects Jim Williams, PhD.
CS 177 Week 15 Recitation Slides
CS Week 6 Jim Williams, PhD.
CSS161: Fundamentals of Computing
CS 200 Arrays, Loops and Methods
CS Week 3 Jim Williams, PhD.
CS 302 Week 8 Jim Williams, PhD.
CS Week 9 Jim Williams, PhD.
Chapter 8 Multi-Dimensional Arrays
Week 6 CS 302 Jim Williams, PhD.
Building Java Programs
CS 200 Objects and ArrayList
Arrays We often want to organize objects or primitive data in a way that makes them easy to access and change. An array is simple but powerful way to.
Review for Final Exam.
CS 200 More Primitives, Objects, Branches and Methods
CS Week 3 Jim Williams.
Topics Covered: Arrays, 1-D & 2-D Passing & Returning Arrays
Lec 4: while loop and do-while loop
CS 200 Loops Jim Williams, PhD.
CS2011 Introduction to Programming I Arrays (I)
CS 200 Primitives and Expressions
CS 200 Arrays Jim Williams, PhD.
Arrays and Array Lists CS 21a.
CS Week 4 Jim Williams, PhD.
slides created by Ethan Apter
CS 200 Primitives and Expressions
CS 200 Additional Topics and Review
CS 200 Methods, Using Objects
Review for Final Exam.
Building Java Programs
CS 200 Objects and ArrayList
Peer Instruction 4 Control Loops.
CS Week 2 Jim Williams, PhD.
Java: Variables, Input and Arrays
CS Week 3 Jim Williams, PhD.
Week 7 CS 302 Jim Williams.
CS 200 Objects and ArrayList
CS302 Week 6 Jim Williams.
Week 7 - Monday CS 121.
Presentation transcript:

CS 200 - Week 7 Jim Williams, PhD

This Week Exam 1 Results Team Lab: Loops & Eclipse Debugging Lecture: Arrays

Arrays A reference type (not primitive) Hold multiple values Access individual values with an index

Key Array Concepts Variable to hold array reference Allocation of memory for the array Initialization of the array Accessing Array Elements

Which are valid ways to create arrays? 1, 2, 3, 4 1, 2, 4 3, 4 2 int [] list1; //1 int [] list2 = new int[5]; //2 int list3 = new int[]{1,2,3}; //3 int [] list4 = new int[1,2,3,4];//4

Which array initializations are valid? int [] listA = new int[4]; //1 int [] listB = new int[]{8, 7, 6}; //2 Scanner input = new Scanner( System.in); int sizeC = input.nextInt(); int [] listC = new int[ sizeC]; //3 1, 2, 3 1,3 1,2 none

What will this print? int [] list; list[0] = 10; System.out.print( list[1]); 30 1020 compiler error runtime error

Will these show the contents of the arrays? both will both won't one but not the other char [] chars = {'a','b','c','d','e'}; System.out.println(chars); int [] list = {10, 2, 23, 64}; System.out.println(list);

What is the value of sum? int sum; int [] list = {1,3,5}; 9 3 compiler error runtime error int sum; int [] list = {1,3,5}; sum = list[0] + list[1] + list[2];

What is the value of z? int [] list = new int[3]; list[1] = 6; int z = list[3]; 3 compiler error runtime error

What is the data type of listG? String array of String compiler error JVM error String [ ] listG = {"first","middle","last"}; Try to draw a memory diagram of this.

Which are true? 1,2,3,4 String []arr = new String[4]; arr[0] = "hello"; arr[1] = "hello"; arr[2] = new String("hello"); arr[3] = arr[2]; System.out.println( arr[2] == "hello" ); //1 System.out.println( arr[2].equals( "hello")); //2 System.out.println( arr[3] == arr[2]); //3 System.out.println( arr[3].equals(arr[2])); //4 1,2,3,4 1,2,4 2,3,4 2,4

Will this set all elements to 3? final int START_VALUE = 3; int [] list = new int[5]; for ( int i = 0; i < list.length -1; i++) { list[i+1] = START_VALUE; } Yes Not first Not last Error

What does this code do? double value = 0.0; find minimum value find maximum value compiler error logic error double value = 0.0; double [] nums= {3.0,5.0,2.3,4.1}; for ( int i = nums.length; i > 0; i--) { if ( nums[i-1] > value) { value = nums[i-1]; }

Which swaps the ith and i+1th values? temp = list[i]; list[i] = list[i+1]; list[i+1] = temp; temp = list[i+1]; list[i] = temp; list[i+1] = list[i];

What is print out? char [] list1 = new char[]{'a', 'b', 'c'}; true false char [] list1 = new char[]{'a', 'b', 'c'}; char [] list2 = new char[]{'b', 'b', 'c'}; System.out.println( list1.length == list2.length); System.out.println( list1[1] == list2[1]);

What is print out? System.out.println( list1 == list2); true false char [] list1 = new char[]{'a', 'b', 'c'}; char [] list2 = new char[]{'a', 'b', 'c'}; System.out.println( list1 == list2); System.out.println( list1.equals( list2)); java.util.Arrays( list1, list2);

Multi-Dimensional Arrays

Which picture of 2-D array is more accurate? int [][] board = new int[3][2];

How many elements will this array hold? 5 10 25 Error int [][] board = new int[5][5];

What is the data type of board[3][1]? int [][] board = new int[5][5]; int int [] reference to int can't access board[3][1]

What is the data type of board[2]? int [][] board = new int[5][5]; int int [] reference to int can't access board[2]

What values will this array have, initially? boolean [][] board; board = new boolean[5][5]; false true none, they must be initialized first.

Which is correct way to set an element value? int [][] board = new int[5][5]; board[0][0] = 5; board[0,0] = 5; board{0,0} = 5; error

How many elements in this array? int [][] board = {{1,2},{4,5,6}}; 5 6 ragged arrays are not valid error

How would you access "Hi."? responses[1][1][1] Error String [ ][ ][ ] responses = { {{"hello"}, { "How do you do.", "Hi."}}, {{"always"}, { "When?", "Really, always?"}} };

Write code to print out as a grid 1 2 3 4 5 7 8 9 int [][]grid = {{1,2,3},{4,5},{7,8,9}}; for ( int r = 0; r < grid.length; r++) { for ( int c = 0; c < grid[r].length; c++) { System.out.print( grid[r][c]); } System.out.println();

Common Algorithms for Arrays Searching Sorting