简体   繁体   English

从一个ArrayList转移到另一个

[英]Transferring from one ArrayList to another

I'm trying to transfer the contents of ArrayList purse to ArrayList purse2 using the add method. 我正在尝试使用add方法add ArrayList purse的内容传输到ArrayList purse2

However, when I try to just do purse2.add(purse) I get a paragraph of errors. 但是,当我尝试只做purse2.add(purse) ,会出现一段错误。

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. 但是,我发现如果我执行purse2.add(input)它应该可以工作,但是当我在transfer方法中这样做时,它只是将"done"添加到数组中,而不是输入的任何其他单词。 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. 如果我将这条线与purse的初始输入放在一起,那么它会完美地工作,除非我无法在那条线上放置我的输出以使其有意义。

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) . 要将列表l1的内容添加到另一个列表l2 ,应使用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. 使用l2.add(l1)是错误的,因为add()仅接受List相同类型的一个元素,在这种情况下为String。 But you are trying to pass a List of String s to the add() method, so the compiler throws an error. 但是,您试图将StringList传递给add()方法,因此编译器将引发错误。

I don't see where you're attempting the transfer but I assume it will go in your transfer() method. 我看不到您在哪里尝试传输,但我认为它将在您的transfer()方法中进行。 What you need to do is loop through purse , adding each element to purse2 , then clear purse . 您需要做的是遍历purse ,将每个元素添加到purse2 ,然后清除purse (You can't remove the elements from purse as you go because you'll run into a ConcurrentModificationException from the iterator.) (您不能随便从purse删除元素,因为会从迭代器中遇到ConcurrentModificationException 。)

您有两种方法可以做到: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: 不确定是否purse2 ,但是当您阅读输入内容时,添加到Jodie purse每个硬币也会立即添加到Johnny的purse2中:

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: 无论如何,当我删除上述行时,我要做的就是相应地修改transfer方法的开头:

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();

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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