【问题标题】:Creating two Dimensional array with existing arrays in java在java中使用现有数组创建二维数组
【发布时间】:2012-12-06 22:30:06
【问题描述】:

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

请帮忙!!!

【问题讨论】:

  • 动态行是什么意思?如果它是一个数组,您需要以某种方式定义每个维度的长度。否则使用List

标签: java arrays codenameone


【解决方案1】:

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

【讨论】:

    【解决方案2】:

    这会有帮助吗?

    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】:

      这可能是您要查找的内容:(假设四个数组的长度相同)

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

        【讨论】:

        • 第二个维度的索引错误。 Java 是从 0 开始的,所以它们必须是 [0], [1], [2]
        【解决方案5】:

        有几种方法可以制作 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>();
        

        【讨论】:

          【解决方案6】:

          您可以创建一维数组的ListList&lt;String []&gt; arrayList = new ... 那你就可以arrayList.add(first_name);

          【讨论】:

            猜你喜欢
            • 1970-01-01
            • 2017-09-28
            • 1970-01-01
            • 1970-01-01
            • 2023-04-08
            • 2012-08-27
            • 1970-01-01
            • 1970-01-01
            相关资源
            最近更新 更多