   random javac errors
  1. In my Assignment class I declare a generic ArrayList of Question objects. I do not actually allocate memory for them until the constructor. Is this a correct/valid method:class Assignment{ArrayList<Question> questions;...Assignment(){questions = new ArrayList<Question>();...}}2. When all Assignments are first created, they are implemented with a parameter-less constructor Assignment(), which, initializes all the data members to zero, null, or -1. Throughout interaction with the user, the Assignment members are accessed and manipulated. One of these members is the ArrayList of Question objects (from Question #1 above).How could I write the class definition such that the Assignment() constructor simply allocates space for the ArrayList, but leaves the ArrayList empty, and then subsequent access to class methods add Question indices/slots into the ArrayList?My best attempt:class Assignment{ArrayList<Question> list;...Assignment(){list = new ArrayList<Question>();...}void addQuestion(Question q){list.add(q);}void removeQuestion(int index){list.remove(index);}}...// In the JApplet codeAssignment proj = new Assignment();Question x = new Question();proj.addQuestion(x);proj.removeQuestion(0);...But I am worried that somehow it is not quite this simple.3. The following code produces an "incovertible types" error:LinkedList makeNumber(double argument, Widget f){LinkedList<AtomicExpression> temp = new LinkedList();temp = listBody;ListIterator iter = temp.listIterator();AtomicExpression stepNode = new AtomicExpression();while(iter.hasNext()){stepNode = iter.next();if(stepNode instanceof Variable){Number n = new Number(argument);iter.set(n);}}temp = f.simplify(temp);return temp;}The line "stepNode = iter.next()" (Line 9) is causing the error, and I don't understand why. If stepNode is an AtomicExpression, and iter is the list iterator to a LinkedList of AtomicExpressions, then why are stepNode and the next node from the list "inconvertible?"Also, will the set() method make the iterator jump yet another node? In this function, I am trying to replace any instances of Variables with newly-allocated Numbers; in effect, trying to "swap them out" of the expression and replace them with numeric constants. Have I implemented this correctly?Also in the function header I specify "LinkedList" as the return value's data type. Should I make this "LinkedList<AtomicExpression>" or does it not make a difference?4. Line 13 from Question #3 above (iter.set(n)) is getting the following warning: unchecked call to set(E) as a member of the raw type java.util.ListIterator. If Number 'n' is an extension of an AtomicExpression, and iter is the list iterator for a LinkedList of such AtomicExpressions, then why am I getting this warning?5. I am getting a bizarre warning in my class header for the JApplet class. Here is my header:public class PragmaMath extends JApplet implements ActionListener, KeyListener, ItemListener, ComponentListenerAnd here is the warning I am getting:serial [serializable] class PragmaMath has no definition of serialVersionUIDIt seems as though ItemListener is the problematic component here...   
Quote:Original post by hisDudeness1. In my Assignment class I declare a generic ArrayList of Question objects. I do not actually allocate memory for them until the constructor. Is this a correct/valid method:Yes, it's fine.Quote:2. How could I write the class definition such that the Assignment() constructor simply allocates space for the ArrayList, but leaves the ArrayList empty, and then subsequent access to class methods add Question indices/slots into the ArrayList?You don't add 'slots' or 'indices' to an ArrayList. That would entail resizing the array each time you add an object, which is inefficient. What you are doing now is fine. In the constructor, you are allocating a new, empty ArrayList.  It has space for a default number of objects (16, IIRC, but you can look in the javadocs to verify). Your addQuestion method will add Question objects to the ArrayList without resizing it until it is full. Once it is full, the ArrayList will grow by by 2 * default capacity. Then you can continue to add objects to it. To change the default capacity, pass the number you want to the constructor.Quote:3. The following code produces an "incovertible types" error:The line "stepNode = iter.next()" (Line 9) is causing the error, and I don't understand why. If stepNode is an AtomicExpression, and iter is the list iterator to a LinkedList of AtomicExpressions, then why are stepNode and the next node from the list "inconvertible?"You are using the Java Generics mechanism for the list, but your iterator is a normal one that isn't declared using the Generics syntax. Change your declaration of the ListIterator to the following:ListIterator<AtomicExpression> itr = listBody.listIterator();I am assuming that listBody is your original list. There's two things that I would like to point out. First, the following is redundant:LinkedList<AtomicExpression> temp = new LinkedList<AtomicExpression>;temp = listBody;First, you are allocating a new object. Then, you are taking the reference to the new object and pointing it at a different object. I guess you are trying to copy the list? If so, that is not what you are doing. Think of it this way: you have two distinct object - 'listBody' points at one and 'temp' points at the other. The line 'temp = listBody' redirects 'temp' so that it is now pointing at the same object as 'listBody', and nothing is pointing at the other object anymore. It's a needless allocation that wastes space.Second, why are you creating a temporary list object anyway? Just get the ListIterator from the original list. That's effectively what you are doing now anyway. If you were wanting to copy the list to avoid modifying the original by mistake, look into the clone method. For LinkedList objects, clone returns a shallow copy (it clones the List, but not the objects it contains). Though generally this is something you don't need to do.Quote:Also, will the set() method make the iterator jump yet another node? In this function, I am trying to replace any instances of Variables with newly-allocated Numbers; in effect, trying to "swap them out" of the expression and replace them with numeric constants. Have I implemented this correctly?The API Javadocs are your friends:Quote:From the LinkedList.set documentationReplaces the last element returned by next or previous with the specified element (optional operation). This call can be made only if neither ListIterator.remove nor ListIterator.add have been called after the last call to next or previous.So no, it does not advance to the next node. It replaces the last element that was returned by next() or previous() as long as you have not added or removed anything.Quote:Also in the function header I specify "LinkedList" as the return value's data type. Should I make this "LinkedList<AtomicExpression>" or does it not make a difference?As long as you are using Generics, you should use it completely. So yes, the return value should be LinkedList<AtomicExpression>. Unless your class is declared to accept any type, in which case it would be LinkedList<E>.Quote:4. Line 13 from Question #3 above (iter.set(n)) is getting the following warning: unchecked call to set(E) as a member of the raw type java.util.ListIterator. If Number 'n' is an extension of an AtomicExpression, and iter is the list iterator for a LinkedList of such AtomicExpressions, then why am I getting this warning?See above about the declaration of your ListIterator.Quote:5. I am getting a bizarre warning in my class header for the JApplet class. Here is my header:public class PragmaMath extends JApplet implements ActionListener, KeyListener, ItemListener, ComponentListenerAnd here is the warning I am getting:serial [serializable] class PragmaMath has no definition of serialVersionUIDIt seems as though ItemListener is the problematic component here...This is not a bizarre warning. It's what happens when one of your classes extends another class that implements Serializable, or implements an interface that extends Serializable. To shut it up, you can generate a serialVersionUID for your class, implement one manaually, or, if possible (such as with Eclipse) turn off the warnings. To implement it manually is easiest. Just add this declaration to your class:private static final long serialVersionUID = 1L;The idea is that if you use Serialization, you need to distinguish between different versions of a class. Anytime you add/remove fields to/from the class, the version should be changed so that the default serialization mechanism can load serialized objects properly. You are getting the warning because your class indirectly implements Serializable.   