In: Computer Science
////Fixme(1) add a statement to import ArrayList class
public class ListManipulation {
public static void main(String[] args) {
//Fixme(2) create an ArrayList of integers and name the ArrayList
list.
//Fixme(3) add the following numbers to the list: 10, 15, 7, -5,
73, -11, 100, 20, 5, -1
displayList(list);
System.out.println();
displayListBackwards(list);
}
public static void displayList(ArrayList<Integer> list)
{
for(Integer i: list)
System.out.print(i + " ");
}
//Fixme(4) define a method displayListBackwards, which takes an
ArrayList as a parameter and returns nothing.
//The method displays each element in the list in the reverse
order, and adds a space after each element.
//Fixme(5) define a method maxElement, which takes an ArrayList as
a parameter and returns the maximum element in the list.
//Fixme(6) define a method searchElement, which takes two
parameters.
//The first parameter is an ArrayList and the second is an int type
value.
//The method searches for value in the list.
//If the value is found in the list, the method returns its index,
otherwise, it returns -1.
//Fixme(7) define a method eventCount, which takes an ArrayList as
a parameter and returns the number of even element in the
list.
}
////Fixme(1) add a statement to import ArrayList class
import java.util.ArrayList;
public class ListManipulation {
public static void main(String[] args) {
//Fixme(2) create an ArrayList of integers and name the ArrayList list.
ArrayList<Integer> list = new ArrayList<Integer>();
//Fixme(3) add the following numbers to the list: 10, 15, 7, -5, 73, -11, 100, 20, 5, -1
list.add(10);
list.add(15);
list.add(7);
list.add(-5);
list.add(73);
list.add(-11);
list.add(100);
list.add(20);
list.add(5);
list.add(-1);
displayList(list);
System.out.println();
displayListBackwards(list);
}
public static void displayList(ArrayList<Integer> list) {
for (Integer i : list)
System.out.print(i + " ");
System.out.println();
}
//Fixme(4) define a method displayListBackwards, which takes an ArrayList as a parameter and returns nothing.
//The method displays each element in the list in the reverse order, and adds a space after each element.
public static void displayListBackwards(ArrayList<Integer> list) {
for (int i = 0; i < list.size(); i++) {
System.out.print(list.get(i) + " ");
}
System.out.println();
}
//Fixme(5) define a method maxElement, which takes an ArrayList as a parameter and returns the findMaximumIndex element in the list.
public static int maxElement(ArrayList<Integer> list) {
int max = list.get(0);
for (int i = 0; i < list.size(); i++) {
if (list.get(i) > max)
max = list.get(i);
}
return max;
}
//Fixme(6) define a method searchElement, which takes two parameters.
//The first parameter is an ArrayList and the second is an int type value.
//The method searches for value in the list.
//If the value is found in the list, the method returns its index, otherwise, it returns -1.
public static int searchElement(ArrayList<Integer> list, int value) {
for (int i = 0; i < list.size(); i++) {
if (list.get(i) == value)
return i;
}
return -1;
}
//Fixme(7) define a method eventCount, which takes an ArrayList as a parameter and returns the number of even element in the list.
public static int eventCount(ArrayList<Integer> list) {
int count = 0;
for (int i = 0; i < list.size(); i++) {
if (list.get(i) % 2 == 0)
++count;
}
return count;
}
}