Creating two Dimensional array with existing arrays in java

丶灬走出姿态 提交于 2020-01-14 09:06:14

问题


I have four arrays of strings and I would like to create a two dimensional array of 3 columns and dynamic rows.

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!!!


回答1:


I am assuming that by dynamic rows you mean that it depends on the number of elements in the first_name array.

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

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



回答2:


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



回答3:


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



回答4:


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

        }



回答5:


There are a couple of ways to make 3-D arrays.

I would avoid Object[][], I never like the handling or performance.

Since a 3-D array is just an array of arrays, an easy approach would to use the List data structure. List[] or List> should do the trick. 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.

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

Here is simple wrapper method I made around a basic array that will function the same as 3-D array.

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.

To retrieve person A's data with this wrapper could make a call similar to this:

    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.

But why try to combine arrays together? You could easy make a POJO called person

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>();



回答6:


You could create a List of 1D arrays: List<String []> arrayList = new ... Then you can do arrayList.add(first_name);



来源:https://stackoverflow.com/questions/13954627/creating-two-dimensional-array-with-existing-arrays-in-java

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!