我无法完善传输方法以从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));
}
}
答案 0 :(得分:2)
在面向对象编程中,您的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);
}
}
在原始示例中,当您从coins.remove(coin);
转移到this.coins
时,a
会从b
移除硬币。
从Purse a
转换为Purse b
的方法与您编写的方法一样,可能已在Purse
类之外定义为静态,因为没有引用当前Purse
this
。
答案 1 :(得分:2)
public void transfer(Purse a, Purse b)
{
for(String coin : a.coins){
b.coins.add(coin);
coins.remove(coin);
}
你的错误就在这条线上:
coins.remove(coin);
这是从属于this
对象的钱包中移除硬币,这与您在测试代码中转移它们的钱币相同。你添加一个硬币,然后立即从同一个钱包中删除它。你应该从钱包a
中删除它们。
由于您在transfer
对象上调用Purse
,因此最好只有一个参数。你调用方法的Purse
应该是你要么从中转移或转移到的钱包,而论证应该是另一个钱包。