java - 如何获得正确的输入来获取我的 ArrayList 的索引?
问题描述
我正在尝试通过 indexOf 获取我的 ArrayList 的索引。到目前为止,我有
我的数组列表:public static ArrayList<Shop> allShops = new ArrayList();
那应该得到索引
Scanner editShop = new Scanner(System.in);
String shopToEdit = editShop.nextLine();
int i = allShops.indexOf(shopToEdit);
System.out.println(i); //see what our index is (returns -1 because the var doesn't seem to get the right input)
EditFunc.edit(i);
而这个,应该会改变我的数组列表
public static void edit(int index){
//change array with given input in edit
//TODO: Make it so they can choose what to edit
//with booleans if editTrueName = false and then later on make it true again
System.out.println("Enter the new shop name:");
Scanner editedShopAttribute = new Scanner(System.in);
String editedShopName = editedShopAttribute.nextLine();
System.out.println("Enter the new shop location:");
String editedShopLocation = editedShopAttribute.nextLine();
Shop EditedVar = new Shop();
EditedVar.createShop(editedShopName,editedShopLocation);
allShops.set(index, EditedVar);
}
我已经复制了调试器显示给我的值并用它替换了它们,但它似乎仍然不起作用。我是否接受了错误类型的数据?我可以尝试什么?
如果我的代码看起来有问题,我总是会努力让它变得更好。
解决方案
你不能用 a 来做Map<String, Shop>
吗?这样你就可以使用shopName
作为键。
顺便说一句,当我看到您使用 java 和 OOP 的新知识时,我强烈建议您阅读 Robert C. Martin 的 Clean Code,这是一本改变游戏规则的书。
推荐阅读
- authentication - Yubico / Attestation 和 Assertion 之间的凭证 ID 长度不匹配
- mysql - 获取链接表中最频繁的值,并在 MySQL 的主表中引用它
- email - 如何设置电子邮件跟踪代码来跟踪不同客户电子邮件提供商的电子邮件打开/点击?
- r - R:如何将日期附加到 timeseries(ts) 对象中?
- javascript - angularjs 指令 ngrepeat 不适用于角度向导中的对象
- visual-studio - Visual Studio Diff 显示不需要的大小写差异
- vitess - Vitess 分片问题
- python - 基于“时间瞬间”的不同字典中的值总和
- python - Windows 10 linux子系统ubuntu试图安装python pip
- css - CSS 如果第一个元素是下拉菜单,那么做一些事情