Java 如何从向量中获取对象的变量?
我的代码 任何帮助都将不胜感激。 这是一个加密货币的测试程序,只是一个个人项目Java 如何从向量中获取对象的变量?,java,vector,Java,Vector,我的代码 任何帮助都将不胜感激。 这是一个加密货币的测试程序,只是一个个人项目 仅供参考,这只是填充文本,因为显然您必须这样做。我认为您需要方法getint index。它返回向量中指定位置的元素。您的问题不是很清楚,但让我猜猜您需要什么 您可以使用泛型: package crypto; import java.util.Vector; public class Wallet { public void main() {} public String username;
仅供参考,这只是填充文本,因为显然您必须这样做。我认为您需要方法getint index。它返回向量中指定位置的元素。您的问题不是很清楚,但让我猜猜您需要什么 您可以使用泛型:
package crypto;
import java.util.Vector;
public class Wallet {
public void main() {}
public String username;
public String password;
public int coins;
Vector<Object> wallets = new Vector<Object>();
public Wallet(String username, String password) {
this.username = username;
this.password = password;
this.coins = 0;
}
public void createWallet(String username, String password) {
wallets.add(new Wallet(username, password));
}
public Object findWallet(String username, String password) {
Object wallet;
for (int i = 0; i < wallets.size(); i++) {
wallet = wallets.get(i);
if (wallet.username == username && wallet.password == password) {
return wallet;
}
}
return 0;
}
public void addCoins(String username, String password, int coins) {
Object wallet = findWallet(username, password);
wallet.coins += coins;
}
}
你可以用谷歌搜索什么是getter和setter
我还想提到的是,您可能希望为您的钱包本身使用单独的类,以及倾向于在其上执行操作的服务
我想你可以通过地图找到更多有用的信息。2不要使用Object。3不要使用==来比较字符串。双重强调不要使用Object。在findWallet方法中,将返回类型设为Wallet,如果找不到该钱包,则返回null,并在尝试访问返回的钱包的成员之前执行null检查。此外,还不确定Wallet实例为何具有钱包列表。所有的一切都很混乱。@BoristheSpider它是一路走来的钱包。我不只是做一个数组列表,因为我想在程序中创建新的钱包,我不知道数组是否可以that@CMDRMegashot7727是的,他们可以。你能让事情顺利进行吗?@CMDRMegashot7727那么接受答案怎么样?这就是我要用的
Vector<Wallet> wallets = new Vector<Wallet>();
List<Wallet> wallets = new ArrayList<>();
public Wallet findWallet(String userName, String password) {
return wallets.stream()
.filter(e -> e.getUserName().equals(userName))
.filter(e -> e.getPassword().equals(password))
.findAny()
.orElse(null);
}