Computer Science Chapter 7
What is args?
Args is a parameter for main, i.e. public static void main (String[] args), and an array of strings for every word typed on the command line. Each element represents an individual word (pg. 471).
What are the differences between ArrayLists and arrays?
ArrayLists are like arrays except their size is automatically adjusted to accommodate the number of items stored in it and items can be added or removed from the list (pg. 474).
What should you be carful about when using the increment/decrement operators and arrays?
Be careful to avoid altering the variable that represents the subscript unless that is your intention, i.e. nums[i--]; versus nums[i]--; (pg. 415).
What is the difference between comparing arrays with the == operator and with a for loop?
Comparing two arrays with the == operator only compares their memory addresses. If you want to compare the elements of the two arrays, you have to use a loop (pgs. 428-429).
What should you not confuse about arrays and their elements?
Do not confuse an array's length field and each elements length() method. The latter includes parenthesis (pg. 446).
What is each element in array of strings?
Each element in an array of strings is actually a memory address for a String object (pg. 443).
How do you declare a ragged array?
How to declare a ragged array (pg. 462): int[][] ragged = new int[2][] // num cols ragged[0] = new int[7]; // length row 0 ragged[1] = new int[4]; // length row 1
Binary Search Algorithum
A loop that searches for a specified value, starting with the element that is in the middle of the array. If the element contains the specified value, the search ends. If the element is greater then the loop searches the lower half. This continues until the value is found or there are no more elements to check (pg. 468).
Sequential Search Algorithm
A loop that sequentially steps through an array, starting with the first element, compares the elements to the value being searched for, and ends when the value is either found or the loop runs out of elements to search (pg. 449).
Variable-Length Argument Lists
A mechanism in Java that makes it possible to write a method that takes a variable number of arguments (pg. 472).
Search Algorithm
A method for locating a specific item in a larger collection of data (pg. 449).
Search Algorithum
A method of locating a specific item in a larger collection of data (pg. 465).
Vararg Parameter
A method parameter that can take a variable number of parameters, i.e. public int sum (int...nums) (pg. 472).
Selection Sort
A sorting algorithm that locates the smallest value in an array and moves it to position 0. The next smallest value is located and moved to position 1, etc. It does this by swapping the first element's value and the smallest with a holding variable (pg. 465).
Enhanced For Loop
A special loop that allows you to more easily loop through all of the elements in an array (pg. 418): for (dataType elementVariable : array) statement; elementVariable retrieves the value of the elements of the array during the loop. Element one for iteration one, etc. array is the array that you want the loop to work on.
Ragged Array
A two-dimensional array that has rows with different lengths (pg. 463).
Sorting Algorithum
An algorithm used to arrange data in some order (pg. 465).
What is an alternative way to declare an array reference variable?
An alternative way to declare array reference variables, although it is not recommended or common because it doesn't determine the size of the array or populate it with elements is: int numbers[]; (pg. 414).
Can an array be passed as an argument to a method?
An array can be passed as an argument to a method. To pass an array, you pass the value in the variable that references the array (pg. 424).
What happens when an array of String objects is created but not initialized?
An array of String objects may be created, but if the array is uninitialized, each string in the array must be created individually (pg. 443).
Two-dimensional Array
An array that holds two sets of data, i.e. int[][] rowsColumns = new int[3][3]; The first set of data represents the row location of an element and the second set represents the column location of an element (pg. 452).
One-dimensional Array
An array that only holds one set of data, i.e. int[] nums = new int[3]; (pg. 452).
What is each element in a String array that is not initialized assigned?
An element in a String array that is not initialized is assigned the value null (pg. 445).
Array
An object that can store a group of values (primitive data or objects) as long as they are of the same type (pg. 405-406).
What is an alternative way to initialize an array?
Another way to initialize an array is through an initialization list, i.e. int[] days = {31, 28, 31};. Each value is stored in the subscripts based on the order that they appear and the size declarator is determined by the number of elements in the list (pg. 413).
What happens when a partially filled array is passed as an argument to a method?
If a partially filled array is passed as an argument to a method, the variable that holds the count of items in the array must be passed as an argument. Otherwise, the method will not be able to determine the number of items that are stored in the array (pg. 440).
How do you copy the actual elements in an array to another array reference variable?
If you want to make an actual copy of an array's elements to another array reference variable, you have to copy the elements by hand or with a loop (pgs. 423-424): int[] array1 = {2,4,6}; int[] array2 = {2,4,6};
How does a three-dimensional array work?
In a three-dimensional array, i.e. library[][][], the first pair of brackets represents the columns in the book shelf, the second pair represents the rows, and the third represents the shelf number (pg. 464).
Can methods return arrays?
In addition to accepting arguments, methods may also return arrays (pg. 441).
How are individual array elements processed?
Individual array elements are processed like any other type of variable (pg. 415).
What are the important pieces of information to note in this statement: int[] numbers = new int[6];?
Int specifies the data-type of the array and the array numbers holds 6 ints in positions 0-5, which is determined by the size declarator (pgs. 405-406).
What do invalid subscripts cause?
Invalid subscripts cause a runtime error (pg. 411).
Why is it common to use final variables as size declarators?
It is common to use final variables as size declarators because they allow programmers to alter the array's size while making it clear that this should not be changed in any other part of the program (pgs. 406-407).
Is it required for the name of the main method's parameter to be called args?
It is not required that the name for main's parameter array be args; however, it is standard convention (pg. 471).
Does Java limit the number of dimensions that an array may have?
Java does not limit the number of dimensions that an array may have. It is possible to create arrays with multiple dimensions, to model data that occurs in multiple sets (pg. 464).
What does Java perform on array to make sure that the statement does not include a subscript that is out of bounds/greater than or equal to the size declarator?
Java performs a bounding script on arrays, which makes sure that the statement does not include a subscript that is out of bounds/greater than or equal to the size declarator (pg. 411).
What does the ArrayList's add(index, element) method do?
The .add(index, element) method inserts an item into an ArrayList at the specified index. The items previously at that index and the items that come after the newly added element are shifted upward one position to accommodate it (pg. 480).
What does the ArrayList's get method do?
The .get(index) method returns the element at the specified index from the ArrayList (pg. 475).
What does the ArrayList's remove method do?
The .remove(index) method takes out the element at the specified index from the ArrayList. The items that come after the element are shifted downward by one index position (pgs. 478-479).
What does the ArrayList's size method do?
The .size() method returns the number of elements in an ArrayList (pg. 475).
What does the ArrayList's toString method do?
The ArrayList's toString() method returns a String representation of all the items stored in an ArrayList object (pgs. 477-478).
What values are the elements in an array automatically assigned to?
The elements in an array automatically assigned the value 0 or null (pg. 408).
What is the import statement for ArrayLists?
The import statement for ArrayLists are import java.util.ArrayList; (pg. 475).
Size Declarator
The number inside the right-side brackets of an array's initialization statement that determines how many values the array can hold, must be greater than or equal to zero (pg. 406).
Capacity
The number of items that an ArrayList can store without having to increase its size (pg. 481).
Subscript
The number that each element in an array is assigned and accessed through, i.e. numbers[0] = 10. The expression numbers[0] is pronounced "numbers sub zero" (pg. 407).
What are the advantages and disadvantages of the sequential search and binary search algorithms?
The sequential search algorithm is easy to implement; however, it is inefficient. The binary search algorithm is faster; however, it requires the elements to be in sequential order (pg. 468).
What does the ArrayList's set method do?
The set(index, element) method replaces the item currently at the specified index with the specified element (pg. 48).
What is the statement for creating ArrayLists?
The statement for creating ArrayLists (pg. 475): ArrayList<DataType> nameList = new ArrayList<DataType>(size);
How do you access all the elements in a two-dimensional array?
To access all the elements in a two-dimensional array, use a nested for loop with the outer-loop going through the rows and the inner-loop going through the columns (pg. 454).
How do you add elements to an ArrayList?
To add elements to an ArrayList, use the add() method: nameList.add(element) (pg. 475).
How do you retrieve the number of columns in a two-dimensional array?
To retrieve the number of columns in a two-dimensional array use numbers[row].length (pg. 459).
How do you retrieve the number of rows in a two-dimensional array?
To retrieve the number of rows in a two-dimensional array use numbers.length (pg. 459).
What does arrayName.length hold?
arrayName.length holds and returns the amount of elements in an array as an int (notice the lack of parenthesis at the end) (pg. 418).
After an array has been created, can its size be changed?
Once an array is created, its size cannot be changed (pg. 407).
Why do programmers return -1 when the search value is not found?
Programmers return -1 when the search value is not found in the array because -1 is not a valid subscript (pg. 451).
How do you sum the individual columns in a two-dimensional array?
To sum the individual columns in a two-dimensional array, use a nested for loop with the outer loop representing the columns and the inner loop representing the rows. Make sure to set the accumulator to zero before accessing the inner row for loop; otherwise, you will add up all the elements in the array and not just the column (pgs. 460-461).
How do you sum the individual rows of a two-dimensional array?
To sum the individual rows of a two-dimensional array, use a nested for loop but make sure to set the accumulator to zero before accessing the inner column for loop; otherwise, the accumulator will add up all the elements in the array and not just the individual row (pg. 460).
Reference Copy
Two or more array reference variables that point to the same array object; thus, when you alter one array you alter the other (pg. 422): int[] array1 = {2,4,6}; int[] array2 = array1;
Can variables represent an array element's subscript?
Variables can represent an array element's subscript: hours[i]; (pg. 411).
How do you use an initializer list with two-dimensional arrays?
Way to use initializer lists with two-dimensional arrays (pg. 458): int[][] nums = { {1,2,3}, // row 1 {4,5,6}, // row 2 {7,8,9} }; // row 3
What happens when you invoke a Java program from the operating system command line?
When you invoke a Java program from the operating system command line, you can specify arguments that are passed into the main method of the program. In addition, you can write a method that takes a variable number of arguments. When a method runs, it can determine the number of arguments that were passed to it and act accordingly (pg. 470).
How do you efficiently change the size of an array?
You can reassign an array reference variable to another array object, thus effectively changing the size of the array (pg. 421): int[] nums = new int[10]; nums = new int[5];
Can you create arrays of objects?
You may create arrays of objects that are instances of classes that you have written (pg. 446).
When should you use a traditional loop over an enhanced for loop?
You should use a traditional for loop over an enhanced one when (pg. 419): 1. you need to change the contents of an array element(s), 2. you need to work through the elements in reverse order, 3. you need to access some of the elements, but not all of them, 4. you need to simultaneously work with two or more arrays, or 5. you need to refer to the subscript of the element(s).