FLVS AP Computer Science A: Module 5 - Arrays

Data and Arrays

Objectives

  • Represent patterns and algorithms involving data sets in everyday life using written language or diagrams.

  • Develop code to represent collections of related data using one-dimensional (1D) array objects.

  • Develop code to read data from a text file.

Definitions and Key Concepts

  • Table vs. List:

    • Lists: Store related data types, sorting values in ascending or descending order; often qualitative.

    • Tables: Organize detailed data into rows and columns to manage multiple related lists.

  • Data Structure: A collection of data referred to by a single name (e.g., arrays or array lists).

  • Array: A data structure acting as a list that holds a single type of data (e.g., Strings, numbers), accessible via index positions.

  • Java’s Data Structures:

    • Linked Lists: A structure holding a varying number of objects where each object points to the next.

    • Trees: A hierarchical data structure growing from a single root node.

    • Array Lists: A structure for storing objects that provides methods to add, delete, or retrieve items.

  • ArrayIndexOutOfBoundsException: An error triggered when attempting to access an index outside the range of 0 to (length - 1).

  • printf() method: A method functional like print() or println() that allows for direct formatting using specifiers.

  • Format Specifiers

    • % : Functions similarly to a \ character in standard print statements.

    • + : Displays a plus sign for positive numbers and a minus sign for negative numbers.

    • - : Aligns the output to the left.

    • , : Includes commas in numeric output.

    • s : Directs Java to print a string value from a specific variable.

    • d : Directs Java to print a decimal integer from a variable.

    • f : Directs Java to print a floating-point number, including a double.

    Formatting and Methods

    • Decimal formatting: The syntax [%number]d%n instructs Java to set the line of text to be number characters long.

    • split() method: A method used to define an array with an initially unknown size by creating new items whenever specified text is encountered.

Traversing Arrays

Objectives

  • Develop code used to traverse the elements in a 1D array and determine the result of these traversals.

  • Array Traversal: The act of visiting every element within an array.

  • For-each loop: A mechanism that begins with an index at 00, incrementing by 11 after each execution until reaching the end; a copy of the current element’s data is made in each iteration.

  • Common Loops for Traversal:

    • For loop

    • For-each loop

    • While loop

Arrays of Objects

Objectives

  • Develop code for traversing the elements in a 1D array and determine results.

  • Develop code for standard and original algorithms based on arrays.

  • Develop code to create string objects and assess outcomes of combination.

Traversal and Classes

  • For-each loop: A mechanism that begins with an index at 0, incrementing by 1 after each execution until reaching the end; a copy of the current element’s data is made in each iteration.

  • Common Loops for Traversal: For loop, For-each loop, and While loop.

  • Accessors: Methods that access private instance variables without modification (e.g., getters).

  • Mutators: Methods that change the value of private instance variables (e.g., setters).

  • Getters: Methods that return a value to an invoking statement.

  • Setters: Methods that assign values to private instance variables or objects.

  • Array of Objects: A data structure consisting of different objects rather than primitives.

  • toString() method: Logic used to format a string that contains different variables.

  • Element Updating: Changing contents by assigning new values or objects to specified indices.

Standard Array Algorithms

Algorithms and Functionality

  • Min/Max Value Algorithm: A process where a variable for Java’s largest/smallest possible number is created; the program loops through the array, checking if each current index's value is larger or smaller than the variable and updating it accordingly.

  • Array Reversal: A method using a loop to swap elements from opposite ends of the array until reaching the middle point.

Wrapper Classes

Objectives

  • Develop code to utilize Integer and Double objects instead of their primitive types and analyze the outcomes.

Definitions and Key Concepts

  • Wrapper Classes: Classes in the java.lang package, specifically Integer and Double, used to treat primitives as objects.

  • Immutable Object: An object whose attributes are permanent and cannot be modified after creation.

  • Wrapper Methods: Access to typical methods like toString() and compareTo() available for object classes.

  • Autoboxing: The process of converting a primitive type into its corresponding wrapper class; essentially “wrapping” a primitive variable into an object.

  • Unboxing: The reverse process of autoboxing; converting a wrapper class object back into its corresponding primitive data type.