JAVA GET API-2D数组,无法检索?

时间:2018-09-08 13:03:26

标签: java arrays 2d

我正在通过这样的API获取可变数据(注意方括号):

[
    ["2018-09-03",287.5,289.8,286.15,287.3,287.65,1649749.0,4750.35],
    ["2018-08-31",286.25,290.5,285.0,285.5,285.95,3716997.0,10691.41],
    ["2018-08-30",286.45,290.55,284.6,286.05,285.6,3861403.0, 11097.03]
]

我在以下脚本中做错了什么?我是Java新手,需要在表中打印此块。请帮助我,在此先感谢

public class ArrayLoopTest{

     public static void main(String []args){
         String[] data = new String[
            ["2018-09-03",287.5,289.8,286.15,287.3,287.65,1649749.0,4750.35],
            ["2018-08-31",286.25,290.5,285.0,285.5,285.95,3716997.0,10691.41],
            ["2018-08-30",286.45,290.55,284.6,286.05,285.6,3861403.0, 11097.03]
        ];
        for (i=0;i < data.length;i++) {
            System.out.println(data[i]);
        }
     }
}

4 个答案:

答案 0 :(得分:1)

欢迎使用Java。

有些事情您似乎做得不好,因为您知道Java是一种强类型语言。但是从代码中,您正在数组中使用浮点数。也许您已经声明您的数组将仅包含字符串。

更多,因此您不会以正确的方式声明数组。  我建议您快速Arrays in java来完成本教程。

但是要解决您的问题,您可以改为

p

ublic static void main(String []args){
            String[][] data = new String[][]{

                {"2018-09-03","287.5","289.8","286.15","287.3","287.65","1649749.0","4750.35"},
                    {"2018-09-03","287.5","289.8","286.15","287.3","287.65","1649749.0","4750.35"},
                    {"2018-09-03","287.5","289.8","286.15","287.3","287.65","1649749.0","4750.35"},

            };



            for (int i=0;i < data.length;i++) {
                    for (int j = 0; j < data[i].lenght(); i++){
`                        System.out.print(data[i][j] + "  ");
                      }
                      System.out.println("--------------")


            }
        }

答案 1 :(得分:0)

由于您的数组格式不正确且未定义变量i,因此无法编译。您需要遍历每个子数组中的项目并打印它们。

String[][] data = new String[][]{
    { "2018-09-03", "287.5", "289.8", "286.15", "287.3", "287.65", "1649749.0", "4750.35" },
    { "2018-08-31", "286.25", "290.5", "285.0", "285.5", "285.95", "3716997.0", "10691.41" },
    { "2018-08-30", "286.45", "290.55", "284.6", "286.05", "285.6", "3861403.0", "11097.03" }
};
for (int i = 0; i < data.length; i++) {
    for (int j = 0; j < data[i].length; j++) {
        System.out.print(data[i][j] + " ");
    }
    System.out.print("\n");
}

答案 2 :(得分:0)

在使用数组时,请记住,数组数据结构只能保存一种类型的数据。这实质上意味着,如果将数组声明为String,则它只能具有String类型的数据。

因此,您可以使用以下代码定义和打印2D阵列。

public static void main(String[] args) {        

    String[][] data = {
            {"2018-09-03","287.5","289.8","286.15","287.3","287.65","1649749.0","4750.35"}      
            ,{"2018-08-31","286.25","290.5","285.0","285.5","285.95","3716997.0","10691.41"}
            ,{"2018-08-30","286.45","290.55","284.6","286.05","285.6","3861403.0", "11097.03"}
    };


    for(int i=0;i<data.length;i++) {
        for(int j=0;j<data[i].length;j++) {
            System.out.print(data[i][j]);
        }
        System.out.println();
    }

    // or you can print like this

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

        System.out.println(Arrays.toString(data[i])+",");

    }
}

编辑:

您可以使用如下所示的com.google.gson库来获取2D array

//String apiResponse = get Api Response
JsonParser parser = new JsonParser();    // parser converstthe api response to Json
JsonObject obj = (JsonObject) parser.parse(apiResponse);
JsonObject obj1 = obj.getAsJsonObject("dataset");
JsonArray arr = obj1.get("data").getAsJsonArray();

String[][] newString = new String[arr.size()][arr.get(0).getAsJsonArray().size()];

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

            for(int j=0;j<newString[i].length;j++) {


                newString[i][j] = arr.get(i).getAsJsonArray().get(j).getAsString();

            }

        }


System.out.println(Arrays.deepToString(newString));

答案 3 :(得分:0)

如果您想将不同的数据类型放在一个数组中,可以用两种方法解决,一维和二维,这在Java中是不可能的。您只能在定义的数组中放入一种数据类型类型。

二维数组代码:

public class ArrayLoopTest{

 public static void main(String []args){
     String[][] data = 
     {
        {"2018-09-03","287.5","289.8","286.15","287.3","287.65","1649749.0","4750.35"},
        { "2018-08-31","286.25","290.5","285.0","285.5","285.95","3716997.0","10691.41"},
        {  "2018-08-30","286.45","290.55","284.6","286.05","285.6","3861403.0", "11097.03"}
     };
    for (int i=0;i < data.length;i++) {
    {
        for(int j=0;j<data[i].length;j++)
        {
            System.out.print(data[i][j]+" "); 
        }
        System.out.println();
    }
    }

 }}

一维数组代码

public class ArrayLoopTest{
public static void main(String []args){
     String[] data ={"2018-09-03,287.5,289.8,286.15,287.3,287.65,1649749.0,4750.35",
                     "2018-08-31,286.25,290.5,285.0,285.5,285.95,3716997.0,10691.41",
                     "2018-08-30,286.45,290.55,284.6,286.05,285.6,3861403.0, 11097.03"};
    for (int i=0;i < data.length;i++) 
    {
        System.out.println(data[i]);
    }
 }}