真的不知道你的意思是什么,但你的代码有一些严重的基本缺陷,所以我会解决这些问题。
//We can define variables outside a while loop
//and use those inside the loop so lets do that
Map trusterMap = new HashMap<String,ArrayList<String>>();
//i is not a "good" variable name,
//since it doesn't explain it's purpose
Int count = 0;
while(myScanner.hasNextInt()) {
//Get the truster and trustee
Int truster = myScanner.nextInt();
Int trustee = myScanner.nextInt();
//Originally you had:
// String listname = truster + i;
//I assume you meant something else here
//since the listname variable is already used
//Add the truster concated with the count to the array
//Note: when using + if the left element is a string
//then the right element will get autoboxed to a string
//Having read your comments using a HashMap is the best way to do this.
ArrayList<String> listname = new ArrayList<String>();
listname.add(truster);
trusterMap.put(truster + count, listname);
i++;
}
此外,您在 myScanner 中存储了一个 Ints 流,这些流将被输入到数组中,但每个都有非常不同的含义(truster
和trustee
)。您是否尝试从文件或用户输入中读取这些内容?有更好的方法来处理这个问题,如果你在下面评论你的意思,我会更新一个建议的解决方案。