Question

Question 1: Write the following method that returns a new ArrayList. The new list contains the...

Question 1: Write the following method that returns a new ArrayList. The new list contains the nonduplicate elements from the original list,

public static <E> ArrayList<E> removeDuplicates(ArrayList<E> list)

Question 2:

Implement the following method that returns the maximum element in an array:

public static <E extends Comparable<E>> E max(E[] list)

Each question is its own code and it must be able to run. Please show details .

Thank you in advance!

Homework Answers

Answer #1

import java.util.ArrayList;
import java.util.Arrays;

public class RemoveDuplicates {

    public static <E> ArrayList<E> removeDuplicates(ArrayList<E> list) {
        ArrayList<E> newList = new ArrayList<>();
        for(int i = 0; i < list.size(); ++i) {
            if(!newList.contains(list.get(i))) {
                newList.add(list.get(i));
            }
        }
        return newList;
    }

    public static void main(String[] args) {
        ArrayList<Integer> numbers = new ArrayList<>(Arrays.asList(1, 8, 2, 8, 1, 4, 2, 6));
        System.out.println("Original list = " + numbers);
        System.out.println("List after removing duplicates = " + removeDuplicates(numbers));
    }
}

public class GenericMaxComparable {

    public static <E extends Comparable<E>> E max (E [] list) {
        E maxValue = list[0];
        for(int i = 0; i < list.length; i++) {
            if(list[i].compareTo(maxValue) > 0) {
                maxValue = list[i];
            }
        }
        return maxValue;
    }

    public static void main(String[] args) {
        String[] colors = {"Red","Green","Blue"};
        Integer[] numbers = {1, 2, 3};
        Double[] doubles = {3.0, 5.9, 2.9};

        System.out.println("Colors: " + max(colors));
        System.out.println("Numbers: " + max(numbers));
        System.out.println("Circle11 Radius: " + max(doubles));
    }

}



Know the answer?
Your Answer:

Post as a guest

Your Name:

What's your source?

Earn Coins

Coins can be redeemed for fabulous gifts.

Not the answer you're looking for?
Ask your own homework help question
Similar Questions
Build two arrays[ ] (Integer and String) and convert them to two ArrayLists and write two...
Build two arrays[ ] (Integer and String) and convert them to two ArrayLists and write two overloaded generic static search method to find the index locations of a specified value. One of the search methods applies to the array type while the other (overloaded) search method applies to the collection type. Implement the following generic linear search method and write a client program to display results: (Here is the header) public static <E extends Comparable<E>> int search(E[] list, E key)...
Take a look at the file GenericMethods.java. There are three methods you must implement: ·public static...
Take a look at the file GenericMethods.java. There are three methods you must implement: ·public static <T extends Comparable<T>> int findMin(T[] arr): Iterate through the array to find the index of the smallest element in the array. If there are two elements with the smallest value, the method should return the index of the first one. The method should run in O(n). ·public static <T extends Comparable<T>> int findMinRecursive(T[] arr): Should behave just like findMin, but should be implemented recursively....
based on the code below, answer the questions Question 1: The LinkedList class uses another class...
based on the code below, answer the questions Question 1: The LinkedList class uses another class called Node (which is defined inside LinkedList.java). What are the fields in the Node class? Question 2: The Node class uses generics. Why? Question 3: The linkFirst method contains the following lines of code: if (f == null) last = newNode; else f.prev = newNode; What is the value of the linked list’s size attribute at this point in the code if f ==...
java Write a single Java statements to accomplish each of the following: a) Displaythevalueoftheseventhelementofcharacterarraych. b) Considering...
java Write a single Java statements to accomplish each of the following: a) Displaythevalueoftheseventhelementofcharacterarraych. b) Considering “Scanner in = new Scanner (System.in);”, input a value into element 5 of one-dimensional double array nums. c) Initialize each of the four elements of the one-dimensional integer array test to 7. d) Declare and create an array called table as a float array that has four rows and three columns. e) Considering the following ArrayList declaration, insert “test” at the fourth position (index...
THIS IS A JAVA PROGRAM THAT NEEDS TO BE WRITTEN Write a recursive method void reverse(ArrayList<Object>...
THIS IS A JAVA PROGRAM THAT NEEDS TO BE WRITTEN Write a recursive method void reverse(ArrayList<Object> obj) that reverses an ArrayList of any type of object. For example, if an ArrayList held 4 strings: "hi", "hello", "howdy", and "greetings" the order would become "greetings", "howdy", "hello", and "hi". Implement a recursive solution by removing the first object, reversing the ArrayList consisting of the remaining Objects, and combining the two. Use the following class ArrayListReverser to write and test your program....
Write a method name maxElement, which returns the largest value in an array that is passed...
Write a method name maxElement, which returns the largest value in an array that is passed as an argument. The method must use recursion to find the largest element. Demonstrate the method in a program. Write the JAVA CODE on the paper that is provided. (Put your name on every piece of paper) Use the following array to test the method. Int [] numbers = {2,12,1999,99,100,4,7,300} PROGRAMMING LANGUAGE- JAVA. Please answer the question ASAP. Thanks in advance!
1-To create an ArrayList of strings, which of the following is correct? `a ArrayList<String> list =...
1-To create an ArrayList of strings, which of the following is correct? `a ArrayList<String> list = new ArrayList<String>(); b ArrayList<String> list = new String<String>​[]; c ArrayList<String> list = new ArrayList<String>; d ArrayList​[String] list = new ArrayList​[String]; 2-All instance variables? a Are assigned a default value if they are not initialized b Must be initialized as part of their declaration c Are assigned an arbitrary value if they are not initialized d Must be initialized by the constructor 3-An instance method...
[Java] I'm not sure how to implement the code. Please check my code at the bottom....
[Java] I'm not sure how to implement the code. Please check my code at the bottom. In this problem you will write several static methods to work with arrays and ArrayLists. Remember that a static method does not work on the instance variables of the class. All the data needed is provided in the parameters. Call the class Util. Notice how the methods are invoked in UtilTester. public static int min(int[] array) gets the minimum value in the array public...
main The main method instantiates an ArrayList object, using the Car class. So, the ArrayList will...
main The main method instantiates an ArrayList object, using the Car class. So, the ArrayList will be filled with Car objects. It will call the displayMenu method to display the menu. Use a sentinel-controlled loop to read the user’s choice from stdin, call the appropriate method based on their choice, and redisplay the menu by calling displayMenu. Be sure to use the most appropriate statement for this type of repetition. displayMenu Parameters:             none Return value:          none Be sure to use...
Q: Implement an equals method for the ADT list that returns true when the entries in...
Q: Implement an equals method for the ADT list that returns true when the entries in one list equal the entries in a second list. In particular, add this method to the class AList. The following is the method header: public boolean equals (Object other) public class AList<T>{ private T list[]; private int capacity = 100; private int numOfEnteries =0; public AList(){ list = (T[])new Object[capacity + 1]; } public void add(T element){ numOfEnteries++; if (numOfEnteries >capacity) System.out.println ("Exceed limit");...