28. (14 marks) Consider ArrayLists and show the command(s) required to complete
ID: 3573377 • Letter: 2
Question
28. (14 marks) Consider ArrayLists and show the command(s) required to complete the following tasks. Assume the tasks are completed in order. Label each task clearly.
a. (2 marks) Create a new ArrayList called list that can hold integers.
b. (2 marks) Add three integers of your choice to the list.
c. (2 marks) Change the second integer in the list to your student number.
d. (2 marks) Remove the first occurrence of the value 10 from your list.
e. (2 marks) Determine and store in a variable the number of elements that are currently in the list.
f. (4 marks) Using a for loop, determine the largest value in your list (assume there are more values than what you entered in part a.). Print the value AND the index where it occurs. This is not a method/function questions, just write the Java statements to complete the task.
Explanation / Answer
Please follow the code and comments for description :
CODE :
import java.util.ArrayList; // required imports
public class operationOnArrayList { // class to run the code
public static void main(String[] args) { // driver method
ArrayList<Integer> al = new ArrayList<>(); // declare an array list (a)
System.out.println("Adding Elements to the List....");
al.add(5); // add 3 integers of your choice (b)
al.add(7);
al.add(10);
System.out.println(" Replacing the Second Element with the Student ID...");
al.add(1, 123); // change the second element to your student ID (c)
System.out.println(" Removing the First Occurrence of the Value 10 from the List...");
al.remove(new Integer(10)); // removes the first occurrence of the value 10 from your list (d)
int count = al.size(); // determines and stores the number of elements that are currently in the list (e)
System.out.println(" Count of Elements that are Currently in the list : " + count);
System.out.println(" Adding more Elements to the List...");
al.add(151);
al.add(605);
al.add(701);
al.add(457);
al.add(221);
al.add(990);
al.add(568);
al.add(602);
al.add(177);
System.out.println(" Getting the Highest Value and the respective Index of the List...");
int highest = al.get(0);
int hIndex = 0;
for (int i = 1; i < al.size(); i++) { // Using a for loop, determining the largest value in the list (f)
int curValue = al.get(i);
if (curValue > highest) {
highest = curValue;
hIndex = i;
}
}
System.out.println("The Highest Value is : " + highest + ", found at Index : " + hIndex);
}
}
OUTPUT :
Adding Elements to the List....
Replacing the Second Element with the Student ID...
Removing the First Occurrence of the Value 10 from the List...
Count of Elements that are Currently in the list : 3
Adding more Elements to the List...
Getting the Highest Value and the respective Index of the List...
The Highest Value is : 990, found at Index : 8
DESCRIPTION :
The respective lines of requirement or the requried statements are used against the comments.
Hope this is helpful.
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.