4

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

    }
}   
1
  • try to indent your code correctly. Also there is no need to post two entire classes, just post the ArrayList manipulation. Commented Mar 12, 2013 at 9:00

4 Answers 4

7

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");

And then erased it whit this one :

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

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

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());
}
Sign up to request clarification or add additional context in comments.

2 Comments

The for loop showing an error "for(QuerySentence q : doclist)" as "Type mismatch: cannot convert from element type Object to QuerySentence"
That's because you've declared your ArrayList as ArrayList<Object> doclist = new ArrayList<Object>();. Use ArrayList<QuerySentence> doclist = new ArrayList<QuerySentence>(); instead.
1
doclist.add(qs1);
doclist.add(qs2);

Comments

0

index is not required add like this

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

Comments

0
       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<QuerySentence> doclist = new ArrayList<QuerySentence>();

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

                for(QuerySentence sent: docList)
                {
                   System.out.println("\nin loop : " + sent.label + sent.query);
                }

        }

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.