【问题标题】:java: formatting a result set into an arrayjava:将结果集格式化为数组
【发布时间】:2012-03-07 17:42:26
【问题描述】:

我的 java 生锈了,我想知道是否有人可以给我一个如何执行以下操作的代码示例:

我有一个来自数据库调用的结果集,它返回下表:

object_id(int), marker_id(int), xpos(float), ypos(float)

结果按 object_id 分组,这样您就有了这样的结果:
1023, 19, 0.2, 0.8
1023, 63, 0.2, 0.9
1023, 63, 0.2, 0.9
1072, 63, 0.2, 0.23
1072, 63, 0.2, 0.9
1072, 63, 0.2, 0.6
1012, 63, 0.2, 0.6
1012, 63, 0.2, 0.6

我一直在寻找生成两个二维双精度数组的最有效方法,这样 第一个有 double[][] array1 = {
{0.2,0.8,0.2,0.9...},
{0.2,0.8,0.2,0.9...},
...}

每个子数组包含序列 x1,y1,x2,y2,x3,y3 for row1,row2,etc... 用于结果集中对应的 object_id。

第二个二维数组将包含:
double[][] array2 = {
{1.0},
{0.9},
{0.8},
...
{0.0}}

每个唯一的 object_id 都有一个条目,第一个条目有 1.0,最后一个条目有 0.0

两个数组应该有相同的长度,因为每个条目代表一个object_id

希望这是有道理的

谢谢

【问题讨论】:

    标签: java


    【解决方案1】:

    以下已编辑答案

    “我需要将其转换为数组以输入到另一个库。– user257543”

    为了做到这一点,一种方法是将列转换为数组,然后根据需要组合它们。

        //get the size of the ResultSet
        myResultSet.last();
        int resultSetSize = myResultSet.getRow();
        //initialize your arrays to be joined
        int[] array1 = new int[resultSetSize];
        int[] array2 = new int[resultSetSize];
       //loop through the result so array1 will be the same as column1 of your table, etc.
        int count = 0;
        myResultSet.beforeFirst();
        while (myResultSet.next()){
            array1[count] = myResultSet.getInt("my column 1");
            array2[count] = myResultSet.getInt("my column 2");
            count++;
        }
        //declare rows and columns for easy code readability
        int row = 0;
        int column = 2;
        //set your combined array row and column lengths
        int[][] aCombined = new int[array1.length][column];
        //reset your column value to put array1 down column 0
        column = 0;
        for (int tempInt : array1){
            aCombined[row][column] = tempInt;
            row++;
        }
        //reset your row and go to the next column to do the same as above
        row = 0;
        column = 1;
        for (int tempInt : array2){
            aCombined[row][column] = tempInt;
            row++;
        }
    

    使用上面的代码,如果您的原始数组是: int[] array1 = {1, 2, 3, 4}; int[] array2 = {5, 6, 7, 8}; 然后 aCombined 将导致 [[1, 5], [2, 6], [3, 7], [4, 8]]

    可视化为

    • [column1/array1,column2/array2]

    • [1, 5]

    • [2, 6]

    • [3, 7]

    • [4, 8]

    原始答案如下

    为什么需要将其转换为数组?为什么不只使用结果集本身?

    例如,如果您需要获取所有object_id为1072的对象的marker_id,只需:

    public ArrayList getMarkerID(int objectId){
         ArrayList<int> myArrayList = new ArrayList<>();
         myResultSet.beforeFirst();
         while (myResultSet.next()){
             if (myResultSet.getInt("object_id") == objectId){
                  myArrayList.add(myResultSet.getInt("marker_id"));
             }
          }
          return myArrayList;
      }
    

    (我只是快速输入了这个,并没有测试它,所以我可能缺少分号或者逻辑可能有点不对)

    【讨论】:

    • 我需要将其转换为数组以输入到另一个库。
    • 我喜欢数组方法,但我希望有一种可以检测 object_id 并结合结果的技术(如原始问题中所述)。尽管我认为我知道如何通过调整您的方法来做到这一点。谢谢!
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2019-03-08
    • 1970-01-01
    • 1970-01-01
    • 2015-03-20
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多