Podcast
Questions and Answers
What indicates in an array declaration the number of elements that an array is to have?
What indicates in an array declaration the number of elements that an array is to have?
Each element of an array is accessed by a number known as a(n)...
Each element of an array is accessed by a number known as a(n)...
The first subscript in an array is always...
The first subscript in an array is always...
The last subscript in an array is always...
The last subscript in an array is always...
Signup and view all the answers
Array bounds checking happens...
Array bounds checking happens...
Signup and view all the answers
This array field holds the number of elements that the array has.
This array field holds the number of elements that the array has.
Signup and view all the answers
What search algorithm steps through an array, comparing each item with the search value?
What search algorithm steps through an array, comparing each item with the search value?
Signup and view all the answers
What search algorithm repeatedly divides the portion of an array being searched in half?
What search algorithm repeatedly divides the portion of an array being searched in half?
Signup and view all the answers
What is the MAXIMUM number of comparisons performed by the sequential search on an array of N elements?
What is the MAXIMUM number of comparisons performed by the sequential search on an array of N elements?
Signup and view all the answers
When initializing a two-dimensional array, you enclose each row's initialization list in...
When initializing a two-dimensional array, you enclose each row's initialization list in...
Signup and view all the answers
To store an item in an ArrayList object, use this method.
To store an item in an ArrayList object, use this method.
Signup and view all the answers
To insert an item at a specific location in an ArrayList object, you use this method.
To insert an item at a specific location in an ArrayList object, you use this method.
Signup and view all the answers
To delete an item from an ArrayList object, you use this method.
To delete an item from an ArrayList object, you use this method.
Signup and view all the answers
To determine the number of items stored in an ArrayList object, you use this method.
To determine the number of items stored in an ArrayList object, you use this method.
Signup and view all the answers
What is the typical number of comparisons performed by the sequential search on an array of N elements?
What is the typical number of comparisons performed by the sequential search on an array of N elements?
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.
Java does not allow a statement to use a subscript that is outside the range of valid subscripts for an array.
Signup and view all the answers
An array's size declarator can be a negative integer expression.
An array's size declarator can be a negative integer expression.
Signup and view all the answers
Both of the following declarations are legal and equivalent: int[] numbers; and int numbers[];
Both of the following declarations are legal and equivalent: int[] numbers; and int numbers[];
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.
The subscript of the last element in a single-dimensional array is one less than the total number of elements in the array.
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.
The values in an initialization list are stored in the array in the order that they appear in the list.
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.
The Java compiler does not display an error message when it processes a statement that uses an invalid subscript.
Signup and view all the answers
When an array is passed to a method, the method has access to the original array.
When an array is passed to a method, the method has access to the original array.
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.
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.
Signup and view all the answers
A two-dimensional array has multiple length fields.
A two-dimensional array has multiple length fields.
Signup and view all the answers
An ArrayList automatically expands in size to accommodate the items stored in it.
An ArrayList automatically expands in size to accommodate the items stored in it.
Signup and view all the answers
Write code that copies array A to array B.
Write code that copies array A to array B.
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' }
Write a loop that displays the first character of the strings stored in the array shown below: String[] petNames = { 'Spot', 'Duke', 'Girl', 'Poochie' }
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.
Write a statement that creates the following 2D array: A double array with 4 rows and 8 columns referenced by the variable candyTypes.
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;
andint 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.
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!