-
June 1st, 2011, 05:50 AM
#1
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
-
June 1st, 2011, 06:47 AM
#2
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 [CODE]...your code here...[/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
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|