Podcast
Questions and Answers
When is it most appropriate to utilize parallel arrays?
When is it most appropriate to utilize parallel arrays?
- When needing to perform complex matrix operations.
- When elements at the same index in multiple arrays are related. (correct)
- When memory usage needs to be optimized at the expense of processing speed.
- When needing to store elements of different data types in a single structure.
Arrays in Java can dynamically change their size after initialization.
Arrays in Java can dynamically change their size after initialization.
False (B)
What method is used for sorting the elements of an array in ascending order using the Arrays
class in Java?
What method is used for sorting the elements of an array in ascending order using the Arrays
class in Java?
sort
The enhanced for
loop, often called the ______ loop, simplifies iterating over arrays and ArrayLists.
The enhanced for
loop, often called the ______ loop, simplifies iterating over arrays and ArrayLists.
Match the following ArrayList
methods with their descriptions:
Match the following ArrayList
methods with their descriptions:
What is the primary difference between an Array and an ArrayList in Java?
What is the primary difference between an Array and an ArrayList in Java?
Primitive data types can be directly stored in an ArrayList
without needing to be wrapped in their corresponding wrapper classes.
Primitive data types can be directly stored in an ArrayList
without needing to be wrapped in their corresponding wrapper classes.
What type of exception is thrown when trying to access an ArrayList
element using an index that is out of bounds?
What type of exception is thrown when trying to access an ArrayList
element using an index that is out of bounds?
To use the ArrayList
class in Java, you must import it from the java.util
package by using the statement: import java.util.______
.
To use the ArrayList
class in Java, you must import it from the java.util
package by using the statement: import java.util.______
.
Match array or ArrayList
characteristics with the appropriate data structure.
Match array or ArrayList
characteristics with the appropriate data structure.
When would it be most appropriate to use the enhanced for
loop (for-each loop) in Java?
When would it be most appropriate to use the enhanced for
loop (for-each loop) in Java?
When assigning one array variable to another (e.g., otherArray = myArray
), a completely new copy of the array is created in memory.
When assigning one array variable to another (e.g., otherArray = myArray
), a completely new copy of the array is created in memory.
How can the length of an array be determined in Java?
How can the length of an array be determined in Java?
In Java, the main method of a program accepts an ______ of strings as input, which are referred to as command-line arguments.
In Java, the main method of a program accepts an ______ of strings as input, which are referred to as command-line arguments.
Match the following descriptions with either Arrays or ArrayLists in Java:
Match the following descriptions with either Arrays or ArrayLists in Java:
How are strings and other objects stored in Arrays in Java?
How are strings and other objects stored in Arrays in Java?
The binarySearch()
method in the Arrays
class can be used on unsorted arrays.
The binarySearch()
method in the Arrays
class can be used on unsorted arrays.
What is the term for a method that accepts different kinds of input?
What is the term for a method that accepts different kinds of input?
The java.util.Arrays
class offers a ______ method to efficiently search for elements in a sorted array.
The java.util.Arrays
class offers a ______ method to efficiently search for elements in a sorted array.
Match the actions with the appropriate result when using arrays:
Match the actions with the appropriate result when using arrays:
What does the clear()
method do when called on an ArrayList
?
What does the clear()
method do when called on an ArrayList
?
The index of the first element in an ArrayList
or array is typically 1.
The index of the first element in an ArrayList
or array is typically 1.
After removing an element from an ArrayList
, what happens to the indexes of subsequent elements?
After removing an element from an ArrayList
, what happens to the indexes of subsequent elements?
The indexOf()
method in ArrayList
returns ______ if the specified element is not found in the list.
The indexOf()
method in ArrayList
returns ______ if the specified element is not found in the list.
Match each method from the Arrays class with its function.
Match each method from the Arrays class with its function.
In the context of arrays, what is a partially filled array?
In the context of arrays, what is a partially filled array?
The toArray()
method of an ArrayList
returns a primitive array.
The toArray()
method of an ArrayList
returns a primitive array.
What class is the addAll()
method from, and what data structure does it help in converting to an ArrayList
from?
What class is the addAll()
method from, and what data structure does it help in converting to an ArrayList
from?
To convert an ArrayList
to an array, you can use the toArray()
method of the ______ class.
To convert an ArrayList
to an array, you can use the toArray()
method of the ______ class.
Match the following descriptions with the appropriate reason why the enhanced for-loop should not be used:
Match the following descriptions with the appropriate reason why the enhanced for-loop should not be used:
Flashcards
Arrays and ArrayLists
Arrays and ArrayLists
A way to store multiple similar data or objects.
ArrayList
ArrayList
A list-like collection of objects that can grow or shrink.
ArrayList import
ArrayList import
Imported as java.util.ArrayList.
ArrayList Definition
ArrayList Definition
Signup and view all the flashcards
Primitive data types in ArrayList
Primitive data types in ArrayList
Signup and view all the flashcards
Element
Element
Signup and view all the flashcards
Index
Index
Signup and view all the flashcards
.get() method
.get() method
Signup and view all the flashcards
add() Method
add() Method
Signup and view all the flashcards
clear() Method
clear() Method
Signup and view all the flashcards
contains() Method
contains() Method
Signup and view all the flashcards
get() Method
get() Method
Signup and view all the flashcards
indexOf() Method
indexOf() Method
Signup and view all the flashcards
isEmpty() method
isEmpty() method
Signup and view all the flashcards
remove() method
remove() method
Signup and view all the flashcards
set() Method
set() Method
Signup and view all the flashcards
size() method
size() method
Signup and view all the flashcards
Arrays
Arrays
Signup and view all the flashcards
Arrays Fixed Size
Arrays Fixed Size
Signup and view all the flashcards
Array Data Types
Array Data Types
Signup and view all the flashcards
Access Array Elements
Access Array Elements
Signup and view all the flashcards
Array's .length field
Array's .length field
Signup and view all the flashcards
Array Memory Allocation
Array Memory Allocation
Signup and view all the flashcards
Array Variable
Array Variable
Signup and view all the flashcards
Enhanced For Loop
Enhanced For Loop
Signup and view all the flashcards
Parallel Arrays
Parallel Arrays
Signup and view all the flashcards
Partially Filled Arrays
Partially Filled Arrays
Signup and view all the flashcards
Array of Strings
Array of Strings
Signup and view all the flashcards
Arrays Class
Arrays Class
Signup and view all the flashcards
sort() Method
sort() Method
Signup and view all the flashcards
binarySearch() Method
binarySearch() Method
Signup and view all the flashcards
Study Notes
- Storing similar data or objects commonly occurs in a program.
- Declaring a new variable for storing a new data point can be cumbersome
- Java has approaches for working with collections of things, including ArrayLists, Arrays, Sets, and Maps.
ArrayLists
- ArrayLists must be imported as
java.util.ArrayList
. - An ArrayList is a linear collection/list of objects requiring the wrapping primitive data types with their respective classes before stored.
- An ArrayList is a data structure that can grow or shrink in size as needed, allowing objects to be added or removed at any time.
Accessing Elements of an ArrayList
- An object in an ArrayList is referred to as an element of the list.
- An element's position in the list is referred to as its index.
- The first element starts at position 0 and the last element is at position .size() - 1.
- An element can be accessed using ArrayList's
.get()
method using a valid integer between zero and the maximum size of the array. - When an index is outside the bounds of the ArrayList, an
IndexOutOfBounds
exception gets thrown.
Working with ArrayLists Example
- Instantiate an ArrayList of Strings:
ArrayList<String> people = new ArrayList();
- Add the names "Alice", "Bob", "Charlie", and "David" to the ArrayList using
.add
method. - Printing the ArrayList yields:
[Alice, Bob, Charlie, David]
- Remove "Alice" by using
.remove
method. - Printing the ArrayList now shows:
[Bob, Charlie, David]
- Printing the
.size
of the ArrayList yields 3 - Printing the element at
.get(0)
prints "Bob" - Printing the element at
.get(4)
will cause the program to crash because it is an Invalid index.
ArrayList Methods
- An arraylist of strings will be called people
ArrayList<String> people = new ArrayList()
.add()
to append a given element to the end of the ArrayList or specify a position to insert an element, shifting following elements down. Example:people.add("Jaime")
-people.add(4, "Jaime")
.clear()
removes all of the elements from a list.contains()
returns true if the list contains the specified element. Example:boolean found = people.contains("Jaime")
..get()
returns an element at a specified position in the list. Example:String person = people.get(4)
.indexOf()
returns the index of the first occurrence of the specified element in the list, or -1 if the list does not contain the element. Example:int pos = people.indexOf("Jaime")
.isEmpty()
returns true if the list has no elements. Example:boolean empty = people.isEmpty()
.remove()
can remove either the element at a specified position in the list, or the first element matching a given object; It then returns the deleted element and shifts other elements up.String removed = people.remove(4)
-String removed = people.remove("Jaime")
..set()
replaces an element with another element, then returns the previous element. Example:String previous = people.set(4,"Jaime")
..size()
returns the number of elements in the array. Example:int numElts = people.size()
.
Arrays
- Arrays are similar to ArrayLists with key differences.
- Arrays are fixed in size and must specify the data type and number of elements.
- An array may be declared for any type, allowing storage of only elements of that specified type without wrappers.
Accessing Elements in Arrays
- Elements are accessed using indexes, also called positions, or subscripts.
- These indexes are numbered the same way as with an ArrayList using square brackets instead of
.get()
. - The length of an array is accessed using the array's
.length
field.
Array Examples Usage
- Initialize an array:
int[] daysInMonth = {31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31};
- To print the first element:
System.out.println(daysInMonth [0]);
- To print the last element:
System.out.println(daysInMonth[daysInMonth.length - 1]);
- To print the array length:
System.out.println(daysInMonth.length);
- Declaring an empty char array:
char[] monthLetters = new char[12];
- Setting an element:
monthLetters[0] = 'J';
.
Storage of Arrays in Memory
- When an array gets declared, all the memory gets allocated.
- The program knows how much memory to allocate, because it knows the length of the array, and the size of the elements the array will store, the elements have space reserved for them in memory.
- The array variable holds the address of the array's memory locations.
- Assigning one array to another copies the reference/address, not the array itself.
- When declaring and assigning another array the values are not copied to the new array eg
otherArray = myArray
, what ends up happening is thatotherArray
is assigned the reference not the values. - Modifying one of these arrays will modify both.
- Copying an array requires iterating through the array and copying values to the other array.
- After you copy all the elements with a
for
loop, the contents of an array are copied by value to the other array. - Primitive datatypes are stored in the array, Strings and Objects, the reference of these elements are stored in the array.
Enhanced For Loop
- The enhanced
for
loop simplifies the syntax iterating over all objects in an Array or ArrayList, managing the indexes automatically. - Example
for(int days: daysInMonth) { System.out.println(days); }
- Using the enhanced
for
loop can simplify code, but is not always applicable. - It does not allow iteration of an array backwards, accessing elements of two arrays at once, assigning new values, or iterate over arrays that are only partially filled.
- To use it effectively requires a standard loop with indexes.
Parallel Arrays
- Parallel arrays are encountered when there are two arrays where the elements at the same indexes are related.
- Using an array of student names and another for student numbers is an example.
- In one array, a student number is looked up and that index is used to find the name of the student in the other array.
Partially Filled Arrays
- Arrays can be declared as empty arrays to be filled in at a later time.
- When an array is declared as empty, it is useful to track how much the array gets filled.
Arrays Class
- Java provides an Arrays class that must be imported with
java.utils.Arrays
. - The Arrays class provides the
sort
method that can sort the elements of an array into ascending order. - The Arrays class provides the
binarySearch
method that can locate a given element in an array. - For
binarySearch
to work, the array must be sorted.
The Arrays Class: sort
sort
is a static method accepts different types arrays of primitives (int, long, short, char, byte, float, double).- The methods also accept any array of objects that have the compareTo() method (e.g., the String class which implements this method).
- A method is considered overloaded when it accepts different kinds of input.
- sort rearranges the array's elements in ascending order.
The Arrays Class: binarySearch
binarySearch()
is an overloaded method which accepts the the array that must be sorted and returns the index of the search element.- If the element is not found, the
binarySearch
method returns a negative integer.
Common Array Problems
- Common array problems include summing its elements, finding average or mean, or finding the largest/smallest elements.
Arrays versus ArrayLists
- Arrays store objects or primitives; and ArrayLists only store objects.
- ArrayLists can store elements of any type but is not recommended; Arrays can only store certain data types.
- ArrayList has
.size()
method and arrays have fixed length field. - The size of an array gets fixed, but the size of an Arraylist can easily be changed.
Converting from ArrayList to Array
- When you're needed to convert between Arraylist and Array, you can call method
toArray()
from java.util.ArrayList Class to achieve that.
Converting from Array to ArrayList
- To convert from Array to ArrayList, use a
for
loop or the.addAll()
method from theJava.util.Collections
class.
Studying That Suits You
Use AI to generate personalized quizzes and flashcards to suit your learning preferences.
Related Documents
Description
Learn how to store and access data using ArrayLists in Java. ArrayLists are a dynamic data structure that allows you to add or remove elements as need. Access elements using the .get() method with a valid index.