我是 Java 新手,我正在处理数组。我有两个数组,并希望将它们链接起来,以便第二个数组中的元素与第一个数组中的元素相对应。这样,我可以在第一个数组中搜索一个元素并在第二个数组中显示相应的值。
short[] Years = {2000, 2001, 2002, 2003, 2004, 2005, 2006, 2007, 2008, 2009, 2010, 2011, 2012};
String[] Months = {"January", "February", "June", "January", "March", "June", "July", "August", "September", "March", "November", "March", "June"};
我正在尝试链接它,例如,当我搜索 March 时,它会显示2004, 2009, 2011
List<String> results = new ArrayList<String>();
for (String s : months)
{
if(s.equals(term))
{
results.add(s);
}
}
if (results.size() > 0)
{
System.out.println("The month " + term + "appears " + results.size() + " times");
}
else
{
System.out.println("Your search for " + term + " did not return any results");
}
我有这个代码来显示一个月出现了多少次,我只需要它打印出这之后的几年。