[英]Java - Passing Arrays in Methods, Variables Not Recognized
I have been stuck on this one for days, but I have broken it down here. 我已经被这个问题困扰了好几天,但是在这里我已经将其分解了。 What I need to do is to create an array of accounts with about 9 variables each (AccountID, WithdrawlDates, etc.) that the user can input in a command prompt.
我需要做的是创建一个帐户数组,每个帐户包含大约9个变量(AccountID,WithdrawlDates等),用户可以在命令提示符下输入这些变量。 From the createAccount() method I can send an instance of user and a accountNum, but the user is not recognized on the receiving setAccount method.
从createAccount()方法中,我可以发送一个用户实例和一个accountNum,但是在接收方setAccount方法上无法识别该用户。
Here's the code: 这是代码:
class User{
private int accountID;
User( int id )
{
accountID = id;
}
static void setAccountID(User user[], int accountNum)
{
user.accountID = accountNum; //accountID is not recognized here
}
static void getAccountID(User user){System.out.println(user.accountID);}
}
class TestUser
{
public static void main(String[] args)
{
createAccount();
}
static void createAccount(){
User[] user = new User[2];
user[0] = new User(25);
User.setAccountID(user, 2001);
}
}
I am open to changing the flow of this, but I don't know where to start. 我愿意改变这种流程,但我不知道从哪里开始。
Thanks! 谢谢!
There's no reason to pass an array of User
objects. 没有理由传递
User
对象数组。 Try this instead: 尝试以下方法:
class User{
private int accountID;
User( int id )
{
accountID = id;
}
static void setAccountID(User user, int accountNum)
{
user.accountID = accountNum; //accountID is not recognized here
}
static void getAccountID(User user){System.out.println(user.accountID);}
}
class TestUser
{
public static void main(String[] args)
{
createAccount();
}
static void createAccount(){
User user = new User(25);
User.setAccountID(user, 2001);
}
}
EDIT: If you need to maintain an array of users as @Luiggi Mendoza suggests in his comment, just pass a single array element to setAccountID()
: 编辑:如果您需要维护用户数组,如@Luiggi Mendoza在其评论中建议的,只需将单个数组元素传递给
setAccountID()
:
static void createAccount(){
User[] user = new User[2];
user[0] = new User(25);
User.setAccountID(user[0], 2001); // set id for first User
}
To access the elements of an array instead of doing something with the array itself you use square brackets like so: 要访问数组的元素而不是对数组本身做任何事情,可以使用方括号,如下所示:
user[userIndex]
from there you can either change the element like this 从那里您可以像这样更改元素
user[userIndex] = new User(id);
or access/modify something about the element itself like this 或像这样访问/修改元素本身
user[userIndex].accountID = whatever;
Additionally, your use of static in the setAccountID is confusing things. 此外,您在setAccountID中使用static会使事情变得混乱。 A static method cannot know anything about accountID because accountID is a part of a uniquely created object where the static method belongs to the class, and not any particular object.
静态方法无法了解关于accountID的任何信息,因为accountID是唯一创建的对象的一部分,其中静态方法属于该类,而不是任何特定对象。 If it must be static for some reason, you will need to change the method to look something like this
如果由于某种原因它必须是静态的,则需要将方法更改为如下所示
static void setAccountID(User user[], int userIndex, int accountNum)
{
user[userIndex].accountID = accountNum;
}
but the following would be much better, since you know the user inside the array anyway: 但是以下内容会更好,因为您仍然知道数组中的用户:
void setAccountID(int accountNum)
{
this.accountID = accountNum;
}
called like this: 这样称呼:
user[userIndex].setAccountID(accountNum);
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.