Chapter 7: Arrays and ArrayList Flashcards
28 Questions
100 Views

Chapter 7: Arrays and ArrayList Flashcards

Created by
@VersatileCopernicium

Questions and Answers

What indicates in an array declaration the number of elements that an array is to have?

  • Size declarator (correct)
  • Element sum
  • Subscript
  • Reference variable
  • Each element of an array is accessed by a number known as a(n)...

  • Subscript (correct)
  • Address
  • Size declarator
  • Specifier
  • The first subscript in an array is always...

  • 1 less than the number of elements
  • 1
  • -1
  • 0 (correct)
  • The last subscript in an array is always...

    <p>1 less than the number of elements</p> Signup and view all the answers

    Array bounds checking happens...

    <p>When the program runs</p> Signup and view all the answers

    This array field holds the number of elements that the array has.

    <p>Length</p> Signup and view all the answers

    What search algorithm steps through an array, comparing each item with the search value?

    <p>Sequential search</p> Signup and view all the answers

    What search algorithm repeatedly divides the portion of an array being searched in half?

    <p>Binary search</p> Signup and view all the answers

    What is the MAXIMUM number of comparisons performed by the sequential search on an array of N elements?

    <p>N</p> Signup and view all the answers

    When initializing a two-dimensional array, you enclose each row's initialization list in...

    <p>Braces</p> Signup and view all the answers

    To store an item in an ArrayList object, use this method.

    <p>Add</p> Signup and view all the answers

    To insert an item at a specific location in an ArrayList object, you use this method.

    <p>Add</p> Signup and view all the answers

    To delete an item from an ArrayList object, you use this method.

    <p>Remove</p> Signup and view all the answers

    To determine the number of items stored in an ArrayList object, you use this method.

    <p>Size</p> Signup and view all the answers

    What is the typical number of comparisons performed by the sequential search on an array of N elements?

    <p>N/2</p> Signup and view all the answers

    Java does not allow a statement to use a subscript that is outside the range of valid subscripts for an array.

    <p>True</p> Signup and view all the answers

    An array's size declarator can be a negative integer expression.

    <p>False</p> Signup and view all the answers

    Both of the following declarations are legal and equivalent: int[] numbers; and int numbers[];

    <p>True</p> Signup and view all the answers

    The subscript of the last element in a single-dimensional array is one less than the total number of elements in the array.

    <p>True</p> Signup and view all the answers

    The values in an initialization list are stored in the array in the order that they appear in the list.

    <p>True</p> Signup and view all the answers

    The Java compiler does not display an error message when it processes a statement that uses an invalid subscript.

    <p>True</p> Signup and view all the answers

    When an array is passed to a method, the method has access to the original array.

    <p>True</p> Signup and view all the answers

    The first size declarator in the declaration of a two-dimensional array represents the number of columns. The second size declarator represents the number of rows.

    <p>False</p> Signup and view all the answers

    A two-dimensional array has multiple length fields.

    <p>True</p> Signup and view all the answers

    An ArrayList automatically expands in size to accommodate the items stored in it.

    <p>True</p> Signup and view all the answers

    Write code that copies array A to array B.

    <p>for (int i = 0; i &lt; A.length; i++) b[i] = A[i];</p> Signup and view all the answers

    Write a loop that displays the first character of the strings stored in the array shown below: String[] petNames = { 'Spot', 'Duke', 'Girl', 'Poochie' }

    <p>for (int i = 0; i &lt; petNames.length; i++) System.out.println(petNames[i].charAt(0));</p> Signup and view all the answers

    Write a statement that creates the following 2D array: A double array with 4 rows and 8 columns referenced by the variable candyTypes.

    <p>double[][] candyTypes = new double[4][8];</p> Signup and view all the answers

    Study Notes

    Array Basics

    • Array declaration includes a size declarator indicating how many elements the array will hold.
    • Each element in an array is accessed via a subscript, which is a number representing the index of the element.
    • The first subscript of an array is always 0, meaning arrays are zero-indexed.
    • The last subscript is one less than the total number of elements in the array.

    Array Characteristics

    • Array bounds checking occurs at runtime, helping to prevent out-of-bounds errors.
    • The length field of an array holds the number of elements it contains.
    • In a two-dimensional array, each row's initialization list is enclosed in braces.

    Search Algorithms

    • A sequential search traverses each element in the array to find a match with the search value.
    • A binary search divides the array in half repeatedly, enhancing search efficiency compared to a sequential search.
    • The maximum number of comparisons in a sequential search on an array of N elements is N.
    • The average number of comparisons in a sequential search is typically N/2.

    ArrayList Class

    • To store an item in an ArrayList, use the add method.
    • To insert an item at a specific location in an ArrayList, the add method is also used with an index.
    • The remove method in an ArrayList enables deletion of an item.
    • The size method returns the count of items currently stored in an ArrayList.
    • An ArrayList automatically resizes itself to accommodate changes in the number of items.

    Java Array and ArrayList Facts

    • Java prevents the use of invalid subscripts in array statements, ensuring safe data access.
    • An array's size cannot be negative; size declarators must always be non-negative.
    • Both declarations of an integer array, int[] numbers; and int numbers[];, are valid and equivalent.
    • The subscript for the last element of a single-dimensional array is one less than the total count, correlating with zero-based indexing.
    • Values in an initialization list are stored in the array in the same order they appear.

    Passing Arrays and Two-Dimensional Arrays

    • When an array is passed to a method, the method operates on the original array, not a copy.
    • The first size declarator in a two-dimensional array declaration represents the number of rows, while the second represents the number of columns.
    • A two-dimensional array can have multiple length fields corresponding to each dimension.

    Code Snippets

    • To copy contents from array A to array B:
      for (int i = 0; i < A.length; i++)
          b[i] = a[i];
      
    • To display the first character of each string in an array:
      for (int i = 0; i < petNames.length; i++)
          System.out.println(petNames[i].charAt(0));
      
    • To create a 2D array with 4 rows and 8 columns:
      double[][] candyTypes = new double[4][8];
      

    Studying That Suits You

    Use AI to generate personalized quizzes and flashcards to suit your learning preferences.

    Quiz Team

    Description

    Test your knowledge on arrays and the ArrayList class with these flashcards from Chapter 7. Each card features key terms and their definitions, helping you grasp the essential concepts required for understanding arrays in programming. Perfect for students and learners alike!

    More Quizzes Like This

    Array Declaration and Usage
    20 questions
    Array Declaration and Definition
    36 questions
    Array Declaration and Usage
    17 questions
    Array Declaration and Indexing
    13 questions
    Use Quizgecko on...
    Browser
    Browser