从一个ArrayList转移到另一个

时间:2015-08-30 14:02:58

标签: java arraylist add transfer

我尝试使用ArrayList purse方法将ArrayList purse2的内容转移到add

但是,当我尝试purse2.add(purse)时,我会收到一段错误。

但是,我已经发现如果我purse2.add(input)它应该有效,但是当我在我的transfer方法中执行此操作时,它只会将"done"添加到数组中,而不是任何其他单词输入。如果我把这一行与purse的初始输入放在一起,那么它就能完美地工作,除了我不能在那里输出我的输出有任何意义。

任何帮助将不胜感激。

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

4 个答案:

答案 0 :(得分:2)

要将列表l2的内容添加到另一个列表l2.addAll(l1),您应该使用l2.add(l1)。使用add()是错误的,因为List只接受同一类型List的一个元素,在这种情况下是一个字符串。但是您试图将String add()传递给 $('.parent').on('click', function(e){ if(e.target.classList.contains('child')) // do something } }) 方法,因此编译器会抛出错误。

答案 1 :(得分:0)

我没有看到您尝试转移的位置,但我认为它将采用您的transfer()方法。您需要做的是循环purse,将每个元素添加到purse2,然后清除purse。 (你不能从purse中删除元素,因为你将从迭代器遇到ConcurrentModificationException。)

答案 2 :(得分:0)

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

答案 3 :(得分:0)

不确定是否有意,但当您阅读输入时,添加到Jodie的purse的每个硬币也会立即添加到Johnny的purse2中:

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

无论如何,当我删除上面提到的行时,我所要做的就是相应地修改transfer方法的开头:

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

我使用以下代码测试了这些更改 - 对我来说结果似乎是正确的:

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