我是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");
}
我有这个代码来显示一个月出现的次数,我只需要打印出这个年后的几年。
答案 0 :(得分:2)
这是一个“关联数组”或“地图”。这是一个Java示例。
Map<String, String> userEmails = new HashMap<String, String>();
userEmails.put("Tony", "tony@metal.com");
userEmails.put("Ozzy", "ozzy@metal.com");
让我们找到Ozzy并打印他的电子邮件地址:
for (String user : userEmails.keySet()) {
if ("Ozzy".equals(user)) {
System.out.println(userEmails.get(user));
}
}
Map是界面。它有“put”和“get”操作。 HashMap是“Map”的流行实现,其中地图键(用户名)具有唯一的哈希码。
答案 1 :(得分:1)
看看我的代码,我解释了我所做的一切。
import java.util.Scanner;
public class GetPrice {
public static void main(String args[]) {
// You can add any number of elements in both the arrays. The lengths
// should, of course, be the same for both the arrays.
String items[] = { "pizza", "cheesebread", "stromboli" };
double prices[] = { 1.1, 2.2, 3.3 };
// What we need to do is, once the user inputs the item, we need to
// search the string and find the index. As the prices are in the
// corresponding indices on the other array, we can just use the index
// number to get the price from the other array. So we just use the same
// index but on a different array.
System.out.println("Choose from the following, to get the price: ");
for (int index = 0; index < items.length; index++)
System.out.println(items[index]);
System.out.println("\nEnter the item: ");
Scanner input = new Scanner(System.in);
String inputItem = input.next();
for (int index = 0; index < items.length; index++) {
if (items[index].equals(inputItem.toLowerCa… {
System.out.println("Price for '" + items[index] + "' is: " + prices[index]);
}
}
}
}
答案 2 :(得分:0)
尝试
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" };
String term = "March";
List<Short> indexes = new ArrayList<Short>();
for (int i = 0; i < Months.length; i++) {
String string = Months[i];
if (term.equals(string)) {
indexes.add(Years[i]);
}
}
for (Short short1 : indexes) {
System.out.print(short1);
}
更新
Scanner keyboard = new Scanner(System.in);
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" };
String term = keyboard.next();
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");
}
List<Short> indexes = new ArrayList<Short>();
for (int i = 0; i < Months.length; i++) {
String string = Months[i];
if (term.equals(string)) {
indexes.add(Years[i]);
}
}
for (Short short1 : indexes) {
System.out.print(short1);
}
}
它为输入March
The month Marchappears 3 times
200420092011