[英]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.