1 / 46

CS 200 Arrays, Loops and Methods

CS 200 Arrays, Loops and Methods. Jim Williams, PhD. This Week. Piazza: Hackathons, Pizza with Prof. Team Lab: Arrays and Hangman drawing diagrams (bring paper and pencil) BP1 Milestone 1 due Thursday Lecture: More Arrays and Methods. BP1 Notes. Process: 3 milestones then further testing

gustavos
Télécharger la présentation

CS 200 Arrays, Loops and Methods

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. CS 200 Arrays, Loops and Methods Jim Williams, PhD

  2. This Week • Piazza: Hackathons, Pizza with Prof. • Team Lab: Arrays and Hangman • drawing diagrams (bring paper and pencil) • BP1 Milestone 1 due Thursday • Lecture: More Arrays and Methods

  3. BP1 Notes Process: 3 milestones then further testing Concepts: Use only material from Chapters 1-8 Teams: • Once you submit as a team for Milestone 1 then you are a team for the entire project.

  4. Which are true? String []arr = new String[4]; arr[2] = new String("hello"); arr[3] = arr[2]; arr[0] = "hello"; arr[1] = "hello"; 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

  5. 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; }

  6. What does this code do? 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]; } }

  7. Which swaps the ith and i+1th values?

  8. What is print out? 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]);

  9. What is print out? 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)); See java.util.Arrays for equals methods to compare the contents of arrays.

  10. Arrays of Arrays Also called: Multi-dimensional arrays

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

  12. How many elements will this array hold? int [][] board = new int[5][5];

  13. What is the data type of board[3][1]? int [][] board = new int[5][5];

  14. What is the data type of board[2]? int [][] board = new int[5][5];

  15. What values will elements in this array have, initially? boolean [][] board; board = new boolean[5][5];

  16. Which is correct way to set an element value? int [][] board = new int[5][5]; How to set element 0,0 to 5?

  17. How many elements in this array? int [][] board = {{1,2},{4,5,6}};

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

  19. Key Concepts • Unit Testing • Memory Access • Drawing a picture of memory for an array • Looping through multidimensional arrays • appropriate use of .length • Common Array algorithms

  20. Unit Testing (method) • Writes method to meet requirements • Verifies that method meets requirements These are different perspectives and can be challenging to switch between. But the better you can develop this skill of switching between perspectives the more reliable your code will likely be.

  21. Requirements /** * This method returns the number of 9s in the table. * @param table a 2 dimensional array of int * @return the number of 9s in table */ public static int num9s(int [][]table) { return -1; }

  22. Today More Arrays & Methods

  23. Draw a Diagram int [][] board; board = new int[3][2]; How do you find the number of rows? How do you find the number of columns?

  24. Draw a Diagram int [][][] board; board = new int[3][][]; board[1] = new int[][]{{1,2},{3,4}};

  25. Draw a Diagram int [][] board; board = new int[][]{{1,2},{4,5,6}}; board[1] = new int[4];

  26. Will this initialize the array to all 1's? int [][] b; b= new int[][]{{1,2},{4,5,6}}; for ( int i = 0; i < 2; i++) for ( int j = 0; j < 3; j++) board[ i ][ j ] = 1;

  27. Will this initialize the array to all 1's? int [][] b; b= new int[][]{{1,2},{4,5,6}}; for ( int i = 0; i < b[i].length; i++) for ( int j = 0; j < b[j].length; j++) b[ i ][ j ] = 1;

  28. Will this initialize the array to all 1's? int [][] b; b= new int[][]{{1,2},{4,5,6}}; for ( int i = 0; i < b.length; i++) for ( int j = 0; j < b[ i ].length; j++) b[ i ][ j ] = 1;

  29. Is array of int at row 1 accessible? int [][] board; board = new int[][]{{1,2},{4,5,6}}; board[1] = null;

  30. Question 1 minute to answer • I want to keep the results to analyze later

  31. What will print out? public static void methodA(int [] list) { list[0] = 9; } public static void main(String []args) { int [] list = new int[]{1,2,3}; methodA( list); System.out.println( list[0]); }

  32. Memory Access • Local variables and parameters (stored on stack) are only visible within the method itself. • to share value, return value from method, caller captures and uses • Instances/objects (stored on heap) are available anywhere using the reference. • Follow the reference from inside or outside the method to shared memory area (heap).

  33. Where in memory is variable z? public static void main(String []args) { int r = 3; int c = 4; int [][] z = new int[r][c]; }

  34. Is it possible for methodC to change the elements in the array? public static void main(String []args) { final char [] items = new char[]{'c','b','a'}; methodC( items); }

  35. Can you call this method to get the new array? public static void createList(int [] list) { list = new int[10]; }

  36. Does main use the array from createList? public static int [] createList(int size) { return new int[size]; } public static void main(String[]args) { int [] list; createList( 10); list[3] = 10; }

  37. What will print out? public static void changeList(int [] list) { if ( list.length > 0) { list[0] = 10; } } public static void main(String []args) { int [] z = new int[5]; changeList( z); System.out.println( z[0]); }

  38. What prints out? static void change( int num, int [] list) { num = 11; list[2] = num; } public static void main( String [] args) { int [] list = {1,2,3}; int num = 10; change( num, list); System.out.println("num:" + num + "\nlist:" + Arrays.toString( list)); }

  39. What prints out? static void change( int num, int [] list) { list = new int[]{4,5,6}; list[2] = num; } public static void main( String [] args) { int [] list = new int[] {1,2,3}; int num = 10; change( num, list); System.out.println("num:" + num + "\nlist:" + Arrays.toString( list)); }

  40. What will print out? public static void changeList(int [][] list) { list[0] = new int[3]; list[0][1] = 9; } public static void main(String []args) { int [][] z = new int[5][5]; changeList( z); System.out.println( z[0][1]); }

  41. 3 Questions 1 minute each

  42. What will print out? public static void methodA(int [] list) { list = new int[3]; list[0] = 9; } public static void main(String []args) { int [] list = new int[]{1,2,3}; methodA( list); System.out.println( list[0]); }

  43. What will print out? public static void methodA(int [] list) { list[0] = 9; list = new int[3]; } public static void main(String []args) { int [] list = new int[]{1,2,3}; methodA( list); System.out.println( list[0]); }

  44. What will print out? public static void methodA(int [] list) { list[0] = 9; } public static void main(String []args) { int [] list = new int[]{1,2,3}; methodA( list); System.out.println( list[0]); }

  45. Common Algorithms for Arrays Searching Sorting

  46. Sorting Demo 9, 6, 4, 5, 8, 2

More Related