Welcome to the Treehouse Community

Want to collaborate on code errors? Have bugs you need feedback on? Looking for an extra set of eyes on your latest project? Get support with fellow developers, designers, and programmers of all backgrounds and skill levels here with the Treehouse Community! While you're at it, check out some resources Treehouse students have shared here.

Looking to learn something new?

Treehouse offers a seven day free trial for new students. Get access to thousands of hours of content and join thousands of Treehouse students and alumni in the community today.

Start your free trial

Android Android Lists and Adapters (2015) Updating the Data Model From JSONArray to a Java Array

jenyufu
jenyufu
3,311 Points

A quick question about source code ordering

I was wondering if I can put private Day[] getDailyForecast behind private Hour[] getHourlyForecast. Basically switch the code order they are listed in so instead of like this:

    private Forecast parseForecastDetails(String jsonData) throws JSONException  {
        Forecast forecast = new Forecast();

        forecast.setCurrent(getCurrentDetails(jsonData));
        forecast.setHourlyForecast(getHourlyForecast(jsonData));
        forecast.setDailyForecast(getDailyForecast(jsonData));
        return forecast;
    }

    private Day[] getDailyForecast(String jsonData) throws JSONException{ //1.
        JSONObject forecast = new JSONObject(jsonData);
        String timezone = forecast.getString("timezone");


    }

    private Hour[] getHourlyForecast(String jsonData) throws JSONException{ //2.
        JSONObject forecast = new JSONObject(jsonData);
        String timezone = forecast.getString("timezone");
}

Do it like this:

    private Forecast parseForecastDetails(String jsonData) throws JSONException  {
        Forecast forecast = new Forecast();

        forecast.setCurrent(getCurrentDetails(jsonData));
        forecast.setHourlyForecast(getHourlyForecast(jsonData));
        forecast.setDailyForecast(getDailyForecast(jsonData));
        return forecast;
    }

    private Hour[] getHourlyForecast(String jsonData) throws JSONException{  //2.
        JSONObject forecast = new JSONObject(jsonData);
        String timezone = forecast.getString("timezone");
}

    private Day[] getDailyForecast(String jsonData) throws JSONException{  //1.
        JSONObject forecast = new JSONObject(jsonData);
        String timezone = forecast.getString("timezone");


    }

Would the code still work?

The reason I ask is because here:

        forecast.setHourlyForecast(getHourlyForecast(jsonData));
        forecast.setDailyForecast(getDailyForecast(jsonData));

Hourly is before Daily but in the arrays below that listed, Ben put Hour[] after Day[]

1 Answer

Jon Kussmann
PLUS
Jon Kussmann
Courses Plus Student 7,254 Points

Hi Jenyufu,

The order for the two methods do not matter. When you call:

 forecast.setHourlyForecast(getHourlyForecast(jsonData));
 forecast.setDailyForecast(getDailyForecast(jsonData));

Your program will first run "setHourlyForecast" because that is listed first... it will "search" for that method in your class. So, how it is position in relation to other methods does not matter in terms of order. Afterwards, "setDailyForecast" will run and your program will "search" for that method throughout your class. When you call methods, the order may matter but not when you "declare" them.

I hope this helps, if not let me know how I can explain better.