gpt4 book ai didi

java - 钱包程序中的传输方法出现问题(ArrayList)

转载 作者:行者123 更新时间:2023-12-01 23:11:48 25 4
gpt4 key购买 nike

我在完善从 2 个数组列表传输内容的传输方法时遇到了麻烦。我需要使用 For-Each 循环来执行此操作,但目前该方法仅传输数组列表中的第一项,仅此而已。

package assignment;

import java.util.ArrayList;

/**
* A purse holds a collection of coins
*
* @author type your name
*/
public class Purse {

ArrayList<String> coins;
ArrayList<String> rcoins;

/**
* Constructs an empty purse.
*/
public Purse() {
coins = new ArrayList<String>();
rcoins = new ArrayList<String>();
}

/**
* Add a coin to the purse
*
* @param coinName the coin to add
*/
public void addCoin(String coinName) {
coins.add(coinName);
}

/**
* Return a string describing the object
*
* @return a string in the format "Purse[coinName1, coinName2, ...]"
*/
public String toString() {
if (coins.size() == 0)
return "Purse[]";

String output = "Purse[";

for (String coin : coins)
output = output + coin + ", ";

// remove the last ", "
output = output.substring(0, output.length() - 2);
return output + "]";
}

public String reverse() {
if (coins.size() == 0)
return "Purse[]";

String output = "Reverse Purse[";

for (String coin : coins)
rcoins.add(0, coin);

for (String coin : rcoins)
output += coin + ",";

output = output.substring(0, output.length() - 1);
return output + "]";

}

public void transfer(Purse a, Purse b) {
for (String coin : a.coins) {
b.coins.add(coin);
coins.remove(coin);
}
}

public String sameContents(Purse a, Purse b) {
String eq = "";
int size;
if (a.coins.size() > b.coins.size())
size = b.coins.size();
else
size = a.coins.size();
for (int i = 0; i < size; i++) {
if (a.coins.get(i).equals(b.coins.get(i)))
eq = "They are equal";
else
eq = "They are not equal";
}
return eq;
}
}

我的测试仪

package assignment;

public class PurseTester {

public static void main(String[] args) {
//Create new Purses
Purse p = new Purse();
Purse q = new Purse();

//Add coins
p.addCoin("Nickel");
p.addCoin("Quarter");
p.addCoin("Dime");
q.addCoin("Penny");
q.addCoin("Quarter");

//Print contents of Purse P and the reversed contents
System.out.println(p.toString());
System.out.println("\n" + p.reverse());

//Print contents of Purse Q
System.out.println("\nOther " + q.toString() + "\n");

//Call the transfer method to transfer the contents of Purse Q into Purse P
q.transfer(q, p);

//Print contents after transfer
System.out.println(p.toString());
System.out.println("Other " + q.toString());

System.out.println("\n");
//Compare purses P and Q to see if they share contents, print reslt
System.out.println(p.sameContents(p, q));
}
}

最佳答案

在面向对象编程中,您的transfer方法应如下所示:

// transfer coins from an other purse to this
public void transfer(Purse from) {
for(String coin : from.coins){
this.coins.add(coin);
from.coins.remove(coin);
}
}

在您原来的示例中,当您从 a 转账时,coins.remove(coin); 将从 this.coins 中删除硬币到b

Purse a 转移到 Purse b (如您编写的方法)的方法可能应该在 Purse 类之外静态定义,因为没有引用当前的Purse this

关于java - 钱包程序中的传输方法出现问题(ArrayList),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/21815153/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com