java - 在 Android-Java 中读取数组时出现奇怪的行为

标签 java android android-studio

我正在尝试从 JSON 获取 int 值数组,但是发生了一些奇怪的事情,当我在代码中的两个位置读取相同的数组时,我得到了不同的输出。

有人知道这种行为背后的原因吗?

public LineGraphSeries<DataPoint> fillGraph(String city, int numberOfDays){
    int data[] = new int[numberOfDays*8];
    DataPoint[] temperatures = new DataPoint[numberOfDays*8];
    LineGraphSeries<DataPoint> graph;
    String url = "http://api.worldweatheronline.com/free/v2/weather.ashx?q=" + city + "&format=json&num_of_days=" + numberOfDays + "&cc=no&fx24=no&show_comments=no&tp=3&key=e74975c820b1f6506bd6b9fdea5a5";
    JSONObject dataZNetu;
    JSONArray dataArray;
    JSONObject dataHourly;
    JSONArray dataHourlyArray;
    try {
        dataZNetu = requestWebService(url).getJSONObject("data");
        dataArray = dataZNetu.getJSONArray("weather");
        for(int i = 0; i<dataArray.length(); i++){
            dataHourly = dataArray.getJSONObject(i);
            dataHourlyArray = dataHourly.getJSONArray("hourly");
            for(int j = 0; j<dataHourlyArray.length(); j++){
                data[i*j] = dataHourlyArray.getJSONObject(j).getInt("FeelsLikeC");
                //temperatures[i*j] = new DataPoint(i*j,data[i*j]);
                Log.v("dataCorrect" + i,String.valueOf(data[i*j])); //Correct values
            }
        }
    }
    catch (Exception e) {
        e.printStackTrace();
    }
    for(int i = 0;i<data.length;i++){
        Log.v("dataBroken" + i/8,String.valueOf(data[i])); //Broken values
    }

    graph = new LineGraphSeries<DataPoint>(temperatures);
    return graph;
}

日志:

05-15 02:05:50.609  13125-13125/com.example.tomus.weatherdresser V/dataCorrect0﹕ 6

05-15 02:05:50.609  13125-13125/com.example.tomus.weatherdresser V/dataCorrect0﹕ 5

05-15 02:05:50.619  13125-13125/com.example.tomus.weatherdresser V/dataCorrect0﹕ 12

05-15 02:05:50.619  13125-13125/com.example.tomus.weatherdresser V/dataCorrect0﹕ 18

05-15 02:05:50.619  13125-13125/com.example.tomus.weatherdresser V/dataCorrect0﹕ 19

05-15 02:05:50.619  13125-13125/com.example.tomus.weatherdresser V/dataCorrect0﹕ 20

05-15 02:05:50.619  13125-13125/com.example.tomus.weatherdresser V/dataCorrect0﹕ 16

05-15 02:05:50.619  13125-13125/com.example.tomus.weatherdresser V/dataCorrect0﹕ 14

05-15 02:05:50.619  13125-13125/com.example.tomus.weatherdresser V/dataCorrect1﹕ 10

05-15 02:05:50.619  13125-13125/com.example.tomus.weatherdresser V/dataCorrect1﹕ 7

05-15 02:05:50.619  13125-13125/com.example.tomus.weatherdresser V/dataCorrect1﹕ 16

05-15 02:05:50.619  13125-13125/com.example.tomus.weatherdresser V/dataCorrect1﹕ 20

05-15 02:05:50.619  13125-13125/com.example.tomus.weatherdresser V/dataCorrect1﹕ 24

05-15 02:05:50.619  13125-13125/com.example.tomus.weatherdresser V/dataCorrect1﹕ 24

05-15 02:05:50.619  13125-13125/com.example.tomus.weatherdresser V/dataCorrect1﹕ 18

05-15 02:05:50.619  13125-13125/com.example.tomus.weatherdresser V/dataCorrect1﹕ 15

05-15 02:05:50.619  13125-13125/com.example.tomus.weatherdresser V/dataCorrect2﹕ 11

05-15 02:05:50.619  13125-13125/com.example.tomus.weatherdresser V/dataCorrect2﹕ 11

05-15 02:05:50.619  13125-13125/com.example.tomus.weatherdresser V/dataCorrect2﹕ 15

05-15 02:05:50.619  13125-13125/com.example.tomus.weatherdresser V/dataCorrect2﹕ 16

05-15 02:05:50.619  13125-13125/com.example.tomus.weatherdresser V/dataCorrect2﹕ 17

05-15 02:05:50.629  13125-13125/com.example.tomus.weatherdresser V/dataCorrect2﹕ 17

05-15 02:05:50.629  13125-13125/com.example.tomus.weatherdresser V/dataCorrect2﹕ 14

05-15 02:05:50.629  13125-13125/com.example.tomus.weatherdresser V/dataCorrect2﹕ 11

05-15 02:05:50.629  13125-13125/com.example.tomus.weatherdresser V/dataBroken0﹕ 11

05-15 02:05:50.629  13125-13125/com.example.tomus.weatherdresser V/dataBroken0﹕ 7

05-15 02:05:50.629  13125-13125/com.example.tomus.weatherdresser V/dataBroken0﹕ 11

05-15 02:05:50.629  13125-13125/com.example.tomus.weatherdresser V/dataBroken0﹕ 20

05-15 02:05:50.629  13125-13125/com.example.tomus.weatherdresser V/dataBroken0﹕ 15

05-15 02:05:50.629  13125-13125/com.example.tomus.weatherdresser V/dataBroken0﹕ 24

05-15 02:05:50.629  13125-13125/com.example.tomus.weatherdresser V/dataBroken0﹕ 16

05-15 02:05:50.629  13125-13125/com.example.tomus.weatherdresser V/dataBroken0﹕ 15

05-15 02:05:50.629  13125-13125/com.example.tomus.weatherdresser V/dataBroken1﹕ 17

05-15 02:05:50.629  13125-13125/com.example.tomus.weatherdresser V/dataBroken1﹕ 0

05-15 02:05:50.629  13125-13125/com.example.tomus.weatherdresser V/dataBroken1﹕ 17

05-15 02:05:50.629  13125-13125/com.example.tomus.weatherdresser V/dataBroken1﹕ 0

05-15 02:05:50.629  13125-13125/com.example.tomus.weatherdresser V/dataBroken1﹕ 14

05-15 02:05:50.629  13125-13125/com.example.tomus.weatherdresser V/dataBroken1﹕ 0

05-15 02:05:50.629  13125-13125/com.example.tomus.weatherdresser V/dataBroken1﹕ 11

05-15 02:05:50.629  13125-13125/com.example.tomus.weatherdresser V/dataBroken1﹕ 0

05-15 02:05:50.629  13125-13125/com.example.tomus.weatherdresser V/dataBroken2﹕ 0

05-15 02:05:50.629  13125-13125/com.example.tomus.weatherdresser V/dataBroken2﹕ 0

05-15 02:05:50.629  13125-13125/com.example.tomus.weatherdresser V/dataBroken2﹕ 0

05-15 02:05:50.629  13125-13125/com.example.tomus.weatherdresser V/dataBroken2﹕ 0

05-15 02:05:50.629  13125-13125/com.example.tomus.weatherdresser V/dataBroken2﹕ 0

05-15 02:05:50.629  13125-13125/com.example.tomus.weatherdresser V/dataBroken2﹕ 0

05-15 02:05:50.629  13125-13125/com.example.tomus.weatherdresser V/dataBroken2﹕ 0

05-15 02:05:50.629  13125-13125/com.example.tomus.weatherdresser V/dataBroken2﹕ 0

最佳答案

您需要更改数组声明以及方式 您正在访问它。而不是在期间使用 * 或乘法 初始化,只是一个多维数组并使用 [][] 代替 对数组进行算术运算。

 int [][]data = new int[numberOfDays][8];
 String url = "http://api.worldweatheronline.com/free/v2/weather.ashx?q=" + city + "&format=json&num_of_days=" + numberOfDays + "&cc=no&fx24=no&show_comments=no&tp=3&key=e74975c820b1f6506bd6b9fdea5a5";
 JSONObject dataZNetu;
 JSONArray dataArray;
 JSONObject dataHourly;
 JSONArray dataHourlyArray;
    try
    {
      WebContext webContext = new WebContext();
      dataZNetu = webContext.DownloadJSON(url).getJSONObject("data");
      dataArray = dataZNetu.getJSONArray("weather");
      for(int i = 0; i<dataArray.length(); i++)
      {
        dataHourly = dataArray.getJSONObject(i);
        dataHourlyArray = dataHourly.getJSONArray("hourly");
        for(int j = 0; j<dataHourlyArray.length(); j++){
          data[i][j] = dataHourlyArray.getJSONObject(j).getInt("FeelsLikeC");
          //temperatures[i*j] = new DataPoint(i*j,data[i*j]);
          Log.v("dataCorrect" + i,String.valueOf(data[i][j])); //Correct values
        }
      }
    }
    catch (Exception e)
    {
      e.printStackTrace();
    }
    for(int i = 0;i<data.length;i++)
    {
      Log.v("dataBroken" + i/8,String.valueOf(data[i])); //Broken values
    }

关于java - 在 Android-Java 中读取数组时出现奇怪的行为,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/30249533/

相关文章:

java - 如何将时间舍入到最近的一天?

java - 使用 gradle 外包 java 包以便在其他项目中重用

Android ListView适配器 "filled"回调

Android AppWidget 木材不记录

java - 自定义标签库导致 "PWC6033: Unable to compile class for JSP"

java - FacesContext : separate error and success messages

android - 改造和授权 cookies

java - 使用 git 时 Android studio 无法将 AndroidManifest.xml 识别为项目的一部分

android - 在 Android Studio 中,ndk-build 下的 CMAKE_VERBOSE_MAKEFILE 是什么?

android - 在 ubuntu 上安装 android studio