为什么在我们已经创建了ArrayList来存储JSONArray时还需要创建列表呢?

huangapple go评论58阅读模式
英文:

What is need of creating the list when we have already created the ArrayList to store the JSONArray?

问题

这个代码是用来通过Volley库从互联网访问JSON格式的数据。我对ArrayList与List一起使用的用途有一些理解问题。我们是否可以只使用ArrayList而不使用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);  //如果我们只需要将问题条目添加到questionArrayList中,为什么还要创建一个List(getQuestions)?

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

                    }
                };

我的Question类如下:

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;
    }
}
英文:

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&lt;Question&gt; questionArrayList= new ArrayList&lt;&gt;();
public List&lt;Question&gt; getQuestions (){
    JsonArrayRequest jsonArrayRequest =new JsonArrayRequest(Request.Method.GET, url, null,
            new Response.Listener&lt;JSONArray&gt;() {
                @Override
                public void onResponse(JSONArray response) {
                    for(int i=0;i&lt;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;
}

}

答案1

得分: 1

你不必这样做,这是你的选择。

更新:

根据方法 ArrayList&lt;Question&gt; 的返回类型,这表明了封装性。在创建 API 或存储库类时,这是非常常见的。根据当前的实现,假设你的调用类希望将数据用作 ArrayList。毫不奇怪,返回类型是 ArrayList。现在,在项目后期,我们需要将数据作为 LinkedList。你当然可以添加代码来实现这一点,或者你可以按原样实现,解耦 List 的实现。

旧回答:

应该为响应创建一个不可变的 List,以确保始终保留原始响应的未修改副本。在这个示例中,响应也被设置为一个不可变的 List,同时将每个项添加到可变的 List 中,以便你可以修改内容。这还减少了以后迭代列表并复制值的另一步,因为 Java 是按引用传递的。

英文:

You don't have to, its your choice.

UPDATE:

Based on being given the return type of the method ArrayList&lt;Question&gt; 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

huangapple
  • 本文由 发表于 2020年9月3日 20:40:36
  • 转载请务必保留本文链接:https://go.coder-hub.com/63723918.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定