2013-04-19 73 views
1

我是Java新手,正在處理數組。我有兩個數組,並希望鏈接它們,以便第二個數組中的元素與第一個數組中的元素相對應。這樣,我可以搜索第一個數組中的一個元素,並在第二個數組中顯示相應的值。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"}; 

我想,這樣當我搜索三月,例如,它會顯示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

所以,你只是想複製一個數組,那麼下面的輸出?您可能想要重新說明這個問題 – jeff

+0

請勿使用2個數組。而是創建一個包含這兩個屬性的自定義對象。然後,您可以將該對象添加到單個數組。 – camickr

+0

兩個數組之間的「鏈接」可以委託給數據結構本身。這正是Map所做的。 –

回答

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 
+0

與我的程序即時通訊讓用戶輸入術語 – user2297518

+0

分配用戶輸入的任何值到變量「期限」,它應該沒事 –

+0

你的意思是指定鍵盤它? – user2297518

2

這是一個「關聯數組」或「地圖」。這是一個Java示例。

Map<String, String> userEmails = new HashMap<String, String>(); 
userEmails.put("Tony", "[email protected]"); 
userEmails.put("Ozzy", "[email protected]"); 

讓我們找到奧茲和打印他的電子郵件地址:

for (String user : userEmails.keySet()) { 
    if ("Ozzy".equals(user)) { 
     System.out.println(userEmails.get(user)); 
    } 
} 

Map是接口。它有「放」和「搞」的操作。 HashMap是「地圖」的一種流行實現,其中地圖鍵(用戶名)具有唯一的哈希碼。

+1

他想要「搜索第一個數組中的元素並在第二個數組中顯示相應的值」。看起來像是一份Map工作。 –

+0

+1是的。映射它。這是一個很糟糕的問題。 – Bohemian

+0

我不確定地圖是如何工作的。就像我說的即時通訊新的Java和我沒有被教過 – user2297518

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]); 
      } 
     } 
    } 
} 
+0

當我運行它,它只是跳過 – user2297518