![](/img/trans.png)
[英]Creating a three dimensional jagged array to store two dimensional arrays of varying size in java
[英]Creating two Dimensional array with existing arrays in java
我有四個字符串數組,我想創建一個3列和動態行的二維數組。
數組如下:
String[] first_name;
String[] last_name;
String[] unit;
String[] phone_number;
Object[][] obj = new Object[first_name.length()][3]
我的問題是如何實現這樣的目標:
obj = {first_name[index] + " " + last_name[index], unit[index], phone_number[index]}
請幫忙!!!
我假設通過動態行表示它取決於first_name
數組中的元素數量。
所以你可以簡單地迭代:
String[][]obj = new String[first_name.length][3];
for (int i = 0; i < first_name.length; i++)
{
obj[i][0] = first_name[i] + " " + last_name[i];
obj[i][1] = unit[i];
obj[i][2] = phone_number[i];
}
但是 ,這種方法並不是很好。 您應該考慮創建一個名為Employee
的對象作為3個字段,然后您只有一個Employee
數組
例如:
public class Employee
{
String name;
String unit;
String phoneNumber;
public Employee(String name, String unit, String phoneNumber)
{
//... rest of constructor to copy the fields
}
//... setters and getters
}
然后你就得:
Employee[] employees = new Employee[first_name.length];
for (int i = 0; i < first_name.length; i++)
{
employees[i] = new Employee(first_name[i] + " " + last_name[i], unit[i], phone_number[i]);
}
這會有幫助嗎?
int len = first_name.lenghth();
String[][] arr2d = new String[len][3];
for (int i=0; i < len; i++) {
arr2d[i][0] = first_name[i] + " " + last_name[i];
arr2d[i][1] = unit[i];
arr2d[i][2] = phone_number[i];
}
String[] first_name = new String[length];
String[] last_name = new String[length];//length means your length of string
String[] unit = new String[length];
String[] phone_number = new String[length];
Object[][] obj = new Object[first_name.length][3];
for(int index =0;index<first_name.length;index++){
obj[index][0] = first_name[index] + " " + last_name[index];
obj[index][1] = unit[index];
obj[index][2] = phone_number[index];
}
這可能是你正在尋找的:(假設四個數組有相同的長度)
String[][] result = new String[first_name.length][3];
for(int i =0; i<first_name.length;i++){
result[i][0]=first_name[i]+" "+last_name[i];
result[i][1]=unit[i];
result[i][2]=phone_number[i];
}
有幾種方法可以制作3-D陣列。
我會避免使用Object [] [],我從不喜歡處理或性能。
由於3-D數組只是一個數組數組,因此一種簡單的方法是使用List數據結構。 List []或List>應該可以解決問題。 這樣你就擁有了Collection對象的所有內置函數,你還可以在它上面使用Apache commons,lambdaJ或Guava。
如果你沒有使用原始數組,那么你也可以制作一個常規的2-D數組,[],它可以像3-D數組一樣,[] []。
這是我在一個基本數組周圍進行的簡單包裝方法,該方法的功能與3-D數組相同。
public class MyThreeDArray{
int MAX_ROW;
int MAX_COL;
int[] arr;
public MyThreeDArray(final int MAX_ROW, final int MAX_COL){
this.MAX_ROW = MAX_ROW;
this.MAX_COL = MAX_COL;
arr = new int[MAX_ROW * MAX_COL];
}
private int findIndex(int row, int col) throws IllegalArgumentException{
if(row < 0 && row >= MAX_ROW ){
throw new IllegalArgumentException("Invaild row value");
}
if(col < 0 && col >= MAX_COL ){
throw new IllegalArgumentException("Invaild col value");
}
return ( row * MAX_COL + col );
}
public int get(int row, int col){
return arr[findIndex(row, col)];
}
public void set(int row, int col, int value){
arr[findIndex(row, col)] = value;
}
}
還要記住,我從未測試過這些。
因此,如果您使用字符串執行此操作,則第0行可能包含第一個名稱值,而第4行可以包含單位值。
使用此包裝器檢索人員A的數據可以進行類似於此的調用:
String firstName = myWrapper.get(0,0);
String lastName = myWrapper.get(0,1);
String phone = myWrapper.get(0,2);
String unit = myWrapper.get(0,3);
人B的數據將存儲在第二行。
但為什么要嘗試將數組組合在一起? 你可以很容易地成為一個叫做POJO的人
public class Person{
String firstName;
String lastName;
String phone;
String unit;
public Person(){}
//ToDo: Getters and Setters
}
這種方式可以輕松添加驗證並清楚地呼叫特定的人沒有任何麻煩。
Person[] customers = new Person[5];
還是更好
List<Person> customers = new ArrayList<Person>();
您可以創建一個列表一維數組的: List<String []> arrayList = new ...
然后,你可以做arrayList.add(first_name);
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.