Java: Arrays & ArrayList Class (7)
An *ArrayList* is similar to an array of objects, but offers many advantages over an array including:
*ArrayList object* automatically expands/shrinks as items are added/removed (526)
create an *ArrayList object* and store its address [code]
*ArrayList<String> nameList =* *new ArrayList<String>();* (526)
statement that creates an array of *String objects* initialized with values [code]
*String[] names = { "Bill", "Susan", "Steven", "Jean" };* (490)
three-dimensional array declaration [code]
*double[] [] [] seats = new double[3] [5] [8];* (521)
Array length [code]
*double[] temperatures = new double[25];* (465)
enhanced for loop [code]
*for (dataType elementVariable : array) {* -->*statement;* *}*(465)
array elements may be used in relational expressions [code]
*if (cost[20] < cost[0])* (462)
use of an integer variable to specify an array's size declarator [code]
*int size;* *int[] numbers;* *System.out.print("How many numbers?");* *size = keyboard.nextInt();* *numbers = new int[size];* (466,467)
array [coded declaration & initialization]
*int[] numbers = new int[6];* (450)
2 different styles to use when declaring array reference variables include: [code]
*int[] numbers;* *int numbers[];* (459)
*ArrayList class* is in the _____ *package*
*java.util* package [code] *import java.util.ArrayList;* (526)
pass an array as an argument by passing the value in the variable that references the array [code]
*showArray(numbers);* (473)
NOTE
-1 is returned when the search value is *not* found in the array because -1 is *not* a valid subscript (499)
*ArrayList object methods:*
-add -size -get -
variable-length argument lists
-makes it possible to write a method that takes a variable number of arguments; -you *can* write a method that accepts any number of arguments when it is called; -when the method runs, it *can* determine the number of arguments that were passed to it and act accordingly (523)
reassign an array reference variable to a different array [code]
//Create array ref'd by numbers var *int[] numbers = new int[10];* //reassign numbers to new array *numbers = new int[5];* (468)
popular sorting & searching algorithms include:
1. selection sort 2. binary search (501)
how does the *selection sort* work?
1. the smallest value in the array is located & moved to element 0 2. the next smallest value is located & moved to element 1 3. the process continues until *all* of the elements have been placed in their proper order (501)
declaring a *two-dimensional array*
2 sets of brackets & 2 size declarators are required: [code] *double[] [] scores = new double[3] [4];* (509)
you can*not* use _____ to compare 2 array reference variables to determine whether they are equal
== operator (476)
what does it mean when it says that Java performs *array bounds checking?*
to *not* allow a statement to use a subscript outside of the range of valid subscripts for an array (456)
*int numbers[], codes[], scores[];* [code]
to declare all 3 variables as references to int arrays using the alternate notation, you need to write a set of *[ ] brackets* after each variable name (460)
off-by-one error
usually seen with loops, this error shows up as a result that is 1 less or 1 greater than the expected value (457)
array subscripts *can be accessed* using _____
variables (slide 9)
ragged array
when the rows of a two-dimensional array are of different lengths (520)
FACT
when you create an array, Java allows you to initialize its elements with values (457)
FACT
it is a common practice to use a final variable as a *size declarator* (451) [code] *final int ARRAY_SIZE = 6;* *int[] numbers = new int[ARRAY_SIZE];*
FACT
it is possible to declare a reference variable and create an instance of an array with one element (450)
FACT
it is possible to reassign an array reference variable to a different array (468)
when initializing a *two-dimensional array*, you enclose each row's initialization list in _____
its own set of braces (513) [code] *int[] [] numbers = { {1, 2, 3}, {4, 5, 6,}, {7, 8, 9} };*
a one-dimensional array has a(n) _____ field that holds the number of elements in the array
length (514)
The _____ field can be useful when processing the entire contents of an array
length field -public field (465)
when you invoke a Java program from the operating system *command line*, you can specify arguments that are passed into the ____ of the program
main method (522) ~when the method runs, it can determine the number of arguments that were passed to it and act accordingly
search algorithm
method of locating a specific item in a larger collection of data (498, 501)
methods can be written to: [list]
methods can be written to: -store values in an array -display an array's contents -total all of an array's elements -calculate their average -etc (472) ~usually, such methods accept an array as an argument
programs that process *two-dimensional arrays* can do so with _____
nested loops (511)
as with one-dimensional arrays, you do *not* use the _____ key word when you provide an initialization list
new (514) ~Java automatically creates the array and fills its elements with the initialization values
array size declarator
number inside the brackets of an array to declare the number of *elements* or *values* the array can hold (450)
length field
number of elements in an array
array
object that can store a group of values, all of the same type (449)
Unlike an array, an *ArrayList* _____ is automatically adjusted to accomodate the number of items being stored in it
object's size (526)
FACT
once an array is created, its size can *not* be changed aka *dimensioning* (451)
primitive variables are designed to hold ____ value(s) at a time
one (449)
when processing the data in a two-dimensional array, each element has 2 subscripts:
one subscript for its row another subscript for its column (510)
a _____ is normally used with an accompanying integer variable that holds the number of items stored in the array
partially filled array (487)
a method can return a _____ to an array
reference (488)
when a two-dimensional array is passed to a method, the parameter must be declared as a(n) _____ to a two-dimensional array
reference (518) [code] *private static void showArray(int[] [] array)*
FACT
when you create an uninitialized array of String objects, you must assign a value to each element in the array (493) [code] *final int ARRAY_SIZE = 4;* *String[] names = new String[ARRAY_SIZE];* *names[0] = "Bill";* *names[1] = "Susan";* *names[2] = "Steven";* *names[3] = "Jean";*
FACT
when you pass an array as an argument, you simply pass the value in the variable that references the array (473)
FACT
when you process a *partially filled array*, you must only process the elements that contain valid data items (486)
NOTE
you can *not* change the value of an array's length field (465)
FACT
you can *not* copy an array by assigning one array reference variable to another, but you *must copy* the individual elements of one array to another (470)
NOTE
you can also pass an array to a *vararg parameter* (526)
CONCEPT
you may create arrays of objects that are instances of classes that you have written (494)
how to *input values* into an array
you must *input* the values one at a time into the individual array elements (452)
FACT
a two-dimensional array has multiple length fields to hold the rows & columns (514)
use a(n) _____ to find the sum of the values in an array
accumulator variable (477)
CONCEPT
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* (472) *VideoNote*
FACT
an array can hold *multiple* values of the same data type simultaneously (449)
CONCEPT
an array of *String objects* may be created, but if the array is uninitialized, each *String* in the array must be created individually (490)
Two-Dimensional array
an array of arrays that can hold multiple sets of data ie: an array that has *rows & columns* (509)
an array of *String objects* is really _____
an array of references to *String objects* (491)
*java CommandLine How does this work?* [code]
args[0] = "How" args[1] = "does" args[2] = "this" args[3] = "work?" (523)
FACT
array elements may be used in relational expressions (462)
One-Dimensional array
array that can hold one set of data (509)
*vararg parameters* are actually _____
arrays (524)
Why are arrays important?
arrays help to improve *efficiency* in writing code
FACT
as with one-dimensional arrays, it is a common practice to use final variables as the size declarators for two-dimensional arrays (510) [code] *final int ROWS = 3;* *final int COLS = 4;* *double[] [] scores = new double[ROWS] [COLS];*
WARNING
be careful not to cause an *off-by-one error* when using the *length field* as the upper limit of a subscript the *length field* contains the number of *elements* that an array has the largest *subscript* that an array has is length - 1 (465)
-the _____ uses a loop to *sequentially* step through an array *starting with the first element*. -it compares each element with the value being searched for and *stops* when the value is *found* or the *end* of the array is encountered. -if the value being searched for is *not* in the array, the algorithm unsuccessfully searches to the *end* of the array
sequential search algorithm (498)
initialization list
series of values inside the braces of an array separated with commas (458)
a _____ is a technique for scanning through an array and rearranging its contents in some specific order
sorting algorithm (501)
a(n) _____ is used as an index to pinpoint a specific element within an array
subscript (such as for loop counters) (451)
objects in an array are accessed with _____, just like any other data type in an array
subscripts (496)
FACT
Java allows you to use an integer variable to specify an array's size declarator ~this makes it possible to allow the user to specify an array's size (466)
FACT
Java automatically creates the array and stores the values in the *initialization list* in it (458)
Java: Arrays & ArrayList Class (7) (449-535)
Java: Arrays & ArrayList Class (7) (449-535)
when an entire array is passed into a method, it is passed just as an object is passed. Why is this?
the actual array itself is *not passed*, but a *reference to the array is passed into the parameter* (473)
FACT
the array and the reference variable are 2 separate entities (469)
*int [] numbers, codes, scores;* [code]
the statement declares 3 variables all 3 are references to int arrays (459)
NOTE
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 also be passed as an argument. Otherwise, the method will not be able to determine the number of items stored in the array (487)
FACT
if you do *not* provide an initialization list, you *must* use the *new key word* to create the array (492) [code] *final int ARRAY_SIZE = 4;* *String[] names = new String[ARRAY_SIZE];*
FACT
individual array elements are processed like any other type of variable (460)
As with the primitive data types, a(n) _____ automatically causes an array of *String objects* to be created in memory
initialization list (492)
*int numbers[], codes, scores;* [code]
the statement declares 3 variables, but only *numbers* is a reference to an int array codes and scores variables are regular int variables (459)
requirements for a *binary search*
the values in the array must be sorted in ascending order (505)
_____ is a class in the Java API similar to an array and allows you to *store & retrieve objects*
ArrayList (526)
FACT
You can think of an *ArrayList* as a *container* for holding other objects (526)
you can *not* use the *enhanced for loop if:
You need to: -change the contents of an array element -work through the array elements in reverse order -*access some* of the array elements, but *not all* of them -simultaneously work w/ 2 or more arrays within the loop -refer to the subscript number of a particular element (466)
-instead of testing the array's first element, the _____ algorithm starts with the element in the *middle* -if that element happens to contain the *desired value*, then the search is over; -otherwise, the value in the *middle* element is either *greater than or less than the value being searched for* -if it is *greater*, the desired value will be found somewhere in the first half of the array; -if it is *less*, half of the array's elements have been eliminated from further searching; -if the desired value wasn't found in the middle element, the procedure is repeated for the half of the array that potentially contains the value
binary search (505)
the _____ is much more efficient than the sequential search
binary search (505)
FACT
by default, Java initializes array elements with 0 (452)
FACT
by using the length fields in algorithms that process two-dimensional arrays, you can write code that works with arrays of any number of rows & columns (517)
length method
counts the number of chars in a String
enhanced for loop
designed to iterate once for every *element* in an *array* for each iteration, it copies an *array element* to a variable (465)
Java does *not* limit the number of _____ that an array may have
dimensions (521) ~it is possible to create arrays with multiple *dimensions*, to model data that occurs in multiple sets
FACT
do *not* use the *new key word* when you use an *initialization list* (458)
declaring an array reference variable [does] or [does not] create an array
does not (450)
FACT
each array in Java has a public field named length ~the field contains the number of elements in the array (465)
the ____ that follows the data type indicates a special type of parameter known as a *vararg parameter*
ellipsis (...) ~*vararg can* take a variable number of arguments (524)
to access the values stored in an array, from the first element to the last element, which is simpler? [enhanced for loop] or [traditional for loop]
enhanced for loop is simpler (466)