You are here:

C++/Array of Objects

Advertisement


Question
Hi Vijayan,
From your profile I kind of guessed that you may probably know Java as well! so I have a Java question. you have also mentioned web programming, do you mean PHP?
Let me first tell you that I'm new to Java  :)

I'm trying to make an array of objects with 3 constructors
what would I put in each constructor?
MyArray ()
MyArray (int initCapacity)
MyArray (Object[] a)
also what would my data members be?
I need to implement the following methods:
public int size ()
     public int capacity ()
     public Object elementAt (int index)
     public void setElementAt (Object element, int index)
     public void removeElementAt (int index)
     public void addElement (Object element)
     private void increaseCapacity ()
     public boolean contains (Object element)
     public int indexOf (Object element)
     public void trimToSize ()
     public boolean isEmpty ()

Thanks for your help.

Answer
> I kind of guessed that you may probably know Java as well!
i am not a java programmer; i do not program (i have never programmed) in java.
but i do have some rudimentary knowlege of the language.
you would get better value if you pose your java questions to experts in java.
if you had missed the link, it is here: http://www.allexperts.com/el/Java/

> you have also mentioned web programming, do you mean PHP?
i had mentioned 'i would not answer questions about gui and web programming.'
i do not know anything about things like PHP.
and gui programming not an area of interest for me.

having got that out of the way, let me try yo answer your question.

from the methods required to be implemented in your class (increaseCapacity, trimToSize), MyArray seems to be a wrapper around an array of java Objects which can be resized efficiently (witout always requiring creation of a new array and a deep copy into it). this is similar to the java java.util.ArrayList and the C++ std::vector.

in addition to the array of Objects, your class would therefore need to keep track of
a. the (logical) size of the array - how many elements od the array are currently in use.
b. the capacity of the array - how much is the total space available in the array (the maximum value to which size can increase without requiring creation of a new array). the unused elements are null to start with.

see: http://java.sun.com/j2se/1.4.2/docs/api/java/util/ArrayList.html
http://www.cplusplus.com/reference/stl/vector/

so this is what MyArray would look like (to start with).

public final class MyArray
{
       // default constructor creates an empty array.
       // both size and capacity are zero
       public MyArray()
       {
         size = 0 ;
         capacity = 0 ;
         objects = null ;
       }

       // this constructor creates an empty array.
       // to which we can add upto initCapacity elements
       // without requiring creation of a new array
       public MyArray( int initCapacity )
       {
         size = 0 ;
         capacity = initCapacity ;
         objects = new Object[initCapacity] ;
       }

   
       // this constructor creates an array which is a clone of array a.
       // both size and capacity are the same as that of array a.
       public MyArray( Object[] a )
       {
         size = a.length ;
         capacity = size ;
         // create a deep copy of the array
         objects = (Object[]) a.clone() ;
       }
   
       private int size ; // number of elements in use
   
       private int capacity ; // size to which the array can grow
         // without requiring relocating the array in memory
   
       private Object[] objects ; // the array containing objects
         // the currently used portion of the array conatains size elements
         // the array has capacity elements; elements not in use are null
   
       // other methods
       // ...
}  

C++

All Answers


Answers by Expert:


Ask Experts

Volunteer


vijayan

Expertise

my primary areas of interest are generic and template metaprogramming, STL, algorithms, design patterns and c++11. i would not answer questions about gui and web programming.

Experience

about 15 years or so

Education/Credentials
post graduate engineer

©2016 About.com. All rights reserved.