1

我是 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");
}

我有这个代码来显示一个月出现了多少次,我只需要它打印出这之后的几年。

4

3 回答 3

3

那是一个“关联数组”或“地图”。这是一个 Java 示例。

Map<String, String> userEmails = new HashMap<String, String>();
userEmails.put("Tony", "tony@metal.com");
userEmails.put("Ozzy", "ozzy@metal.com");

让我们找到 Ozzy 并打印他的电子邮件地址:

System.out.println(userEmails.get("Ozzy"));

地图是界面。它有“put”和“get”操作。 HashMap是“Map”的一种流行实现,其中映射键(用户名)具有唯一的哈希码。

于 2013-04-19T02:39:40.417 回答
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]);
            }
        }
    }
}
于 2013-04-19T02:40:06.493 回答
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
于 2013-04-19T02:53:20.800 回答