-
using a vector instead of array
I don't think I fully understand this vector this yet. I've got one class that initially uses an array and my assignment is to use a vector. The previous posts in your forums have been very helpful in explaining the vector but i've still got one part that I don't understand.
The original code, that uses the array, looks like this:
public LineItems( int size, SalesInvoice owner )
{
items = new LineItem[ size ];
nItems = size;
for ( int i = 0; i < size; i++ )
{
items[i] = new LineItem( this );
}
this.owner = owner;
}
To change the code to use a vector, I wrote it like this:
public LineItems ( SalesInvoice owner )
{
for ( int i = 0; i < items.size(); i++ )
{
items.add( new LineItem(this) );
}
this.owner = owner;
}
Further into my code I call:
public double getTotal()
{
double total = 0.0;
for ( int i = 0; i < items.size(); i++ )
{
//**getTotal method from LineItemClass
total += items.elementAt( i ).getTotal();
-------------------------^----------^
----------------------error 1----- error 2
}
return total;
}
when I compile I get two errors - error 1 is cannot resolve symbol and error 2 is inconvertiable types (found string/require double). Any advise you could send my way would be greatly appreciated.
-
The vector contains LineItem object so get one of them like:
LineItem li=(LineItem)items.elementAt(i);
Then, if a double value is one of the ListItem properties you could get it e.g. like:
double d=li.getTheDoubleValueSomwehereInThere();
eschew obfuscation
-
OK-Now this is making much more sense and I thank you. Some of Java's concepts are a little difficult to grasp from a book - your explainations help me a lot - just want to thank you for that.
Posting Permissions
- You may not post new threads
- You may not post replies
- You may not post attachments
- You may not edit your posts
Forum Rules
|
Top DevX Stories
Easy Web Services with SQL Server 2005 HTTP Endpoints
JavaOne 2005: Java Platform Roadmap Focuses on Ease of Development, Sun Focuses on the "Free" in F.O.S.S.
Wed Yourself to UML with the Power of Associations
Microsoft to Add AJAX Capabilities to ASP.NET
IBM's Cloudscape Versus MySQL
|
Bookmarks