2012-06-20 74 views
1

嗨,如何組織不同類型的數組?

我是新來的Java和試圖找出如何將這個數據推入數組(6行,3列)?

x1 John 6 
x2 Smith 9 
x3 Alex 7 
y1 Peter 8 
y2 Frank 9 
y3 Andy 4 

然後,我會從最後一列進行數學計算。

這是我的代碼...

public class Testing { 
    public static void main(String[] args) { 

     Employee eh = new Employee_hour(); 

     Employee_hour [] eh_list = new Employee_hour[6]; 
     eh_list[0] = new Employee_hour("x1", "John", 6); 
     eh_list[1] = new Employee_hour("x2", "Smith", 9); 
     eh_list[2] = new Employee_hour("x3", "Alex", 7); 
     eh_list[3] = new Employee_hour("y1", "Peter", 8); 
     eh_list[4] = new Employee_hour("y2", "Frank", 9); 
     eh_list[5] = new Employee_hour("y3", "Andy", 4); 
     print(eh_list); 
    } 

    private static void print(Employee_hour[] mass){ 
     for (int i = 0; i < mass.length; i++) { 
      System.out.print(mass[i] + " "); 
     } 
     System.out.println(); 
    } 
} 

但我得到這個作爲輸出...

[email protected] [email protected] testing.Employee_hour @ 420a52f [email protected] [email protected] [email protected]

如何從最後一列獲得數字?

+0

它需要是一個數組嗎?它不是最高效的數據結構 –

+0

您是否必須使用數組或者是否可以使用其他數據結構?什麼是數據表示以及如何使用它?如果將數據放入Object [] []數組中,則以後使用該數據將會很麻煩。 – pgras

+0

如果它不一定是一個數組,它可能值得使用地圖。 –

回答

3

Java是強類型的,所以你不能只創建一個接受任何類型的數組。
但是,您可以製作一個類型爲Object的多維數組,並使用java.lang.Integer作爲整數值。
另一種方法是創建一個表示表中行的類,並創建該類的數組。

9

爲什麼不爲你的記錄創建一個特定的Java bean?

class Person { 
    String id; 
    String name; 
    int anotherNumber; 
    // constructor, getters, setters 
} 

,然後用它是這樣的:

Person [] people = new Person[10]; 
people[0] = new Person("x1", "John", 6); 
... 

或者更好的是採用java.util.List而非陣列。

字段訪問

爲了訪問不同的領域,您可能需要讓你的領域進行的公共(非常糟糕的主意),並簡單地稱它們爲object_instance.field_name,或提供所謂的干將:

class Person { 
    String id; 
    String name; 
    int anotherNumber; 
    // constructor, getters, setters 

    public int getAnotherNumber() { 
    return anotherNumber; 
    } 
} 

打印時,然後調用它:

for (int i = 0; i < mass.length; i++) { 
    System.out.print(mass[i].getAnotherNumber() + " "); 
} 

什麼爲什麼ÿ ou試過沒有工作:

System.out.println(mass[0])在你的情況下將打印整個對象表示,默認情況下它打印它在你的情況下做了什麼。

class Person { 
    String id; 
    String name; 
    int anotherNumber; 
    // constructor, getters, setters 

    public String toString() { 
    return "{id="+id+", name="+name+", anotherNumber="+anotherNumber+"}"; 
    } 
} 
+0

我同意這種做法,尤其是'List'建議。我會在泛型中使用'ArrayList',這樣你就只能傳遞'Person'對象。 – eboix

+0

再次謝謝Maksimov,你的更新文章完全幫助我。 – devger

1

Array是一種安全的集合,你可以使用通用對象數組:爲了更好的,你需要重寫ObjectString toString()方法做到這一點。其他方式使用collection Api

2

如果你想存儲它作爲數組用戶2維數組。這裏是一個示例。

String[][] a2 = new String[10][5]; 
    for (int i=0; i<a2.length; i++) { 
    for (int j=0; j<a2[i].length; j++) { 
    a2[i][j] = i; 
    System.out.print(" " + a2[i][j]); 
    } 
    System.out.println(""); 
    } 
    } 

更好的辦法是cretae對象

class User { 
    String id; 
    String userName; 
    int userSomeValue; 

// 
} 

然後用

userList.get(index); 
retriving內容推到一個列表

User ob1=new User(); 
// set the values. 
List<User> userList=new ArrayList<User>(); 
userList.add(ob1); 

使用此列表處理。第

0

有是將數據存儲在數組中的一些方法,但只能在一個數組中存儲相同類型的值。例如只是String,int或float。在你的情況下,你必須使用String,但即使它包含一個數字,你也不能使用字符串類型變量進行任何計算。我會建議maksimov描述的方式。