Strange behaviour while reading array in Android-Java -
i'm trying array of int values json, strange happening , different output when read same array on 2 places in code.
does know reason behind behaviour?
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 = 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 = 0;i<data.length;i++){ log.v("databroken" + i/8,string.valueof(data[i])); //broken values } graph = new linegraphseries<datapoint>(temperatures); return graph; }
log:
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
you need change array declaration way accessing it. instead of using * or multiplication during initialization, multi-dimension array , use [][] instead of performing arithmetic operation on array.
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 = 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 = 0;i<data.length;i++) { log.v("databroken" + i/8,string.valueof(data[i])); //broken values }
Comments
Post a Comment