在Java中使用现有数组创建二维数组

5

我有四个字符串数组,希望创建一个有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]}

Please help out!!!


4
动态行是什么意思?如果它是一个数组,你需要以某种方式定义每个维度的长度。否则,请使用“List”。 - jbx
6个回答

4

我假设您所说的“动态行”是指它取决于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]);
}

1

这个有帮助吗?

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];
}

1
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];

        }

第二维的索引错误了。Java是以0为基础的,所以它们必须是[0]、[1]、[2]。 - jbx

1

这可能是您正在寻找的内容:(假设四个数组具有相同的长度)

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];
        }

1

有几种方法可以创建3D数组。

我会避免使用Object[][], 我从来不喜欢它的处理或性能。

由于3D数组只是数组的数组,一个简单的方法是使用List数据结构。List[]或List>应该可以胜任。这样你就拥有了所有集合对象的内置功能,还可以在其上使用Apache commons、lambdaJ或Guava。

如果你一定要使用原始数组,那么你也可以创建一个常规的2D数组,[],它可以像3D数组,[][]一样运作。

这里是我围绕基本数组制作的简单包装方法,它将与3D数组相同地运行。

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的数据将存储在第二行。

但是为什么要尝试将数组合并在一起呢?您可以轻松地创建一个名为Person的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>();

0
你可以创建一个由一维数组组成的列表List<String []> arrayList = new ... 然后可以执行 arrayList.add(first_name);

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接