Question

java Write a generic method that takes an ArrayList of objects (of a valid concrete object...

java

Write a generic method that takes an ArrayList of objects (of a valid concrete object type) and returns a new ArrayList containing no duplicate elements from the original ArrayList. The method signature is as follows: public static ArrayList UniqueObjects(ArrayList list) Test the method with at least 2 array lists of different concrete object types. Label your outputs properly and show the array lists content before and after calling method UniqueObjects

0 0
Add a comment Improve this question Transcribed image text
Answer #1

RemoveDuplicates.java

import java.util.ArrayList;

public class RemoveDuplicates {

public static void main(String[] args) {

//Creating an ArrayList

ArrayList arl1=new ArrayList();

//Adding Elements to ArrayList

arl1.add(34);

arl1.add(34);

arl1.add(32);

arl1.add(45);

arl1.add(65);

arl1.add(56);

arl1.add(54);

arl1.add(98);

arl1.add(54);

arl1.add(98);

System.out.println("ArrayList#1 Before Removing Duplicates :");

System.out.println(arl1);

arl1=UniqueObjects(arl1);

System.out.println("ArrayList#1 After Removing Duplicates :");

System.out.println(arl1);

//Creating an ArrayList

ArrayList arl2=new ArrayList();

//Adding Elements to ArrayList

arl2.add(35);

arl2.add(37);

arl2.add(22);

arl2.add(35);

arl2.add(37);

arl2.add(89);

arl2.add(43);

arl2.add(32);

arl2.add(78);

arl2.add(65);

System.out.println("ArrayList#2 Before Removing Duplicates :");

System.out.println(arl2);

arl2=UniqueObjects(arl2);

System.out.println("ArrayList#2 After Removing Duplicates :");

System.out.println(arl2);

}

//This method will elimnates the duplicate Elements in the ArrayList

public static ArrayList UniqueObjects(ArrayList list)

{

ArrayList arl=new ArrayList();

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

{

if(!arl.contains(list.get(i)))

arl.add(list.get(i));

}

return arl;

}

}

________________

Output:

ArrayList#1 Before Removing Duplicates :
[34, 34, 32, 45, 65, 56, 54, 98, 54, 98]
ArrayList#1 After Removing Duplicates :
[34, 32, 45, 65, 56, 54, 98]
ArrayList#2 Before Removing Duplicates :
[35, 37, 22, 35, 37, 89, 43, 32, 78, 65]
ArrayList#2 After Removing Duplicates :
[35, 37, 22, 89, 43, 32, 78, 65]

______________Thank You

Add a comment
Know the answer?
Add Answer to:
java Write a generic method that takes an ArrayList of objects (of a valid concrete object...
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. Our experts will answer your question WITHIN MINUTES for Free.
Similar Homework Help Questions
  • A java exercise please! Write a static method named swapHavles that takes an ArrayList of integers...

    A java exercise please! Write a static method named swapHavles that takes an ArrayList of integers as a a parameter and returns a new ArrayList that has every element in the second half of the original ArrayList swapped with every element in the first half of the original ArrayList. Note: • You can assume that the ArrayList passed to this method as a parameter always contains an even number of elements and will always contain at least two elements. For...

  • JAVA Question a) Write a generic method public static <...> Collection<Pair<...>> sortPairCollection(Collection <Pair<....>> col) in a...

    JAVA Question a) Write a generic method public static <...> Collection<Pair<...>> sortPairCollection(Collection <Pair<....>> col) in a Utils class that takes as parameter a collection of Pair<K,V> objects and returns a new collection object (use ArrayList<...>) with the pair elements from collection col sorted in ascending order. For comparing pairs, the K type must implement Comparable<....> Use the proper type constraints. b) Write a main() function in Utils.java that tests the sortPairCollection() with K=String and V=Integer.

  • *Java* Hi. I need some help with creating generic methods in Java. Write a program GenMethods...

    *Java* Hi. I need some help with creating generic methods in Java. Write a program GenMethods that has the following generic methods: (1) Write the following method that returns a new ArrayList. The new list contains the nonduplicate (i.e., distinct) elements from the original list. public static ArrayList removeDuplicates(ArrayList list) (2) Write the following method that shuffles an ArrayList. It should do this specifically by swapping two indexes determined by the use of the random class (use Random rand =...

  • JAVA Write a static method that takes an array of a generic type as its only...

    JAVA Write a static method that takes an array of a generic type as its only argument. The method should display the array and return the number of elements in the array. Test the method in main with at least three arrays of objects. SAMPLE OUTPUT Here is an Integer array 12 21 7 16 8 13 That array held 6 elements Here is a String array one two three four That array held 4 elements Here is a Double...

  • Write a method public static ArrayList merge(ArrayList a, ArrayList b) that merges two array lists, alternating...

    Write a method public static ArrayList merge(ArrayList a, ArrayList b) that merges two array lists, alternating elements from both array lists. If one array list is shorter than the other, then alternate as long as you can and then append the remaining elements from the longer array list. For example, if a is 1 4 9 16 and b is 9 7 4 9 11 then merge returns the array list 1 9 4 7 9 4 16 9 11...

  • *Java* Create a public static method named maxRes that takes an ArrayList of type Double and...

    *Java* Create a public static method named maxRes that takes an ArrayList of type Double and returns a double. This method returns the maximum result (do not return the original value) of taking the tangent of each value from the input

  • //Java (Sort ArrayList) Write the following method that sorts an ArrayList: public static > void sort(ArrayList...

    //Java (Sort ArrayList) Write the following method that sorts an ArrayList: public static > void sort(ArrayList list) Write a test program that does the following operations: 4. Creates an empty ArrayList of Integer elements; 5. Generates 20 integer values, drawn at random between 0 and 9 (included), and adds them to the array list; 6. Calls the method sort and prints both the original list, and the sorted one.

  • Write a method that takes an ArrayList of Integer objects and returns an ArrayList of Character...

    Write a method that takes an ArrayList of Integer objects and returns an ArrayList of Character objects of the same size. The returned elements of the ArrayList are assigned letter grade corresponding to integer grade of the same index element of the ArrayList parameter (A if 90 or above, F if less than 60). Include code to test your method. [For other letter grades: 80 ?? 89 −> ?, 70 ?? 79 −> ?, 60 ?? 69 −> ?]

  • 2a) Write a method countEvens that takes an ArrayList of String objects as input and returns...

    2a) Write a method countEvens that takes an ArrayList of String objects as input and returns the number of even length strings contained in the input. For example, if the input is [ one, peach, pear, plum ] then countEvents(inp) should return 2. 2b) Write a method, mirror, that doubles the size of a list of integers by appending a mirror image of the list. For example, given an array list containing [ 1, 5, 2, 6 ], the method...

  • Implement the following methods (based on ArrayLists): (JAVA) a) merge method that takes two ArrayLists as...

    Implement the following methods (based on ArrayLists): (JAVA) a) merge method that takes two ArrayLists as input and returns the merged ArrayList containing the elements from both the input lists. b) enumerate method that takes an ArrayList as input and prints the enumerated contents of the input list. c) reverse method that takes an ArrayList as input and returns an ArrayList with the contents of input list in reverse order. Test your method implementations.

ADVERTISEMENT
Free Homework Help App
Download From Google Play
Scan Your Homework
to Get Instant Free Answers
Need Online Homework Help?
Ask a Question
Get Answers For Free
Most questions answered within 3 hours.
ADVERTISEMENT
ADVERTISEMENT
Active Questions
ADVERTISEMENT