1 / 36

CS 200 Arrays

CS 200 Arrays. Jim Williams, PhD. This Week. Team Lab: Loops & Debugging P6 Due Thursday BP1 Milestone 1 available this week. Exam Review Come to Marc's or my office hours to review Exam Final Exam policy Lecture: Arrays. MasterMind Demo. Milestone Bonus Points.

dwanda
Download Presentation

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

  2. This Week • Team Lab: Loops & Debugging • P6 Due Thursday • BP1 Milestone 1 available this week. • Exam Review • Come to Marc's or my office hours to review Exam • Final Exam policy • Lecture: Arrays

  3. MasterMind Demo Milestone Bonus Points

  4. Changing Behavior of Loop continue - skip rest of body, check condition break - leave loop immediately Affect only the innermost loop in which they are contained.

  5. What is the output? for (int i = 1; i <= 5; ++i) { System.out.print("\n" + i + " : "); for (int j = i; j <= 10; j++) { if (i % 2 == 0) continue; if (j % 3 == 0) break; System.out.print(j + " "); } }

  6. Data Structures Organize data to suit a specific purpose. CS 300 OO Programming and Basic Data Structures CS 400 Advanced Data Structures and SW Carpentry

  7. Arrays • Hold multiple values • Access individual values with an index • A reference type (not primitive) • Automatically initialized (allocated on heap) • Contiguous memory area

  8. Memory Diagram for Arrays in main method: int [] list; list = new int[5]; int [] list2 = new int[]{ 2, 4, 6, 8}; terms: element, index, element datatype reference, braces { }, brackets [ ]

  9. Today P6 is due today BP1 is now available • milestone 1 due Thursday, March 14th. Lecture: Single and multi-dimensional arrays (arrays and arrays of arrays)

  10. What century is 1947 in? and why?

  11. Why array indexing starts with 0. • Single dimension arrays are contiguous in memory • Equally efficient to access any element • nth element's address is: address + (n * elementSize)

  12. int [] list3 = new int[]{ 3, 5, 7, 9}; System.out.println( list3[3] );

  13. Question double [] list = new double[]{8.0,7.1,6.2,5.3,4.4}; If the array begins at address 1000, what is address of the element with value 6.2?

  14. Which are valid ways to create arrays? 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

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

  16. What will this print? int [] list = null; list[0] = 10; list[1] = list[0] + 20; System.out.print( list[1]);

  17. Will these show the contents of the arrays? char [] chars = {'a','b','c','d','e'}; System.out.println(chars); int [] list = {10, 2, 23, 64}; System.out.println(list);

  18. What is the value of sum? int sum; int [] list = {1,3,5}; sum = list[0] + list[1] + list[2];

  19. Either syntax works in Java int [ ] list1; //preferable OR int list2 [ ];

  20. What is the value of z? int [] list = new int[3]; list[1] = 6; list[2] = 3; int z = list[3];

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

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

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

  24. 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]; } }

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

  26. 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]);

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

  28. Multi-Dimensional Arrays More accurately: multiple single dimensional arrays

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

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

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

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

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

  34. Which is correct way to set an element value? int [][] board = new int[5][5];

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

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

More Related