Question

Java Write a method that uses an ArrayList and its contains method to take an input...

Java

Write a method that uses an ArrayList and its contains method to take an input array of strings return a new array, free of any duplicate strings.

Homework Answers

Answer #1
import java.util.ArrayList;
import java.util.HashSet;
import java.util.List;
public class ArrayStrings {
   public static void main(String[] args) {
      List<String> alist1 = new ArrayList<String>();
      alist1.add("sneha");
      alist1.add("shweta");
      alist1.add("Marry");
      alist1.add("sneha");
      alist1.add("Marry");
      System.out.println("The ArrayList items are:" );
      for (String i : alist1)
      System.out.println(i);
      HashSet<String> h = new HashSet<String>(alist1);
      List<String> alist2 = new ArrayList<String>(h);
      System.out.println("\nThe new arraylist items after removing duplicates are:" );
      for (String i : alist2)
      System.out.println(i);
   }
}
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
Java Lab Assignment Write a method named findMax. The method will take an array of doubles...
Java Lab Assignment Write a method named findMax. The method will take an array of doubles as an argument, and it will return the largest value in the array. (Note, you will have to create a new array of doubles) . method must be well documented using JavaDoc, example: /** * The find method checks if the target in the array * @param String [] a - array of Strings * @param String t - value to be searched for...
Java Lab Assignment Write a method called convertToArray. The method will take a String as an...
Java Lab Assignment Write a method called convertToArray. The method will take a String as an argument and it will return an array of chars. Each index of the String will be stored in each index of the array. method must be well documented using JavaDoc, example: /** * The find method checks if the target in the array * @param String [] a - array of Strings * @param String t - value to be searched for * @return...
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....
Java Lab Assignment Write a method called convertToString. The method will take an array of chars...
Java Lab Assignment Write a method called convertToString. The method will take an array of chars as an argument and it will return a String. Each index of the array will be part of the each index of the String. method must be well documented using JavaDoc, example: /** * The find method checks if the target in the array * @param String [] a - array of Strings * @param String t - value to be searched for *...
1. a. Write the Java code to fill a Java array of ints of size 100...
1. a. Write the Java code to fill a Java array of ints of size 100 with the value -1. b. Write the Java code to create an ArrayList of Strings, and add the Strings "Just", "Do", "It!" to it.
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 JAVA PROGRAMMING Write a method that finds the shortest word in an array of...
THIS IS JAVA PROGRAMMING Write a method that finds the shortest word in an array of String values and returns the length, in characters, of that word. The method should use the following header. public static int minLength(String[] array) Write a test program that prompts the user to enter ten strings, store them in an array and invokes this method to return the shortest length, and displays that value. (A near complete program can be found attached to the dropbox)
Write a program that uses an array of integers initialized to whatever values you wish. Write...
Write a program that uses an array of integers initialized to whatever values you wish. Write a methods to calculate and return the minimum and a method to calculate and return the maximum value in the array. Write an additional method that accepts the array as a parameter and then creates and returns a new array with all the same values as the original plus 10. (num +=10) In Java
This is JAVA Write the method findMin that will return the smallest number in an array...
This is JAVA Write the method findMin that will return the smallest number in an array of integers. Examples: findMin({29,2,32,12}) -> 2 public int findMin(int[] numbers) {    }
[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...