嗨我正在尝试将二维数组转换为三个单维数组

时间:2015-06-15 05:51:45

标签: java arrays

您正在尝试将二维数组转换为三个单维数组,但我怎么也不知道有谁知道请帮帮我

这里我的数组就在那里

install.packages(ISLR);

我想得到:

double[][] data= {{97, 36, 79}, {94, 74, 60}, {68, 76, 58}, {64, 87, 56}, {68, 27, 73}, {74, 99, 42}, {7, 93, 87}, {51, 69, 40}, {38, 23, 33}, {57, 86, 31}};

代码段:

double[] xarry={97,94,68,.....}//first elements

double[] yarry={36,74,76,.....}//second elements

double[] bubble_size={79,60,58,.....}//third element

4 个答案:

答案 0 :(得分:2)

如果你自己尝试过,那真的不是那么难。

  • 获取data数组的长度,创建3个具有该长度的1-D双数组。
  • 遍历data数组,使用索引0-2将正确的值放入1-D数组中

<强>代码:

public class QuickTester {

    public static void main(String[] args) {

        double [][] data= {{97, 36, 79}, {94, 74, 60},
                {68, 76, 58}, {64, 87, 56}, {68, 27, 73},
                {74, 99, 42}, {7, 93, 87}, {51, 69, 40},
                {38, 23, 33}, {57, 86, 31}};

        double [] xArr = new double[data.length];
        double [] yArr = new double[data.length];
        double [] bubbleSizeArr = new double[data.length];

        for(int i = 0; i < data.length; i++) {
            xArr[i] = data[i][0];
            yArr[i] = data[i][1];
            bubbleSizeArr[i] = data[i][2];
        }

        // Test printing
        for(int i = 0; i < data.length; i++) {
            System.out.printf("%5.2f %5.2f %5.2f%n", xArr[i],
                    yArr[i], bubbleSizeArr[i]);
        }
    }
}

<强>输出:

97.00 36.00 79.00
94.00 74.00 60.00
68.00 76.00 58.00
64.00 87.00 56.00
68.00 27.00 73.00
74.00 99.00 42.00
 7.00 93.00 87.00
51.00 69.00 40.00
38.00 23.00 33.00
57.00 86.00 31.00

答案 1 :(得分:1)

我添加了示例代码以从3维数组中获取值。应该很简单。您可以使用Java中的arrayList来存储值。扩展附加的程序并解决您的问题。

for (int i = 0; i < data.length; i++) {
        System.out.println("xarray - " + data[i][0]);
        System.out.println("yarray - " + data[i][1]);
        System.out.println("bubble - " + data[i][2]);
    }

答案 2 :(得分:1)

这很简单。如果你需要一些清晰度,请通过并告诉我。 公共课ThreeDto2D {

public static void main(String[] args) {
    double[][] data= {{97, 36, 79}, {94, 74, 60}, {68, 76, 58}, {64, 87, 56}, {68, 27, 73}, {74, 99, 42}, {7, 93, 87}, {51, 69, 40}, {38, 23, 33}, {57, 86, 31}};

    double data1[] = new double[data.length];
    double data2[] = new double[data.length];
    double data3[] = new double[data.length];

    for (int x= 0;x < data.length;x++)
    {           
        for (int y=0; y < data[x].length ;y++)
        {
            if (y==0)
                data1 [x] = data [x][y];
            else if (y==1)
                data2 [x] = data [x] [y];
            else if (y==2)
                data3 [x] = data [x] [y];               
        }           
    }

    for (int i=0;i<data1.length;i++)
    {
        System.out.print(data1[i]+" ");
        System.out.print(data2[i]+" ");
        System.out.print(data3[i]+" ");
        System.out.println();
    }

}

}

输出: 97.0 36.0 79.0 94.0 74.0 60.0 68.0 76.0 58.0 64.0 87.0 56.0 68.0 27.0 73.0 74.0 99.0 42.0 7.0 93.0 87.0 51.0 69.0 40.0 38.0 23.0 33.0 57.0 86.0 31.0

答案 3 :(得分:1)

我正在根据您的建议改进我的代码。

public double[] getX_Array(double[][] bubble_Data) {
    double minValue = 0;
    double positiveValue = 0;
    int count = 0;

    x_Array = new double[bubble_Data.length];
    y_Array = new double[bubble_Data.length];
    size_Array = new double[bubble_Data.length];

    for (int i = 0; i < bubble_Data.length; i++) {

        x_Array[i] = bubble_Data[i][0];
        y_Array[i] = bubble_Data[i][1];
        size_Array[i] = bubble_Data[i][2];
    }

    System.out.println("single array of positve values " +
            Arrays.toString(size_Array));
    return x_Array;
}