【问题标题】:What is need of creating the list when we have already created the ArrayList to store the JSONArray?当我们已经创建了 ArrayList 来存储 JSONArray 时,还需要创建列表吗?
【发布时间】:2020-12-22 16:59:50
【问题描述】:

当我们已经创建了ArrayList来存储JSONArray时,还需要创建列表吗? 此代码是使用 volley 库从 Internet 访问 JSON 格式的数据。我在理解 ArrayLists 和 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) {

                }
            };

//我的问题类是这样的:-

包 com.example.trivia.model;

公开课问题{

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;
}

}

【问题讨论】:

  • getQuestions 方法返回什么?我们看不到任何return 声明,因此无法理解开发人员为什么这样做
  • 它返回questionArrayList 抱歉,不知怎的被省略了。

标签: java android list arraylist android-volley


【解决方案1】:

你不必,这是你的选择。

更新:

基于给定方法ArrayList&lt;Question&gt;的返回类型,这表示封装。这对于创建 API 或存储库类非常常见。对于当前的实现,假设您的调用类期望将数据用作 ArrayList。毫不奇怪,返回类型是 ArrayList。现在,在项目的后期,我们需要的数据是一个 LinkedList。您当然可以添加代码来完成这项工作,或者您可以按原样实现它,将 List 的实现解耦。

旧答案:

应该为您的回复创建一个不可变的列表,以确保您始终拥有未修改的原始回复。在此示例中,响应设置为不可变列表,并将每个项目添加到可变列表中,以便您可以修改内容。这也减少了稍后迭代列表和复制值的另一个步骤,因为 Java 是按引用传递的

【讨论】:

  • getQuestions() 也是类中唯一的方法。那么,开发者是否有可能只使用 this 代替 onCreate() 方法?
  • 如果没有看到整个类或 getQuestions() 方法的返回类型,很难说。从我可以看到它是一类未知长度的方法,返回值是完全未知的。不过,假设我在控制器中有这个,我将使用如上所述的 ArrayList。如果如您所描述的那样,则 ArrayList 没有理由不公开且没有任何用途。
  • 是的,你是对的。 JSONArray 由控制器实例化,它确实有一个返回类型:return questionArrayList;。控制器如何改变任何东西?为什么要将 ArrayList 与控制器一起使用?你能帮我解决这个问题吗?
  • 好吧,这更有意义。返回类型为 ArrayList 是为了将您的代码与接口 List 的特定实现分离。这允许调用类中的变量以它认为合适的方式使用它。即 list = new LinkedList 或 list = new ArrayList (发生在这里)。有关更多详细信息,请参阅此帖子stackoverflow.com/questions/147468/…
猜你喜欢
  • 1970-01-01
  • 2017-02-20
  • 1970-01-01
  • 2021-12-04
  • 1970-01-01
  • 2020-03-25
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多