数组参数传递
我正在参加初学者的java课程。这个实验室是让我创建一个类“Wallet”来操作代表钱包的数组。钱包包含“contents[]”数组来存储代表纸币的整数。变量“count”保存钱包中纸币的数量。在编写方法(与蛇形驱动程序类中提供的方法调用相匹配)来初始化电子钱包并添加货币/更新“计数”后,我需要将一个实例化电子钱包的数组传输到另一个电子钱包。我不知道这将如何工作,因为一个 Wallet 类只弄乱了一个名为“myWallet”的钱包,现在我需要一个名为“yourWallet”的新钱包并用“myWallet”的数组值填充它。 //我应该注意,本课程不允许使用 Java API 库
我的 Wallet 类到目前为止如下所示:
public class Wallet
{
// max possible # of banknotes in a wallet
private static final int MAX = 10;
private int contents[];
private int count; // count # of banknotes stored in contents[]
public Wallet()
{
contents = new int[MAX];
count = 0;
}
/** Adds a banknote to the end of a wallet. */
public void addBanknote(int banknoteType)
{
contents[count] = banknoteType;
count = count + 1;
}
/**
* Transfers the contents of one wallet to the end of another. Empties the donor wallet.
*/
public void transfer(Wallet donor)
{
//my code belongs here
}
...
驱动程序代码如下所示:
public class Driver
{
public static void main(String args[])
{
Wallet myWallet = new Wallet();
myWallet.addBanknote(5);
myWallet.addBanknote(50);
myWallet.addBanknote(10);
myWallet.addBanknote(5);
System.out.println("myWallet contains: " + myWallet.toString());
// transfer all the banknotes from myWallet to yourWallet
Wallet yourWallet = new Wallet();
yourWallet.addBanknote(1);
yourWallet.transfer(myWallet);
System.out.println("\nnow myWallet contains: "
+ myWallet.toString());
System.out.println("yourWallet contains: "
+ yourWallet.toString());
我想使用 addBanknote() 来帮助解决此问题,但是我不知道如何告诉transfer()方法将所有myWallet转移到yourWallet。
我有一个想法在transfer()中做这样的事情:
yourWallet.addBanknote(myWallet.contents[i]);
通过遍历来增加myWallet内容的i。这看起来非常错误,但我完全不知道如何编写这个方法。 如果我的问题不清楚以至于没有人可以提供帮助,我将非常乐意收到有关如何提出更好的问题或如何使用正确的术语进行搜索的建议。 感谢您提供的任何帮助。
I'm in a beginner's java class. This Lab is for me to make a class "Wallet" that manipulates an array that represents a Wallet. Wallet contains the "contents[]" array to store integers represing paper currency. The variable "count" holds the number of banknotes in a wallet. After writing methods (that match provided method calls in a serpate Driver class) to initialize the Wallet and add currency/update "count", I need to transfer the array of one instantiated Wallet to another. I don't know how that would work because the one Wallet class has only been messing with a wallet called "myWallet" and now I need to take a new Wallet called "yourWallet" and fill it with "myWallet"'s array values.
//I should note that using the Java API library is not allowed in for this course
My Wallet class looks like this so far:
public class Wallet
{
// max possible # of banknotes in a wallet
private static final int MAX = 10;
private int contents[];
private int count; // count # of banknotes stored in contents[]
public Wallet()
{
contents = new int[MAX];
count = 0;
}
/** Adds a banknote to the end of a wallet. */
public void addBanknote(int banknoteType)
{
contents[count] = banknoteType;
count = count + 1;
}
/**
* Transfers the contents of one wallet to the end of another. Empties the donor wallet.
*/
public void transfer(Wallet donor)
{
//my code belongs here
}
...
The Driver code looks like this:
public class Driver
{
public static void main(String args[])
{
Wallet myWallet = new Wallet();
myWallet.addBanknote(5);
myWallet.addBanknote(50);
myWallet.addBanknote(10);
myWallet.addBanknote(5);
System.out.println("myWallet contains: " + myWallet.toString());
// transfer all the banknotes from myWallet to yourWallet
Wallet yourWallet = new Wallet();
yourWallet.addBanknote(1);
yourWallet.transfer(myWallet);
System.out.println("\nnow myWallet contains: "
+ myWallet.toString());
System.out.println("yourWallet contains: "
+ yourWallet.toString());
I want to use addBanknote() to help with this, but I don't know how to tell the transfer() method to transfer all of myWallet into yourWallet.
I had the idea to do somethign like this in transfer():
yourWallet.addBanknote(myWallet.contents[i]);
with a traversal to increase i for myWallet contents. It seems horribly wrong, but I'm at a complete loss as to write this method.
If my problem is so unclear that nobody can help, I would be more than happy to receive advice on how to ask a better question or on how to search with correct terms.
Thanks for any help you can provide.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(4)
我不想破坏你的作业,因为你似乎走在正确的道路上,但我确实有一些评论,你可以接受也可以不接受:)
首先,我可能会将钞票类型放在一些枚举中。但这听起来有点高级,考虑一下
将捐赠者的所有钞票转移给自己应该不是什么大问题
您会陷入困境
( for 循环就可以)但我认为如果您尝试实现 a因为您不仅使用 count 作为长度,还使用 count 作为索引变量,那么 。我的意思是你假设内容[0] ...内容[count-1]持有有效的钞票。如何在不费太多功夫的情况下删除一个呢?
警告:更高级一点
在你的情况下,我可能会选择将banknoteType设置为0,表示你的钱包中有一个空的钞票槽,并将_addBanknote(intbanknoteType)实现为:
这可能有点难以承受在此刻。但它允许您实现:
请注意,在这两种方法中,当我成功删除或添加钞票时,我都会返回。只有当我找不到空闲插槽或请求的钞票时,我才会完成 for 循环并最终抛出异常,从而停止程序。
I don't want to spoil your homework as you seem to be going the right way, but I do have some comments which you may either take or not :)
First, I would probably put the bank note types in some enumeration. But as that sounds a bit to advanced, consider
Transferring all the banknotes from the donor to yourself should not be so much of a problem
(a for loop would do) but I think you're in a world of hurt if you are trying to implement a
as you are using count not only as a length but also as an index variable. By this I mean that you assume contents[0] ... contents[count-1] hold valid banknotes. And how do you remove one without too much work?
Warning: a bit more advanced
In your case I would probably opt to have a banknoteType of 0 indicating an empty banknote slot in your wallet, and implement _addBanknote(int banknoteType) as:
This may be a bit overwhelming at this point. But it would allow you to implement:
Please note that in both methods I return when I successfully removed or added the banknote. Only when I could not find a free slot, or the requested banknote, I finish the for loop and end up throwing an exception and thereby stopping the program.
我认为这个问题很好,我认为你走在正确的道路上。您调用
Wallet#addBanknote(int)
的方式是正确的。您所说的是正确的:还有一件事,如果您的内容多于
MAX
,您的Wallet#addBanknote(int)
方法会发生什么?I think the question is fine and I think you're on the right path. The way you're calling
Wallet#addBanknote(int)
is correct. What you have said is the right thing:Just another thing, what would happen in your
Wallet#addBanknote(int)
method if you have more contents than theMAX
?您可以创建一个接受另一个钱包的构造函数或一个函数(如前所述)并使用 System.arraycopy 一次性复制数组。 System.arraycopy 速度很快,对于像这样的小东西来说它绝对是大材小用,但它是你工具包中的好工具。
提到的另一种选择是,在循环中将元素从一个数组逐个元素复制到另一个数组也可以正常工作。
You can create either a constructor that takes another wallet, or a function (as already mentioned) and use System.arraycopy to copy the array in one fell swoop. System.arraycopy is fast, and its definitely overkill for something small like this, but its good tool to have in your toolkit.
The other alternative mentioned, copy the elements from one array to the other element by element in a loop will work fine too.
传输方法中的 myWallet 被命名为“donor”,因此,它看起来并没有什么可怕的错误:
您只需要围绕它进行循环,并删除 yourWallet。这是该类的实例的名称。该实例位于类/方法
this
内部,但不需要指定,因为范围内没有其他addBanknote
-方法,这可能是指。 (感谢芒果醉)。The myWallet inside the transfer method is named 'donor', and with that, it doesn't look horribly wrong:
You just need a loop around it, and to remove the yourWallet. which is the name of an instance of that class. That instance is inside the Class/method
this
, but needn't be specified, because there is no otheraddBanknote
-Method in scope, which could be meant. (Thanks to mangoDrunk).