using a vector instead of array


DevX Home    Today's Headlines   Articles Archive   Tip Bank   Forums   

Results 1 to 3 of 3

Thread: using a vector instead of array

  1. #1
    Join Date
    Feb 2005
    Posts
    22

    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.

  2. #2
    Join Date
    Nov 2004
    Location
    Norway
    Posts
    1,560
    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

  3. #3
    Join Date
    Feb 2005
    Posts
    22
    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
  •  
HTML5 Development Center
 
 
FAQ
Latest Articles
Java
.NET
XML
Database
Enterprise
Questions? Contact us.
C++
Web Development
Wireless
Latest Tips
Open Source


   Development Centers

   -- Android Development Center
   -- Cloud Development Project Center
   -- HTML5 Development Center
   -- Windows Mobile Development Center