简体   繁体   English

使用java中的现有数组创建两个维度数组

[英]Creating two Dimensional array with existing arrays in java

I have four arrays of strings and I would like to create a two dimensional array of 3 columns and dynamic rows. 我有四个字符串数组,我想创建一个3列和动态行的二维数组。

the arrays are like: 数组如下:

String[] first_name;
String[] last_name;
String[] unit;
String[] phone_number;


Object[][] obj = new Object[first_name.length()][3]

My problem is how do I achieve something like this: 我的问题是如何实现这样的目标:

obj = {first_name[index] + " " + last_name[index], unit[index], phone_number[index]}

Please help out!!! 请帮忙!!!

I am assuming that by dynamic rows you mean that it depends on the number of elements in the first_name array. 我假设通过动态行表示它取决于first_name数组中的元素数量。

So you could simply iterate: 所以你可以简单地迭代:

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

However , this approach is not very good. 但是 ,这种方法并不是很好。 You should consider creating an object for example named Employee which as the 3 fields, and then you just have an array of Employee 您应该考虑创建一个名为Employee的对象作为3个字段,然后您只有一个Employee数组

For example: 例如:

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
}

And then you just have: 然后你就得:

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

Would this help? 这会有帮助吗?

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

        }

this could be what you are looking for: (Assume that the four arrays have same length) 这可能是你正在寻找的:(假设四个数组有相同的长度)

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

There are a couple of ways to make 3-D arrays. 有几种方法可以制作3-D阵列。

I would avoid Object[][], I never like the handling or performance. 我会避免使用Object [] [],我从不喜欢处理或性能。

Since a 3-D array is just an array of arrays, an easy approach would to use the List data structure. 由于3-D数组只是一个数组数组,因此一种简单的方法是使用List数据结构。 List[] or List> should do the trick. List []或List>应该可以解决问题。 This way you have all the built-ins of a Collection object plus you can also Apache commons, lambdaJ or Guava on top of it. 这样你就拥有了Collection对象的所有内置函数,你还可以在它上面使用Apache commons,lambdaJ或Guava。

If you are dead set on using primitive arrays then you could also make a regular 2-D array, [], that can act like a 3-D array, [][]. 如果你没有使用原始数组,那么你也可以制作一个常规的2-D数组,[],它可以像3-D数组一样,[] []。

Here is simple wrapper method I made around a basic array that will function the same as 3-D array. 这是我在一个基本数组周围进行的简单包装方法,该方法的功能与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;
    }
}

Also keep in mind I never tested any of this. 还要记住,我从未测试过这些。

So if you did this with Strings then row 0 might contain the first name values, ... and row 4 could have the unit values. 因此,如果您使用字符串执行此操作,则第0行可能包含第一个名称值,而第4行可以包含单位值。

To retrieve person A's data with this wrapper could make a call similar to this: 使用此包装器检索人员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);

And person B's data would be stored in the second row. 人B的数据将存储在第二行。

But why try to combine arrays together? 但为什么要尝试将数组组合在一起? You could easy make a POJO called person 你可以很容易地成为一个叫做POJO的人

public class Person{
    String firstName;
    String lastName;
    String phone;
    String unit;

public Person(){}
//ToDo: Getters and Setters
}   

This way could just easily add validation and clearly call a specific person without any trouble. 这种方式可以轻松添加验证并清楚地呼叫特定的人没有任何麻烦。

    Person[] customers = new Person[5];

or better yet 还是更好

    List<Person> customers = new ArrayList<Person>();

您可以创建一个列表一维数组的: List<String []> arrayList = new ...然后,你可以做arrayList.add(first_name);

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM