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

时间:2015-05-15 00:14:44

标签: 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

1 个答案:

答案 0 :(得分:1)

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

 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
    }