1/50
Flashcards reviewing key concepts about arrays and ArrayLists in Java, covering topics such as array creation, manipulation, searching, sorting, and the use of ArrayLists.
Name | Mastery | Learn | Test | Matching | Spaced | Call with Kai |
|---|
No analytics yet
Send a link to your students to track their progress
What is the purpose of arrays?
Arrays allow us to create a collection of like values that are indexed.
How are array element values initialized when creating an array?
Array element values are initialized to 0.
At what index do array indexes always start?
Array indexes always start at 0.
Can the size of an array be changed after it's created?
Once created, an array size is fixed and cannot be changed.
How do you access an element in an array?
An array is accessed by the reference name and a subscript that identifies which element in the array to access.
What range of indexes are valid for an array?
Array indexes always start at zero and continue to (array length - 1).
What is a common error when accessing arrays?
Off-by-One Errors.
How can an array be initialized with specific values upon creation?
When relatively few items need to be initialized, an initialization list can be used to initialize the array.
How is data processed within an array?
Processing data in an array is the same as any other variable.
How can the length of an array be determined?
Arrays are objects and provide a public field named length that is a constant that can be tested.
Describe the enhanced for loop and its use with arrays.
Simplified array processing (read only). Always goes through all elements. for(datatype elementVariable : array)
Is it possible for a user to specify the size of an array at runtime?
Yes, you can let the user specify the size of an array.
What happens to the first array when reassigning array references?
If the first array no longer has a reference to it, it will be garbage collected.
How do you properly copy an array?
You need to copy the individual elements of one array to another.
When passing a single array element to a method, how is it handled?
When a single element of an array is passed to a method it is handled like any other variable.
How are arrays passed as arguments to methods?
Arrays are objects. Their references can be passed to methods like any other object reference variable.
How do you determine if two Arrays are equal?
You compare the contents of an array by looping through and ensuring each element is the same.
What is one way to sum all the elements in the array units?
int total = 0; for (int i = 0; i < units.length; i++) total += units[i];
What is a partially filled array?
It is an array that is sized to the largest expected number of elements. Use a counting variable to keep track of how much valid data is in the array.
How can you save the contents of an array to a file?
You can loop through the array and print each value to a file.
How can you read the contents of a file into an array?
You can loop through the file and assign each value to the array.
Can a method return an array?
Yes, a method can return a reference to an array. The return type of the method must be declared as an array of the right type.
How are string arrays used?
Arrays are not limited to primitive data. An array of String objects can be created.
What is the difference between array length field and string Array length method?
Arrays have a final field named length. String objects have a method named length.
How do you initialize each element in an array of objects?
Each element needs to be initialized by instantiating the object.
What is a search algorithm?
A method of locating a specific item in a larger collection of data.
What is the selection sort algorithm?
The smallest value in the array is located and moved to element 0, then the next smallest value is located and moved to element 1 and so on.
A binary search requires the array to be sorted in which order?
Ascending order
What is a two-dimensional array?
A two-dimensional array is an array of arrays.
When declaring a two-dimensional array, what do the two size declarators represent?
The first one is for the number of rows; The second one is for the number of columns.
When processing data in a two dimensional array, each element has how many subscripts?
Two, one for its row and one for its column.
How are two-dimensional arrays typically processed?
Using nested loops.
How do you initialize a Two-Dimensional Array?
Initializing a two-dimensional array requires enclosing each row’s initialization list in its own set of braces.
How do you find out the number of rows in the array?
The length field of the array gives the number of rows in the array.
How do you access the number of columns in a given row in the array?
Each row has a length constant tells how many columns is in that row.
What is a Ragged Array?
When the rows of a two-dimensional array are of different lengths, the array is known as a ragged array.
Can a Java program receive arguments from the operating system command-line?
Yes.
How does the main method receive arguments from the command line?
The main method receives a String array as a parameter.
What is the purpose of a variable-length argument list?
Special type parameter – vararg… – vararg parameters are actually arrays.
How does an ArrayList differ from an array?
An ArrayList automatically expands when a new item is added and automatically shrinks when items are removed.
What Java package must be imported to use he ArrayList class?
java.util.ArrayList
How do you specify the type of objects an ArrayList can hold?
Using angled brackets, e.g., ArrayList
How do you add items to an ArrayList?
Use the add method.
How do you get the current number of elements in an ArrayList?
Call the size method.
How do you access items in an ArrayList?
Use the get method with the index of the item.
How can I easily print all the elements in an ArrayList?
The ArrayList class's toString method returns a string representing all items in the ArrayList
How do you remove items from an ArrayList?
Use the remove method.
How do you insert a new item at a specific location in the ArrayList?
Use the add method with two arguments: the index and the item.
How do you replace an existing item in an ArrayList?
Use the set method with the index and the new item.
What is the default capacity of an ArrayList?
10 items.
How do you create an ArrayList with a specified initial capacity?
Use a parameterized constructor: ArrayList