0

What is the need of creating the list when we have already created the ArrayList to store the JSONArray? This code is to access the data from the internet which is JSON format, using the volley library. I am having a little problem in understanding the use of ArrayLists along with List. Couldn't we just exclude the List?

//What is the use of the List?

ArrayList<Question> questionArrayList= new ArrayList<>();
public List<Question> getQuestions (){
    JsonArrayRequest jsonArrayRequest =new JsonArrayRequest(Request.Method.GET, url, null,
            new Response.Listener<JSONArray>() {
                @Override
                public void onResponse(JSONArray response) {
                    for(int i=0;i<response.length();i++){
                        Question question = new Question();
                        try {
                            question.setQuestionId(response.getJSONArray(i).getString(0));
                            question.setTorF(response.getJSONArray(i).getBoolean(0));
                            questionArrayList.add(question);//If we had to add the question entries to the questionArrayList then why create a list(getQuestions)?

                        } catch (JSONException e) {
                            e.printStackTrace();
                        }
                    }
                }
            },
            new Response.ErrorListener() {
                @Override
                public void onErrorResponse(VolleyError error) {

                }
            };

//My Question class is this:-

package com.example.trivia.model;

public class Question {

private String QuestionId;
private boolean IsTorF;

public Question(String questionId, boolean isTorF) {
    this.QuestionId = questionId;
    this.IsTorF = isTorF;
}
public Question(){
}
public String getQuestionId() {
    return QuestionId;
}

public void setQuestionId(String questionId) {
    QuestionId = questionId;
}

public boolean isTorF() {
    return IsTorF;
}

public void setTorF(boolean torF) {
    IsTorF = torF;
}

}

Arpit Anand
  • 347
  • 2
  • 17

1 Answers1

1

You don't have to, its your choice.

UPDATE:

Based on being given the return type of the method ArrayList<Question> this indicates encapsulation. This is very common for creating API's or Repository classes. With the current implementation, let's assume your calling class is expecting to use the data as an ArrayList. No surprise, the return type was of an ArrayList. Now, later in the project we need the data is a LinkedList. You can certainly add code to make that work OR you can implement it as it was, decoupling the implementation of List.

OLD ANSWER:

You should be creating an immutable List for your response to ensure you always have the original response unmodified. In this example the response is getting set to an immutable List as well adding each item out to a mutable list so you can modify the contents. This also reduces another step of iterating over the list later and copying the values since Java is pass-by-reference

DevinM
  • 1,112
  • 1
  • 12
  • 29
  • **getQuestions()** is also the only method in the class. So, is it possible that the dev only used this in place of **onCreate()** method? – Arpit Anand Sep 03 '20 at 16:18
  • Its hard to say without seeing the entire class or the return type of getQuestions() method. From what i can see its a method within a class of unknown length and the return value is completely unknown. Nevertheless, assuming I had this within a controller, I would be using the ArrayList as described above. If it is as you describe it, there is no reason for the ArrayList as its not public and serves no purpose. – DevinM Sep 03 '20 at 18:09
  • Yes, you are right. The JSONArray is instantiated by a controller and it does have a return type: `return questionArrayList;` .How does having a controller change anything? Why would you be using ArrayList with controllers? Can you please help me on this? – Arpit Anand Sep 03 '20 at 18:25
  • okay that makes more sense. The return type being the ArrayList is to decouple your code from a specific implementation of the interface List. This allows the variable from the calling class to use it as it sees fit. i.e. list = new LinkedList or list = new ArrayList (which is happening here). see this post for more details https://stackoverflow.com/questions/147468/why-should-the-interface-for-a-java-class-be-preferred – DevinM Sep 03 '20 at 19:23