Question

JAVA - Given two List objects (input and output), write a generic method that 1) clears...

JAVA - Given two List objects (input and output), write a generic method that 1) clears output list if it contains any element, and 2) copies every element of input list to the output list in the same order and position. Note that the type of output list can be any super type of the input list (e.g. type(output)=Numeric & type(input)=Double).

import java.util.List;
import java.util.ArrayList;

public class Collections {

   // Modify method signature if needed & implement the body of method
   public static void copy(List output, List input) {
   }

}

RUN THIS TO CHECK CORRECTNESS

import java.util.ArrayList;
import java.util.List;

public class QA4Test {

   // Test code, do not modify
   public static void main(String[] args) {

      List<Number> output = new ArrayList<Number>();
      List<Integer> input = new ArrayList<Integer>();
      input.add(5);
      input.add(3);
      Collections.copy(output, input);
      System.out.println(output.get(0));  // this should be 5

      List<Integer> output3 = new ArrayList<Integer>();
      List<Integer> input3 = new ArrayList<Integer>();
      input3.add(3);
      input3.add(1);
      input3.add(2);
      Collections.copy(output3, input3);
      System.out.println(output3.get(0));  // this should be 3

      List<String> output2 = new ArrayList<String>();
      List<Long> input2 = new ArrayList<Long>();

      // Collections.copy(output2, input2); // will raise error
   }
   
}
0 0
Add a comment Improve this question Transcribed image text
Answer #1
import java.util.List;
import java.util.ArrayList;

public class Collections {

    // Modify method signature if needed & implement the body of method
    public static void copy(List output, List input) {
        output.clear();
        for (int i = 0; i < input.size(); i++) {
            output.add(input.get(i));
        }
    }

}
Add a comment
Know the answer?
Add Answer to:
JAVA - Given two List objects (input and output), write a generic method that 1) clears...
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
  • CHALLENGE ACTIVITY 7.16.1: Enter the output of the ArrayList ADT functions. Jump to level 1 Type...

    CHALLENGE ACTIVITY 7.16.1: Enter the output of the ArrayList ADT functions. Jump to level 1 Type the program's output import java.util.ArrayList; import java.util.Scanner; public class IntegerManager ( public static void printSize(ArrayList<Integer> numsList) System.out.println(numsList.size() + "items"); public static void main(String[] args) Scanner scnr = new Scanner(System.in); int currval; ArrayList<Integer> intList = new ArrayList<Integer>(); Input 123-1 printSize (intList); Output currval = scnr.nextInt(); while (currval >= 0) { intList.add(currval); currval = scnr.nextInt(); printSize (intList); intList.clear(); printSize (intList); 1 2 Check Next

  • How do i rewrite this code only using the list below? import java.util.ArrayList; import java.util.Collection; import...

    How do i rewrite this code only using the list below? import java.util.ArrayList; import java.util.Collection; import java.util.List; import java.util.Set; import java.util.HashSet; public class RotationCheck {    /**    * Rotates the elements in the specified list by the specified distance. After calling this    * method, the element at index {@code i} will be the element previously at index    * {@code (i - distance) % list.size()}, for all values of {@code i} between {@code 0} and    * {@code...

  • Add another method public static void displayObject(ArrayList list, int n) that will display then information on...

    Add another method public static void displayObject(ArrayList list, int n) that will display then information on the nth object of list. If n is not a valid index, the method should generate and throw and exception that the main can then process. You get to decide what exception (one built into Java or a custom exception) and how you would like to “handle” the exception (terminate the program, prompt for more input, etc). Here is the program so far: import...

  • Problem 1 Given a linked list of integers, write a function getUnique that removes all duplicates elements in the linked list and returns the new linked list of unique elements (The order does not ma...

    Problem 1 Given a linked list of integers, write a function getUnique that removes all duplicates elements in the linked list and returns the new linked list of unique elements (The order does not matter). Example: Input: 1-»2->3->1-2 Output: 1->2->3 public class Node f int iterm Node next; Node(int d) t item = d; next-null; ) import java.util.ArrayList; public class ExtraLab public static void main (String[] args)t PROBLEM 1 System.out.println("PROBLEM 1"); Node head new Node(1); head.next-new Node (2); head.next.next-new Node(3);...

  • Can someone help me with my code.. I cant get an output. It says I do...

    Can someone help me with my code.. I cant get an output. It says I do not have a main method. HELP PLEASE. The instruction are in bold on the bottom of the code. package SteppingStones; //Denisse.Carbo import java.util.Scanner; import java.util.ArrayList; import java.util.List; public class SteppingStone5_Recipe { private String recipeName; private int servings; private List<String> recipeIngredients; private double totalRecipeCalories; public String getRecipeName() { return recipeName; } public void setRecipeName (string recipeName){ this.recipeName = recipeName; } public int getServings() { return...

  • The JCF provides numerous classes that implement the List interface, including LinkedList, ArrayList, and Vector. Write...

    The JCF provides numerous classes that implement the List interface, including LinkedList, ArrayList, and Vector. Write code to show how the JCF class ArrayList and LinkedList are used to maintain a grocery list. Verify List methods such as "add()", get(), "remove()", "isEmpty()" and "size()" by implementing a test program. import java.util.ArrayList; import java.util.Iterator; public class GroceryList { static public void main(String[] args){ ArrayList<String> groceryList = new ArrayList<String>(); Iterator<String> iter;    groceryList.add("apples"); groceryList.add("bread"); groceryList.add("juice"); groceryList.add("carrots"); groceryList.add("ice cream");    System.out.println("Number of items...

  • how would i use test.add because i would like to use add import java.io.File; import java.io.FileNotFoundException;...

    how would i use test.add because i would like to use add import java.io.File; import java.io.FileNotFoundException; import java.util.ArrayList; import java.util.Arrays; import java.util.List; import java.util.Scanner; public class checkFruit { private List<String> tests; public List<String> getFruit(String fruits) { tests = new ArrayList<String>(Arrays.asList(fruits.split(","))); for (String test : tests) { System.out.println(test); tests.add()//how would i use add here } return tests; } public static void main(String[] args) { Scanner in = new Scanner(System.in); System.out.print("Enter file name: "); File file = new File(in.nextLine()); try { checkFruit...

  • Need help creating a GUI using JavaFX. Also to Convert all console input/output to be used...

    Need help creating a GUI using JavaFX. Also to Convert all console input/output to be used in a GUI. Here is my Code import java.util.ArrayList; import java.util.Arrays;       import java.util.Collections; public class Part1_2 {                 public static void main(String[] args) {                                 ArrayList<String> names = new ArrayList<String>();                                 names.add("Wilson");                                 names.add("David");                                 names.add("John");                                 names.add("Eli");                                 names.add("Riley");                                 for (String name : names) {                                                                          System.out.println(name);                                 }                                 System.out.println("\n");                                 Collections.sort(names);                                                               for (String name : names) {                                                                         ...

  • Prompt: In this stepping stone lab assignment, you will build a Recipe class, getting user input...

    Prompt: In this stepping stone lab assignment, you will build a Recipe class, getting user input to collect the recipe name and serving size, using the ingredient entry code from Stepping Stone Lab Four to add ingredients to the recipe, and calculating the calories per serving. Additionally, you will build your first custom method to print the recipe to the screen. Specifically, you will need to create the following:  The instance variables for the class (recipeName, serving size, and...

  • Can someone please help me add appropriate descriptive comments to each line of code in the...

    Can someone please help me add appropriate descriptive comments to each line of code in the project explaining why the code is in the application. import java.util.ArrayList; import java.util.List; import java.util.function.Predicate; public class BookManager { private List<Book> bookList;    public BookManager() { bookList = new BookCatalog().getCatalog(); }    public List<Book> getBooks(Predicate<Book> condition) { List<Book> books = new ArrayList<>(); for (Book b: bookList) { if (condition.test(b)) { books.add(b); } } return books; } } import java.util.List; public class BookManagerApp { public...

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