如何在java中将对象添加到ArrayList中 [英] How to add an object into ArrayList in java

查看:43
本文介绍了如何在java中将对象添加到ArrayList中的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

这里有两个类,查询语句和stackov.一个arraylist 用于存储查询语句类的对象.但最近添加的对象覆盖了前一个.如何添加对象以使它们不被覆盖?

Here are two classes, query sentence and stackov. An arraylist is used to store objects of query sentence class.But the recently added object is overriding the previous one.How do I add objects so that they are not overridden?

QuerySentence.java

public class QuerySentence {

    public static String query;
    public static String label;
    public QuerySentence(){

    }
    public QuerySentence(String query,String label){
        this.query = query;
        this.label = label;
    }
}  

Stackov.java

package QueryClassifier;

import java.util.ArrayList;

public class stackov {

    public static void main(String args[])
    {
        QuerySentence qs1 = new QuerySentence("What state produces the best lobster to eat","LOCATION");
        QuerySentence qs2 = new QuerySentence("What is Dick Clark's birthday","DATE");
        ArrayList<Object> doclist = new ArrayList<Object>();

            doclist.add(0,qs1);
            doclist.add(1,qs2);

            int size = doclist.size();
               while(size>0)
               {
                    QuerySentence qs3 = (QuerySentence) doclist.get(size-1);
                    System.out.println("\nin loop : " + qs3.label + qs3.query);
                    size--;
                }

    }
}   

推荐答案

问题不是来自您的循环,而是来自您的类 QuerySentence.您正在创建静态对象,这意味着您不会为类的不同实例创建不同的字段,但该类将只有它们的唯一副本.那么这里发生了什么,您将这些值分配给您的类 QuerySentence :

The problem doesn't come from your loop but from your class QuerySentence. You're creating static objects, which means that you don't create different fields for different instances of your classes, but the class will have only a unique copy of them. So what was happening here, you were assigning these value to your class QuerySentence :

QuerySentence qs1 = new QuerySentence("What state produces the best lobster to eat","LOCATION");

然后用这个擦掉它:

QuerySentence qs2 = new QuerySentence("What is Dick Clark's birthday","DATE");

当您将两个对象添加到 ArrayList 中时,当然在您的循环中您将打印两个结果.但只有qs2中的结果.从您的字段声明中删除 static ,它会正常工作:

As you add two objects into your ArrayList, of course in your loop you will print two results. But only the results in qs2. Remove static from your fields declarations and it will work fine :

public String query;
public String label;

您无需在添加时索引查询语句.

You don't need to index your query sentences while adding.

doclist.add(qs1);
doclist.add(qs2);

您还可以改进循环.你可以这样做:

And you can also improve your loop. You could just do :

ArrayList<QuerySentence> doclist = new ArrayList<QuerySentence>();

// some code...

for(QuerySentence q : doclist)
{
    System.out.println("In loop : " + q.label + q.query);
}

还请记住 Java 约定:您的类应以大写字母开头 (Stackov).如果您还从事封装工作,最好声明您的字段 private 并为它们创建 gettersetter.有了这个,你的 for-statement 应该是这样的:

Please also remember Java conventions : your classes should begin with an uppercase (Stackov). If you also work on encapsulation, it's better to declare your fields private and create getters and setters for them. With this your for-statement should look like this :

for(QuerySentence q : doclist)
{
    System.out.println("In loop : " + q.getLabel() + q.getQuery());
}

这篇关于如何在java中将对象添加到ArrayList中的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆