Ch 7 Arrays

Previously in CS

  • We have written programs that
    • can receive data as input from the keyboard, store the data in variables, and process the data in some way to produce a result (output).
    • can make decisions based on particular boolean conditions being true or false.
    • can loop over a portion of code repeatedly so long as a condition evaluates to true.

Overview

Arrays are a fundamental data structure, and they are extremely useful. We use arrays to hold values of the same type at contiguous memory locations. In particular, the use of arrays allows us to create “groups” or “clusters” of variables without needing to give a unique variable name to each, but still allowing us to individually index into the elements of the array. If you haven’t started counting from zero yet, now is the time, because in Java, arrays are zero-indexed which means the first element of a k-element array is located at array index 0 and the last element is located at array index k-1.

Strings are a special case of arrays — in Java, a string is it’s on data type that is like an array of characters. It suffices to group and discuss arrays and strings together. You will continue to practice using various string methods. You will also learn to create and manipulate arrays of various data types.

Calendar

  • Tuesday, 11/28: Return loops test. Revisit loops.
  • Thursday, 11/30: Finishing loops.
  • Monday, 12/4: Strings day 1.
  • Wednesday, 12/6: Strings day 2. Iguana ST Quiz. Final day to turn in test corrections (must have already met with me prior to today)
  • Friday, 12/8: Strings day 3.
  • Tuesday, 12/12: Arrays day 1. Binary. Begin Tweet Validator.
  • Thursday, 12/14: MC Quiz 3 + Array Sum FRQ Quiz (50-75-100). Arrays day 2. Binary. Begin Merge Arrays.
  • Monday, 12/18: Test/Exam 3. Turn in assignments. Gift Exchange Party!

Tuesday’s Overview

Think it Through, Break it Down, Plan it Out (partial or complete solution in pseudocode), Code it.

  1. Michael, Connor, Adonis.  4.10, 4.16
  2. Luke, Aaron, Zach.  4.11, 4.13
  3. Aidan, Johnny, Hunter.  4.15, 4.22
  4. Lane, Cole, Jordan.  4.21, 4.25
  5. Kyle, Matthew, Dillon.  4.8, 4.26

Present Thoughts, Breakdown, and Explain partial or complete solution to class

  1. peer review: ask questions for clarification and evaluate proposed solution
  2. boss review: ask questions for clarification and evaluate proposed solution

Arrays

Binary

Tweet Validator Lab

  1. Thinking it Through, Breaking it Down, Planning it Out, Coding It.

Check Yo’self Exit Ticket

Resources

  1. Arrays – slides
  2. Algorithms on Arrays – slides
  3. Arrays Practice Work Sheet – KEY
  4. Test/Exam 3 Review – KEY

Arrays in Real Life

  1. Post Office Boxes. An array is a chunk of memory, as a mail bank is a chunk of space on the wall Arrays have been divided into identically-sized blocks called elements, as a mail bank has been divided into identically-sized blocks called P.O. boxes. Each element can hold a certain amount of data, as each P.O. box can hold a certain amount of mail. That data is of the same data type, such as int, as the mail is all of the same type, such as letters. Lastly, each element of an array can be accessed directly by index number, as each P.O. box can be accessed directly by mailbox number
  2. Games: How might Battleship, Tic Tac Toe, Wheel of Fortune, and other games utilize arrays?
  3. Multiplication: How might you use a multidimensional array to define a multiplication table?

Programming Exercises

  1. Arrays Programming Exercises

Common Errors with Arrays

  1. forgetting to create the array – only declaring it (int[ ] nums;)
  2. using 1 as the first index not 0
  3. using array.length as the last valid index in an array, not array.length – 1.
  4. using array.length() instead of array.length (not penalized on the free response)
  5. using array.get(0) instead of array[0] (not penalized on the free response)
  6. going out of bounds when looping through an array (using index <= array.length). You will get an ArrayIndexOutOfBoundsException.
  7. jumping out an loop too early by using one or more return statements before every value has been processed.

Thought Questions

  1. Is there ever a situation where it might be better to have 2, 3, or n separate variables instead of having a 2-, 3-, or n-element array?
  2. Because we have to specify the size of an array before we start using it, how might we respond to needing extra elements?
  3. What if we need to insert an element between two other elements we’ve already defined in our array?
  4. If we don’t otherwise know the number of elements in an array, how might we be able to calculate it? What other information do we need to know in order to do so?

Recap(itulation) of Arrays

  1. In this chapter you learned about Arrays. An array is consecutive storage for multiple items of the same type like the top five scores in a game. You learned how to declare arrays, create them, and access array elements. Array elements are accessed using an index. The first element in an array is at index 0.

    ../_images/arrayIndicies.png
  • Array – An array can hold many items (elements) of the same type. You can access an item (element) at an index and set an item (element) at an index.
  • Array Declaration – To declare an array specify the type of elements that will be stored in the array, then ([]) to show that it is an array of that type, then at least one space, and then a name for the array. Examples: int[] highScores; String[] names;
  • Array Creation – To create an array type the name and an equals sign then use the new keyword, followed by a space, then the type, and then in square brackets the size of the array (the number of elements it can hold). Example: names = new String[5];
  • Array Index – You can access and set values in an array using an index. The first element in an array called arr is at index 0 arr[0]. The last element in an array is at the length minus one – arr[arr.length - 1].
  • Array Initialization – You can also initialize (set) the values in the array when you create it. In this case you don’t need to specify the size of the array, it will be determined from the number of values that you specify. Example: int[] highScores = {99,98,98,88,68};
  • Array Length – The length of an array is the number of elements it can hold. Use the public lengthfield to get the length of the array. Example: given int[] scores = {1,2,2,1,3,1};scores.lengthequals 6.
  • Class Method – A method that can be called on the class. It is declared using the static keyword. An example is Math.abs(-3).
  • Element Reference – A specific element can be referenced by using the name of the array and the element’s index in square brackets. Example: scores[3] will return the 4th element (since index starts at 0, not 1). To reference the last element in an array, use array[array.length - 1]
  • For-each Loop – Used to loop through all elements of an array. Each time through the loop the loop variable will be the next element in the array starting with the element at index 0, then index 1, then index 2, etc.
  • Out of Bounds Exception – An error that means that you tried to access an element of the array that doesn’t exist maybe by doing arr[arr.length]. The first valid indices is 0 and the last is the length minus one.
  • for – starts both a general for loop and a for-each loop. The syntax for a for each loop is for (type variable : array). Each time through the loop the variable will take on the next value in the array. The first time through the loop it will hold the value at index 0, then the value at index 1, then the value at index 2, etc.
  • static – used to create a class method, which is a method that can be called using the class name like Math.abs(-3).

AP Exam Practice with Arrays

  1. Coding Bat exercises
  2. Easy MC Questions
  3. Medium MC Questions
  4. Hard MC Questions
  5. Code Practice with Arrays

Learning Objectives

  • To describe why arrays are necessary in programming (§7.1).
  • To declare array reference variables and create arrays (§§7.2.1–7.2.2).
  • To obtain array size using arrayRefVar.length and know default values in an array (§7.2.3).
  • To access array elements using indexes (§7.2.4).
  • To declare, create, and initialize an array using an array initializer (§7.2.5).
  • To program common array operations (displaying arrays, summing all elements, finding the minimum and maximum elements, random shuffling, and shifting elements) (§7.2.6).
  • To simplify programming using the foreach loops (§7.2.7).
  • To apply arrays in application development (AnalyzeNumbers, DeckOfCards) (§§7.3–7.4).
  • To copy contents from one array to another (§7.5).
  • To develop and invoke methods with array arguments and return values (§§7.6–7.8).
  • To define a method with a variable-length argument list (§7.9).
  • To search elements using the linear (§7.10.1) or binary (§7.10.2) search algorithm.
  • To sort an array using the selection sort approach (§7.11).
  • To use the methods in the java.util.Arrays class (§7.12).
  • To pass arguments to the main method from the command line (§7.13).
  • LO 2.1.1: Describe the variety of abstractions used to represent data
  • LO 2.2.3: Identify multiple levels of abstraction that are used when writing programs

Enduring Understandings

  • EU 2.1 : A variety of abstractions built on binary sequences can be used to represent all digital data.
  • EU 2.2 : Multiple levels of abstraction are used to write programs or create other computational artifacts.