Podcast
Questions and Answers
Which of the following is NOT a characteristic of an ArrayList
?
Which of the following is NOT a characteristic of an ArrayList
?
- It can store primitive data types directly. (correct)
- It can grow or shrink in size as needed.
- It is a linear collection of objects.
- It requires importing `java.util.ArrayList`.
An ArrayList
can store both objects and primitive data types directly.
An ArrayList
can store both objects and primitive data types directly.
False (B)
To use the ArrayList
class in Java, what import statement is required?
To use the ArrayList
class in Java, what import statement is required?
java.util.ArrayList
In an ArrayList
, the position of the first element is referred to as index ______.
In an ArrayList
, the position of the first element is referred to as index ______.
What will happen if you try to access an element at an index that is outside the bounds of an ArrayList
?
What will happen if you try to access an element at an index that is outside the bounds of an ArrayList
?
The .get()
method in ArrayList
returns the element at the specified position.
The .get()
method in ArrayList
returns the element at the specified position.
In the context of an ArrayList
, what term is used to describe an object stored within the list?
In the context of an ArrayList
, what term is used to describe an object stored within the list?
The method used to retrieve the number of elements in an ArrayList
is called ______.
The method used to retrieve the number of elements in an ArrayList
is called ______.
Which method is used to remove a specific element from an ArrayList
?
Which method is used to remove a specific element from an ArrayList
?
The clear()
method removes all elements from an ArrayList
.
The clear()
method removes all elements from an ArrayList
.
What method is used to determine if an ArrayList
contains a specified element?
What method is used to determine if an ArrayList
contains a specified element?
To replace an element at a specific position in an ArrayList
, you would use the ______ method.
To replace an element at a specific position in an ArrayList
, you would use the ______ method.
What is a key difference between arrays and ArrayLists
?
What is a key difference between arrays and ArrayLists
?
Once an array is declared, its size can be changed.
Once an array is declared, its size can be changed.
How do you access the length of an array?
How do you access the length of an array?
Array elements are accessed using ______ instead of the .get()
method used in ArrayLists
.
Array elements are accessed using ______ instead of the .get()
method used in ArrayLists
.
What is the index of the last element in an array named numbers
?
What is the index of the last element in an array named numbers
?
When you assign one array to another using =
, you create a copy of the entire array.
When you assign one array to another using =
, you create a copy of the entire array.
What does it mean for array elements to be stored 'by reference'?
What does it mean for array elements to be stored 'by reference'?
The enhanced for
loop is also known as the ______ loop.
The enhanced for
loop is also known as the ______ loop.
Which of the following is a limitation of the enhanced for
loop?
Which of the following is a limitation of the enhanced for
loop?
Parallel arrays are two arrays where the elements at the same indexes are not related.
Parallel arrays are two arrays where the elements at the same indexes are not related.
In the context of arrays, what is a 'partially filled' array?
In the context of arrays, what is a 'partially filled' array?
The main method accepts an array of ______ as arguments.
The main method accepts an array of ______ as arguments.
Which class provides useful methods for working with arrays?
Which class provides useful methods for working with arrays?
The Arrays.sort()
method can only sort arrays of primitive data types.
The Arrays.sort()
method can only sort arrays of primitive data types.
What must be true about an array before using the binarySearch()
method?
What must be true about an array before using the binarySearch()
method?
If the binarySearch()
method does not find the element, it returns a ______ integer.
If the binarySearch()
method does not find the element, it returns a ______ integer.
Match the following ArrayList
methods with their descriptions:
Match the following ArrayList
methods with their descriptions:
Which method is available in java.util.ArrayList
to convert it into an array?
Which method is available in java.util.ArrayList
to convert it into an array?
Flashcards
What is an ArrayList?
What is an ArrayList?
Linear collection/list of objects; must import java.util.ArrayList.
ArrayList and primitive data types
ArrayList and primitive data types
Primitive data types need to be wrapped with their respective wrapper classes to be stored in an ArrayList.
What is an element?
What is an element?
An object in an ArrayList.
What is an index?
What is an index?
Signup and view all the flashcards
What does .get() method do?
What does .get() method do?
Signup and view all the flashcards
What does .add() method do?
What does .add() method do?
Signup and view all the flashcards
What does .remove() method do?
What does .remove() method do?
Signup and view all the flashcards
What does .set() method do?
What does .set() method do?
Signup and view all the flashcards
What does .size() method do?
What does .size() method do?
Signup and view all the flashcards
What does .isEmpty() method do?
What does .isEmpty() method do?
Signup and view all the flashcards
What does .contains() method do?
What does .contains() method do?
Signup and view all the flashcards
What does .indexOf() method do?
What does .indexOf() method do?
Signup and view all the flashcards
What does .clear() method do?
What does .clear() method do?
Signup and view all the flashcards
What are Arrays?
What are Arrays?
Signup and view all the flashcards
What are indexes?
What are indexes?
Signup and view all the flashcards
How to access elements in an array?
How to access elements in an array?
Signup and view all the flashcards
How to get the length of an array?
How to get the length of an array?
Signup and view all the flashcards
Array memory allocation
Array memory allocation
Signup and view all the flashcards
Arrays referencing each other
Arrays referencing each other
Signup and view all the flashcards
How to copy an entire array
How to copy an entire array
Signup and view all the flashcards
What does for-each loop do?
What does for-each loop do?
Signup and view all the flashcards
What are parallel arrays?
What are parallel arrays?
Signup and view all the flashcards
Partially filled array
Partially filled array
Signup and view all the flashcards
What is Arrays class?
What is Arrays class?
Signup and view all the flashcards
What does sort method do?
What does sort method do?
Signup and view all the flashcards
What does binarySearch method do?
What does binarySearch method do?
Signup and view all the flashcards
What does overloaded mean?
What does overloaded mean?
Signup and view all the flashcards
Key differences: Arrays vs. ArrayLists
Key differences: Arrays vs. ArrayLists
Signup and view all the flashcards
ArrayList to Array conversion
ArrayList to Array conversion
Signup and view all the flashcards
Array to ArrayList conversion
Array to ArrayList conversion
Signup and view all the flashcards
Study Notes
Arrays and ArrayLists
- Storing similar data or objects is common in programming
- Declaring variables for each data point is cumbersome
- Java offers several approaches for managing collections: ArrayLists, Arrays, Sets, Maps, etc.
ArrayLists
- ArrayLists must be imported from the java.util.ArrayList package
- An ArrayList is a linear collection or list of objects
- Primitive data types must be wrapped in their corresponding classes for storage in an ArrayList
- int x = 5 cannot be stored directly as an int in an ArrayList, instead Integer y = new Integer(5) should be used
- ArrayLists can dynamically grow or shrink in size as needed
- You can add or remove objects from an Arraylist at any time
Accessing Elements of an ArrayList
- Objects in an ArrayList are referred to as elements
- An Element's position in the list is its index
- The first element is at index 0
- The last element's index is positioned at .size() - 1
- Elements in an ArrayList can be accessed using the .get() method
- A valid integer index between zero and the size of the array must be used to pull elements
- An IndexOutOfBounds exception will be caused if the index is outside of these bounds
ArrayList Methods
- add(): Appends an element to the end of the list or inserts an element at a specified index
- clear(): Removes all elements from the list
- contains(): Determines if the list contains a specified element
- get(): Returns the element at a specified index
- isEmpty(): Checks if the list has no elements
- indexOf(): Returns the index of the first occurrence of a specified element, or -1 if not found
- remove(): Removes the element at a specified position or the first element matching a given object
- set(): Replaces an element with another element, returning the previous one
- size(): Returns the number of elements in the list
Arrays
- Arrays share similarities with ArrayLists
- Arrays are fixed in size once declared
- When declaring an array, the data type and the number of elements must be specified
- Arrays can store any type, but must only store elements of that type
- Arrays can store primitives, without needing wrappers
Accessing Elements in Arrays
- Elements are accessed using indexes, also called positions or subscripts
- Indexes are numbered starting from 0, similiar to ArrayList
- Square brackets are used instead of .get() to access elements
- A sample call is: String person = people[2]
- The .length field is used to determine array length
- An example call is: int numPeople = people.length
Array properties
- Arrays can be initialized with values upon creation or filled later
- Care must be taken when accessing array elements that have not been assigned values
- Memory for an array is allocated upon declaration
- This is because the program knows the length and size of elements
- With
int[] myArray = new int[4]
the elements have space saved in memory as myArray[0] myArray[1] myArray[2] myArray[3] - The array variable stores the address of these locations
Array Memory
- Assigning one array to another (e.g., otherArray = myArray) means both variables now point to the same memory locations
- Modifying one array will affect the other, as they share the same reference
- To create separate copies, each element must be copied individually using a loop
- Strings and other objects are stored by reference in the array
Enhanced For Loops
- Enhanced for loops, or for-each loops, provide a simpler syntax for iterating over arrays and ArrayLists
- They avoid having to manage indexes manually
- Enhanced for loops are more efficient when iterating over all objects in a collection
- Enhanced for loops do not work when:
- Arrays need to be iterated backwards
- Accessing elements of two arrays at once
- Attempting to assign new values
- Only partially filled arrays need to be iterated
Parallel and Partially Filled Arrays
- Parallel arrays are those where elements at the same indexes in each array are related (e.g., student names and corresponding student numbers)
- Arrays can be declared as empty and filled in later, resulting in partially filled arrays
Main Method Arguments
- The main method accepts an array of strings as input, by convention
- These are can be called arguments or args
- It is common to iterate these arguments as part of program execution
Arrays Class Utilities
- The Arrays class that can be imported from java.utils.Arrays provides useful array methods
- Static method
sort
sorts elements of an array in ascending order - Static method
binarySearch
finds a given element in an array and returnselement-number
- Must be sorted to use
Sort Details
- The
sort
method is static - Accepts multiple types of input
- Arrays of primitives (int, char, byte, float, double), arrays of Objects that have
compareTo()
methods implemented - When an array's elements are being called by different kinds of input it is referred to as being overloaded
Binary Search
- Similarly to
Sort
,binarySearch()
is an overloaded method. - Accepts two arguments: the array to be searched and the value to search for
- This requires an ascending sorted array
- The element index of the search is the return
- If the element can't be found, a negative integer will be returned
Array Problems
- Arrays are often tasked with
- Sum of elements
- Average/mean of elements
- Determining the greatest/smallest of its elements
- Determine index of max/min element
Arrays vs. ArrayLists
- Arrays: store objects or primitives, fixed length,
length
field, must specify elements specified type - ArrayLists: store objects, variable length,
size()
method, store elements of any type, but not recemmended
Converting Between Arrays and ArrayLists
- To convert an ArrayList to an array, use the toArray() method
- To convert an array to an ArrayList, use a for loop or the addAll() method from java.util.Collections
Studying That Suits You
Use AI to generate personalized quizzes and flashcards to suit your learning preferences.
Related Documents
Description
Explore the use of ArrayLists in Java for storing and managing collections of objects. Learn how ArrayLists dynamically grow or shrink and how to access elements using their index positions. Understand the concept of primitive data types and how they are wrapped.