【问题标题】:How to convert ArrayList<Double> to 2D array? I'll want a matrix如何将 ArrayList<Double> 转换为二维数组?我想要一个矩阵
【发布时间】:2018-01-24 01:52:30
【问题描述】:

我有一个这样的 ArrayList:

[2.0, 3.0, 5.0, ...., 9.0] // has 30 value inside

我从这段代码中得到了输出

ArrayList<Double> data = new ArrayList<Double>();

        for (Document matrix : matrices) {

            Double column1 = matrix.getDouble("column1");
            data.add(column1);
            Double column2 = matrix.getDouble("column2");
            data.add(column2);
            Double column3 = matrix.getDouble("column3");
            data.add(column3);
            Double column4 = matrix.getDouble("column4");
            data.add(column4);
            Double column5 = matrix.getDouble("column5");
            data.add(column5);
        }

        System.out.println("Current array list is:"+data);

那么,我可以将数组 list = data[30] 转换为 2d array = data[6][5] 吗? 也许像这样的输出

data[6][5] :
2.0 3.0 5.0 ... ...
.
.
.
.
... ... ... ... 9.0

或者我必须做6个不同的初始化ArrayList才能得到矩阵?

【问题讨论】:

  • 你真的想要一个二维矩阵,这样你就可以对它进行矩阵数学运算之类的,还是只想格式化输出?
  • 是选项 1:我想做自组织映射到该矩阵

标签: java arrays matrix arraylist multidimensional-array


【解决方案1】:

我不久前为矩阵写了这篇文章。它处理不同的形式(列表、数组、二维数组等)。应该是你需要的

package com.bossanova.hitl.utils;

import com.google.gson.Gson;
import com.google.gson.reflect.TypeToken;
import java.lang.reflect.Array;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;

/**
 *
 * @author Dan
 *
 * @see This has moved to commons
 */
@Deprecated
public class ArrayUtils {

    private static Gson gson;
    static{
        gson = new Gson();
    }

    public static <T extends Number> T[][] convertListTo2DArray(List<T> elements, Class<T> numberClass, int rows, int cols) {
        if (rows * cols < elements.size()) {
            throw new IndexOutOfBoundsException("Theres not going to be enough room in the array for all the data");
        }
        @SuppressWarnings("unchecked")
        T[][] array = (T[][]) Array.newInstance(numberClass, rows, cols);
        int index = 0;
        for (int row = 0; row < rows; row++) {
            for (int col = 0; col < cols; col++) {
                array[row][col] = elements.get(index);
                index++;
            }
        }
        return array;
    }

    public static double[][] convertDoubleListTo2DPrimativeArray(List<Double> elements, int rows, int cols) {
        Double[][] twoDArray = convertListTo2DArray(elements, Double.class, rows, cols);
        double[][] retVal = new double[rows][cols];
        for (int row = 0; row < rows; row++) {
            for (int col = 0; col < cols; col++) {
                retVal[row][col] = twoDArray[row][col];
            }
        }
        return retVal;
    }

    public static List<Double> convert2DArrayToList(double[][] data) {
        int rows = data.length;
        int cols = data[0].length;
        List<Double> retVal = new ArrayList<>();
        for (int row = 0; row < rows; row++) {
            for (int col = 0; col < cols; col++) {
                retVal.add(data[row][col]);
            }
        }
        return retVal;
    }

    public static double[][] deepCopy(double[][] original) {
        final double[][] result = new double[original.length][];
        for (int i = 0; i < original.length; i++) {
            result[i] = Arrays.copyOf(original[i], original[i].length);
        }
        return result;
    }

    public static double[][] convertJsonDoubleArrayTo2DArray(String json, int rows,int cols){
        return convertDoubleListTo2DPrimativeArray(convertJsonDoubleArrayToDoubleList(json), rows, cols);
    }

    public static List<Double> convertJsonDoubleArrayToDoubleList(String json){
        List<Double> doubleList = gson.fromJson(json, new TypeToken<List<Double>>(){}.getType());
        return doubleList;
    }
}

【讨论】:

    【解决方案2】:

    只需做一些基本的算法来查找你想要的坐标。

    static Double getXY(List<Double> list, int x, int y, int numberOfColumns) {
        return list.get(y * numberOfColumns + x);
    }
    

    【讨论】:

      【解决方案3】:

      也许其他答案更好,但这解决了我的问题:

      data.toArray();
      
              double[][] matrix2D = new double[ribet.size()/5][5];
      
              int k=0;
      
              for (int i=0; i<data.size()/5; i++) {
                  for (int j=0; j<5; j++) {
                      matrix2D[i][k%5] = data.get(k);
                      k = k+1;
                  }
              }
      

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 2011-07-01
        • 1970-01-01
        • 2021-10-12
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2011-04-18
        相关资源
        最近更新 更多