Skip to main content
Contents
Dark Mode Prev Up Next Scratch ActiveCode Profile
\(
\newcommand{\lt}{<}
\newcommand{\gt}{>}
\newcommand{\amp}{&}
\definecolor{fillinmathshade}{gray}{0.9}
\newcommand{\fillinmath}[1]{\mathchoice{\colorbox{fillinmathshade}{$\displaystyle \phantom{\,#1\,}$}}{\colorbox{fillinmathshade}{$\textstyle \phantom{\,#1\,}$}}{\colorbox{fillinmathshade}{$\scriptstyle \phantom{\,#1\,}$}}{\colorbox{fillinmathshade}{$\scriptscriptstyle\phantom{\,#1\,}$}}}
\)
Section 14.4 Writing-ArrayLists-WE2-P1
Subgoals for Writing ArrayLists.
Importing the ArrayList class
Before using ArrayList, import it from the java.util package:
import java.util.ArrayList;
Declaring an ArrayList variable
Determine the type of objects to be stored (use wrapper classes for primitives, e.g., Integer instead of int)
Determine the name of the ArrayList variable
Use syntax:
ArrayList<DataType> name;
Instantiating an ArrayList object
Use the
new keyword with the constructor to create a new ArrayList object (When ArrayLists are instantiated, they are empty and have a size of 0.)
Adding elements to an ArrayList
To add to the end of an ArrayList, use:
listName.add(valueToBeAdded)
To add an element at a specific location in an ArrayList, use:
listName.add(index, valueToBeAdded) where index is within the bounds 0 to
listName.size()
Accessing an element in an ArrayList
Determine the index of the element to be accessed
Use:
listName.get(index) to retrieve the element
Ensure the index is within bounds: 0 to
listName.size() - 1, otherwise an IndexOutOfBoundsException occurs
Changing a value in an ArrayList
Determine the index of the element to be changed
Determine the new value or expression to assign
Use:
listName.set(index, newValue) to update the value
Ensure the index is within bounds: 0 to
listName.size() - 1, otherwise an IndexOutOfBoundsException occurs
Decide whether accessing all elements, updating, or accessing a subset
If only
accessing elements, use an enhanced for (for-each) loop:
for (DataType item : listName) - iterates from first to last, storing a copy of each element in
item
If updating or using indices, use a traditional for loop:
Initialize loop control variable to 0 (or
listName.size() - 1 for reverse)
Set condition:
i < listName.size() (or
i >= 0 for reverse)
Increment or decrement loop control variable appropriately
Use Subgoals 5 or 6 to access or change values as appropriate
Passing an ArrayList as an argument
Check if the method expects an ArrayList argument (check documentation or method signature)
When calling a method, pass a reference to an ArrayList (usually variable name) as an argument in the method call.
Note: changes to elements in the ArrayList that are done inside the method will persist
Subsection 14.4.1
Exercises Exercises
1.
Q4: Put the following code in order so that the values inserted into the ArrayList are stored in increasing order.
import java.util.ArrayList;
import java.util.Scanner;
---
ArrayList<Integer> increasing = new ArrayList<Integer>();
---
System.out.println("Enter values to be inserted, -1000 to end");
---
Scanner scan = new Scanner(System.in);
---
int value = scan.nextInt();
---
while (value != -1000) {
---
int insertAtIndex = 0;
---
while(insertAtIndex < increasing.size() && increasing.get(insertAtIndex) < value) {
---
insertAtIndex++;
}
---
increasing.add(insertAtIndex, value);
---
System.out.println("Enter values to be inserted, -1000 to end");
---
value = scan.nextInt();
---
}
2.
Q5: Put the following code in order to create a program that will declare and instantiate an ArrayList of 10 random values between 1 and 100 and then find the maximum value in the ArrayList.
import java.util.*;
public class main{
public static void main (String[] args) {
---
Random r = new Random();
ArrayList<Integer> list = new ArrayList<>();
---
for (int i = 0; i < 10; i++) {
list.add(r.nextInt(100) + 1);
System.out.println("list["+i+"] is " + list.get(i));
}
---
int max = list.get(0);
---
for (int num: list) {
---
if (num > max)
---
max = num;
---
}
---
System.out.println("Maximum value is " + max);
---
}
}
3.
Q6: Put the following code in order to create a method that will find the last occurrence of a target value and return the index of where that value is located.
public static int find (ArrayList<Integer> arr, int target) {
---
int loc = -1;
---
for (int i = 0; i < arr.size(); i++) {
---
if (arr.get(i) == target)
---
loc = i;
---
}
---
return loc;
}
4.
Q7: Put the following code in order to create a method that will return true if a target value is found in the ArrayList and false otherwise.
public static boolean find (ArrayList<Integer> arr, int target) {
---
for (int i = 0; i < arr.size(); i++) {
---
if (arr.get(i) == target)
---
return true;
---
}
---
return false;
}
You have attempted
of
activities on this page.