我通过这样的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]);
}
}
}
使用数组时请记住,数组数据结构只能保存一种类型的数据。这实质上意味着如果一个数组声明为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));
欢迎来到java的wold。
以下是一些您似乎没有做得好的事情,因为您知道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("--------------")
}
}
它可以通过两种方式解决,一维和二维,如果您考虑将不同的数据类型放在单个数组中,这在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]);
}
}}
由于您的数组格式不正确且未定义变量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");
}