简体   繁体   中英

Transferring from one ArrayList to another

I'm trying to transfer the contents of ArrayList purse to ArrayList purse2 using the add method.

However, when I try to just do purse2.add(purse) I get a paragraph of errors.

However, I have figured out that if I do purse2.add(input) it should work, but when I do that in my transfer method it just adds "done" into the array instead any other word that is inputted. If I put this line up with the initial input of purse then it works perfectly, except I cannot have the line up there for my output to make any sense.

Any help would be appreciated.

package purse;
import java.util.Scanner;
import java.util.Collections;
import java.util.ArrayList;

public class Purse {
    ArrayList<String> purse = new ArrayList<>(); 
    ArrayList<String> purse2 = new ArrayList<>();
    Scanner coin = new Scanner (System.in); 
    Scanner coin2 = new Scanner (System.in);
    String input = " ";
    String input2 = " ";
    String end = "done";

    public void addCoin(){
        System.out.println("Please put as many coins of U.S currency as you like into Jodie's purse, type 'done' when finished: ");

        while (!input.equalsIgnoreCase ("done"))
        {
            input = ( coin.nextLine());
            if (input.equalsIgnoreCase("penny") || input.equalsIgnoreCase("nickel") || input.equalsIgnoreCase("dime") || input.equalsIgnoreCase("quarter") || input.equalsIgnoreCase("done")) {
                purse.add(input);
                purse2.add(input);
                purse.remove("done");
            } else {
                System.out.println("Please input a coin of U.S currency.");
            }
        }
        System.out.println("Contents of purse: " + purse);
        Collections.reverse(purse);
        System.out.println("This is the contents of the purse reversed: " + purse );

        //Start of Johnny's Purse
        System.out.println("Please put as many coins of U.S currency as you like into Johnny's purse, type 'done' when finished: ");
        while (!input2.equalsIgnoreCase ("done")) {
            input2 = (coin.nextLine());
            if (input2.equalsIgnoreCase("penny") || input2.equalsIgnoreCase("nickel") || input2.equalsIgnoreCase("dime") || input2.equalsIgnoreCase("quarter") || input2.equalsIgnoreCase("done")) {
                purse2.add(input2); 
                purse2.remove("done");
            } else {
                System.out.println("Please input a coin of U.S currency.");
            }
        }
        System.out.println("Contents of purse: " + purse2);
    }

    public ArrayList<String> printPurseContents() {
        return purse;  
    }

    public void transfer() {
        purse.clear();

        System.out.println("Jodie is feeling bad for Johnny so Jodie is going to give him all of her money!");
        System.out.println("Johnny's purse  now has: " + purse2 );
        System.out.println("and now Jodie's pure is empty: " +purse);
    }
}

To add the content of a List l1 into another List l2 you should use l2.addAll(l1) . Using l2.add(l1) is wrong because add() accepts only one element of the same type of the List , that is a String in this case. But you are trying to pass a List of String s to the add() method, so the compiler throws an error.

I don't see where you're attempting the transfer but I assume it will go in your transfer() method. What you need to do is loop through purse , adding each element to purse2 , then clear purse . (You can't remove the elements from purse as you go because you'll run into a ConcurrentModificationException from the iterator.)

您有两种方法可以做到:1)使用循环(for / foreach)2)使用addAll()方法(最佳方法)

Not sure if this is intended, but when you read the input, each coin added to Jodie's purse is immediately added to Johnny's purse2 as well:

purse.add(input);
purse2.add(input); // I think this line should be removed

Anyway, when I removed the above mentioned line, all I had to do was to modify the beginning of the transfer method accordingly:

public void transfer() {
    purse2.addAll(purse);
    purse.clear();
    // the rest of the method follows unchanged

I tested these changes with the following code - to me the result seemed correct:

Purse p = new Purse();
p.addCoin();
p.transfer();

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM