2014-10-30 88 views
0

我需要存儲25個不同的數組,其中位置1是人的名稱,其餘索引是int,用於執行數學運算。我有一個String arrray [25] [53]。如何使數組[0-25] [1-52]是一個整數?我假設.parseInt,但我不知道他們是如何工作考慮我仍然在學習Java。如何將整數存儲到字符串中的多維數組中

String[][] volunteerNamesAndHours = new String [25][53]; 
    int ID = 0; 
    int week; 
    do{ 
     volunteerNamesAndHours[ID][NAME] = input.next(); 
      for(week = 1; week < 53; week++){ 
       volunteerNamesAndHours[ID][week] = Integer.parseInt(null, ID); 

編輯: 我會使用OOP或地圖,但考慮到我們還沒有得到遠在我當然不希望在我的步邊界,並讓我的教授瘋了。我知道這不是最直觀的,但是這是我最終提出任何身體看到一個問題?

public static String[][] getvolunteerChart(Scanner input){ 
    String[][] volunteerNamesAndHours = new String [25][53]; 
    int ID = 0; 
    int week; 
    do{ 
     volunteerNamesAndHours[ID][NAME] = input.next(); 
      for(week = 1; week < 53; week++){ 
       volunteerNamesAndHours[ID][week] = Integer.toString(input.nextInt()); 
      } 
     ID++;  
    } 
    while(ID <= 24); 

    return volunteerNamesAndHours;   

      } 
+0

還沒到OOP呢。我確定我會在接下來的兩週內這樣做。 – jscincy1 2014-10-30 12:35:31

+0

使用Map查看答案 – brso05 2014-10-30 12:37:40

回答

0

您應該按照面向對象的方法,並在這樣的類封裝的志願者數據:

public class Volunteer { 

    private int id; 

    private String name; 

    private int hours; 

    . 
    . 
    . 

    public in getId() { 
     return id; 
    } 

    public void setid(int id) { 
     this.id = id; 
    } 

    . 
    . 
    . 

} 

現在你可以將它們存儲在一個List或數組。與陣列不同,List通常沒有預定義的大小限制。

List<Volunteer> volunteers = new ArrayList<>(); 

// Or as an array 

Volunteer[] volunteers = new Volunteer[25]; 

然後,您可以遍歷列表,並使用getter和setter這樣做你的數學:

for (Volunteer volunteer : volunteers) { 
    int oldHours = volunteer.getHours(); 
    int newHours = oldHours + 8; 

    volunteer.setHours(newHours); 
} 
-1

如果你堅持要用一個String數組,你將需要使用Integer類的toString功能。

E.g.

Integer.toString(42); 

但是,這會在嘗試稍後對它們執行數學運算時引起疼痛。

+0

我需要每個數組(array [0] [0],array [1] [0] ...等)的第一個索引作爲名稱,從而使我將其聲明爲字符串數組。 – jscincy1 2014-10-30 12:37:24

+0

好吧,我錯過了。你仍然想使用整數數組。把一堆整數你想在字符串數組中進行數學運算會導致痛苦。因此,使用其他答案中建議的映射可能是這裏的一種方式。 – cowls 2014-10-30 12:40:15

1

我會建議使用HashMap中關鍵和整數數組列表作爲價值存儲的名字一樣

Map<String, ArrayList<Integer>> volunteerNamesAndHoursMap = new HashMap<String, ArrayList<Integer>>(); 
1

你需要使用一個Map結構,該用例。

Map<String, Integer[]> personIDs = new HashMap<>(25); 

personIDs.put("Peter", new Integer[]{5,22,7734}); 

personIDs.get("Peter");//returns the array 5,22,7734 
0

首先,我認爲你需要一個字符串數組,然後整數長度爲52的多維數組。Java是一種非常靜態的語言,它不會讓你做一些瘋狂的事情,比如將整數存儲在一個字符串數組中。

String[] volunteerNames = new String [25]; 
int[] volunteerHours = new int [25][52]; 

所有第二,我覺得你真的可以使用一些不錯的對象定位在這裏,使一個類志願者,並在其中存儲與名稱的字符串和INT的爲那些小時的數組。

(PS:那麼試着搜索一下getter和setters,通常的做法是用private scope聲明類變量並定義訪問它們的方法,但是我在這裏用public scope來保持這個例子的簡短)

public class Volunteer { 

    public String name; 
    public int[] hours; 

    Volunteer(String name, int[] hours) { 
     this.name = name; 
     this.hours = hours; 
    } 
} 

第三,如果你真的想像你這樣做,我想你應該使用一個地圖。

Map<String, Integer[]> volunteersHours = new HashMap<String, Integer[]>(); 
volunteersHours.put("John", new Integer[] {1, 2, 3, 4, etc...}); 

volunteersHours.get("John"); // will return the hours array 
相關問題