2017-08-16 84 views
-2

我有这样一个ArrayList:如何将ArrayList <Double>转换为2D数组?我会想一个矩阵

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

使输出,可我转换数组列表=数据[30]到2d数组=数据[6] [5]? 也许这样的输出

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

或者我必须做出6种不同的起始ArrayList中获得矩阵?

+0

难道你真的想要一个二维矩阵,这样你可以在上面或东西做矩阵数学,或者你只是想格式化输出? – ajb

+0

是的选项1:我想做自组织映射到该矩阵 – newcomers

回答

0

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

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

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

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