COMP1010 ArrayList Reference

0.0(0)
studied byStudied by 2 people
learnLearn
examPractice Test
spaced repetitionSpaced Repetition
heart puzzleMatch
flashcardsFlashcards
Get a hint
Hint

Assuming the existence of an ArrayList of Integer objects named values, add the value 1729 to the end of the list.

Get a hint
Hint

values.add(1729);

Get a hint
Hint

Assuming the existence of an ArrayList of Integer objects named values that contains zero or more items, add the value 1729 to the beginning of the list. 

Get a hint
Hint

values.add(0, 1729);

Card Sorting

1/14

encourage image

There's no tags or description

Looks like no tags are added yet.

Study Analytics
Name
Mastery
Learn
Test
Matching
Spaced

No study sessions yet.

15 Terms

1
New cards

Assuming the existence of an ArrayList of Integer objects named values, add the value 1729 to the end of the list.

values.add(1729);

2
New cards

Assuming the existence of an ArrayList of Integer objects named values that contains zero or more items, add the value 1729 to the beginning of the list. 

values.add(0, 1729);

3
New cards

Assuming the existence of an ArrayList of Integer objects named values that contains zero or more items, add the value 1729 to the middle of the list. 

If the list contains an even number of items, the middle is easy. 

If the list contains an odd number of items, insert the item to the LEFT of the current middle item.

int middle = values.size()/2;

values.add(middle, 1729);

4
New cards

Assuming the existence of an ArrayList of Integer objects named values that contains at least 5 items, removes the fifth item in the list.

values.remove(4);

5
New cards

Assuming the existence of an ArrayList of Integer objects named values, removes the first occurrence of item 5 (if any) in the list.

int indexOfFive = values.indexOf(5);

if(indexOfFive != -1){

values.remove(indexOfFive);

}

6
New cards

Assuming the existence of an ArrayList of Integer objects named values that contains at least 4 items, change the fourth item to 888.

values.set(3, 888);

7
New cards

Assuming the existence of an ArrayList of Integer objects named values , store the first and last indices of item 1729 in variables idx1 and idx2 respectively.

int idx1 = -1;

int idx2 = -1;

int target = 1729;

for(int i = 0 ; i < values.size(); i++){

if (values.get(i) == target){

if (idx1 == -1){

idx1 = i;

}

idx2 = i;

}

}

8
New cards

Assuming the existence of an ArrayList of Integer objects named values, write a piece of code that adds up all the numbers in values and stores the result in a variable total.

int total = 0;

for(int i = 0; i < values.size(); i++){

total += values.get(i);

}

9
New cards

Assuming the existence of an ArrayList of Integer objects named values, display each item of the list from back to front (last item should be displayed first, and the first item should be displayed last), each item on a new line.

for(int i = values.size()-1; i >= 0; i--){

System.out.println(values.get(i));

}

10
New cards

Assuming the existence of an ArrayList of Integer objects named values, write a piece of code that stores the count of odd numbers in values and stores the result in a variable countOdd.

int countOdd = 0;

for (int i = 0; i < values.size(); i++){

if(values.get(i) %2 != 0){

countOdd++;

}

}

11
New cards

Assuming the existence of an ArrayList of Integer objects named values, write a piece of code that adds up all the odd numbers in values and stores the result in a variable totalOdd.

int totalOdd = 0;

for (int i = 0 ; i < values.size(); i++){

int currentVal = values.get(i);

if(currentVal %2 != 0){

totalOdd += currentVal;

}

}

12
New cards

Complete the function named sumRange that, when passed an ArrayList of Integer objects and two integers representing the starting and ending index, returns the sum of all the numbers in the ArrayList data between the indices idx1 and idx2. Return 0 if the list is null or empty or if either of the indices is invalid.

int sumRange (ArrayList<Integer> data, int idx1, int idx2){

if(data==null||data.isEmpty()){

return 0;

}

if(idx1 < 0 || idx1 >= data.size()){

return 0;

}

if(idx2 < 0|| idx2 >= data.size()){

return 0;

}

int sum = 0;

for(int i = idx1; i <= idx2; i++){

sum+= data.get(i);

}

return sum;

}

13
New cards

Define a function named isAscending that when passed an ArrayList of Integer objects, returns true if it is in ascending order, false otherwise.

Ascending order is when every item is less than or equal to the item after it.

public static boolean isAscending(ArrayList<Integer> data){

if(data==null){

return false;

}

for(int i = 0; i < data.size()-1; i++){

if(data.get(i) > data.get(i+1)){

return false;

}

}

return true;

}

14
New cards

Define a function named areMutuallyReverse that when passed two ArrayList of Integer objects, returns true if they are mutually reverse of each other, false otherwise.

public static boolean areMutuallyReverse(ArrayList<Integer> list1, ArrayList<Integer> list2){

if (list1 null || list2 null) {

return false;

}

if (list1.size() != list2.size()) {

return false;

}

ArrayList<Integer> reversedList = new ArrayList<>(list1);

Collections.reverse(reversedList);

return reversedList.equals(list2);

}

15
New cards

Define a function named totals, that when passed an ArrayList of ArrayList of Integer objects (you read that right!), returns an ArrayList of Integer objects holding the sum of each sub-list.

public static ArrayList<Integer> totals(ArrayList<ArrayList<Integer>> listOfLists) {

if (listOfLists == null) {

return null;

}

ArrayList<Integer> result = new ArrayList<>();

for (ArrayList<Integer> subList : listOfLists) {

if (subList == null) {

result.add(null);

} else {

int sum = 0;

for (Integer number : subList) {

sum += (number != null) ? number : 0;

}

result.add(sum);

}

}

return result;

}