CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Results 1 to 2 of 2
  1. #1
    Join Date
    May 2011
    Location
    Finland
    Posts
    12

    Dynamic number of objects

    Hi,
    I have a program which accepts inputs such as latitude, longitude, towerHeight, signalPower, and frequency. I need my program to do this task.
    - each time a user clicks an "Add" button, an object is created with the above field values. And I want the user to click/create objects/ as many as he want.
    This is the constructor for the objects.
    public OwnTxData(int latitude,int longitude,int freq, double power,double msHeight){
    this.latitude = latitude;
    this.longitude = longitude;
    this.frequency = freq;
    this.power = power;
    this.msHeight = msHeight;

    }
    How can I allow a dynamic array of objects to be created? How shall I refer to each of the objects later? I am new to java, please help for any ideas.
    Thanks in advance

  2. #2
    dlorde is offline Elite Member Power Poster
    Join Date
    Aug 1999
    Location
    UK
    Posts
    10,163

    Re: Dynamic number of objects

    For a dynamic array, you can use an ArrayList. Create it something like this:
    Code:
    List<OwnTxData> dataList = new ArrayList<OwnTxData>();
    This creates a new ArrayList to hold OwnTxData objects and assigns it to a List variable for OwnTxData objects called dataList (you can call it what you like).

    To create a new OwnTxData object and insert it into the List:
    Code:
    // create data object
    OwnTxData dataObject = new OwnTxData(latitude, longitude, freq, power, msHeight);
    
    // add to end of list
    dataList.add(dataObject);
    
    ...
    
    // to get the first object out of the list:
    OwnTxData dataObject = dataList.get(0);
    It is not knowledge, but the act of learning, not possession, but the act of getting there which generates the greatest satisfaction...
    F. Gauss
    Please use &#91;CODE]...your code here...&#91;/CODE] tags when posting code. If you get an error, please post the full error message and stack trace, if present.

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  





Click Here to Expand Forum to Full Width

Featured