|
|
Welcome to the Java Forums.
You are currently viewing our boards as a guest which gives you limited access to view most discussions and access our other features. By joining our free community, you will:
- have access to post topics
- communicate privately with other members (PM)
- not see advertisements between posts
- have the possibility to earn one of our surprises if you are an active member
- access many other special features that will be introduced later.
Registration is fast, simple and absolutely free so please, join our community today!
If you have any problems with the registration process or your account login, please contact us.
|
|

06-25-2008, 10:25 PM
|
|
Member
|
|
Join Date: Jun 2008
Posts: 11
|
|
|
Collecton of Variables
Hi,
I'm making a class that will hold information about various parameters variables (id, size, name, ...).
The problem is that I have many such parameters, so when I am setting them (the values are coming from a file), I would like to be able to loop through each parameter, since the files always give the parameters in the correct order.
So inside the constructor of the class I would like to be able to loop through all the parameters like this:
for (parameter : value)
value = inputFromFile;
The problem is that the parameters can be of different types (int, string,...).
Does anyone know of any construct that will allow me to loop through a collection of variables in order?
I would like to access the information from other classes like this:
classInstance.params.id
(Where params is instance of the construct that allows me to do this).
If anyone has any variations on how to do this, that would be great.
Thanks a lot.
|
|

06-26-2008, 05:45 AM
|
 |
Moderator
|
|
Join Date: Jul 2007
Location: Colombo, Sri Lanka
Posts: 2,959
|
|
|
You saying that data in the file is not in a specific order in data type?
__________________
Use an appropriate Subject. "Help, urgent!" isn't one. To view links or images in signatures your post count must be 10 or greater. You currently have 0 posts.
Has someone helped you? Then you can To view links or images in signatures your post count must be 10 or greater. You currently have 0 posts. their helpful post.
Want to make your IDE the best? To view links or images in signatures your post count must be 10 or greater. You currently have 0 posts.
To view links or images in signatures your post count must be 10 or greater. You currently have 0 posts. To view links or images in signatures your post count must be 10 or greater. You currently have 0 posts. (Close on September 4, 2008)
To view links or images in signatures your post count must be 10 or greater. You currently have 0 posts.
|
|

06-26-2008, 05:55 AM
|
|
Senior Member
|
|
Join Date: Jun 2008
Posts: 178
|
|
|
1) Can you give an example of what the file will look like? Just a file with say 4 parameters and say 3 or 4 collections of said parameters.
2) Is this a text file or is it a file of serialized objects?
|
|

06-26-2008, 04:29 PM
|
|
Member
|
|
Join Date: Jun 2008
Posts: 11
|
|
Hi, thanks for your replies.
The file will look like this:
<Hit>
<Hit_num>1</Hit_num>
<Hit_id>gi|83195617|dbj|DB346777.1|</Hit_id>
<Hit_accession>DB346777</Hit_accession>
<Hit_len>535</Hit_len>
</Hit>
As you can see, the file is .xml and some of the perameters are integers(num and len) and some are strings(id, accession).
Each <hit>...</hit> tag pair represent one result. Each result has several parameters, but I have more than one result in a file, so there could be many <hit>...</hit> tag pairs.
I suppose I could just use String for each parameter and then cast the integers later, but the problem is still the same. I would like to be able to loop through all the variables in order to read then in, but when the data is being accessed I would like to be able to access a particular parameter without having to remember it was the 5th parameter. So I would like to be able to have:
classInstance.params.Hit_Id
This way after I hit the . after params, I would get a drop down box where I can select the specific variable for Hit_id.
These variables are all public, I'm thinking right now that I should create a private array of pointers, and make all these variables Strings. That way internally I can loop through the variables and then when using the interface I can use the dot notation.
Anyone have a better way?
P.S. I'm using regex to separate the data from the tags. I have already created holder strings that hold one <hit>...</hit> each. Now I need to take the parameters and put them into the correct variable.
|
|

06-26-2008, 04:57 PM
|
 |
Senior Member
|
|
Join Date: Jan 2008
Location: Cebu City, Philippines
Posts: 524
|
|
|
HashMap<String, ArrayList>
Where String is your key/node and ArrayList must be Object implemented....
You can try it....
__________________
A specific, detailed, simple, well elaborated, and "tested before asking" question may gather more quick replies. hopefully To view links or images in signatures your post count must be 10 or greater. You currently have 0 posts.
|
|

06-26-2008, 04:59 PM
|
|
Member
|
|
Join Date: Jun 2008
Posts: 11
|
|
Ok, I just realized that you can't have C++ style pointers in Java. Does anyone know if it is possible to make an array where each element references another variable. For example, if I have
and somehow, I get
to reference g, now I can do
and then
will print "dude".
Is this possible?
|
|

06-26-2008, 05:11 PM
|
 |
Senior Member
|
|
Join Date: Jan 2008
Location: Cebu City, Philippines
Posts: 524
|
|
yes you can.... but you will be amazed if you try the Java's Collection Framework
__________________
A specific, detailed, simple, well elaborated, and "tested before asking" question may gather more quick replies. hopefully To view links or images in signatures your post count must be 10 or greater. You currently have 0 posts.
|
|

06-26-2008, 05:25 PM
|
|
Member
|
|
Join Date: Jun 2008
Posts: 11
|
|
|
As far as I can tell, the problem with using a collection is that if I want to access a particular parameter, I need to remember its name. I won't get a nice drop down box letting me access it. This will be a problem since I can have up to a hundred parameters.
|
|

06-27-2008, 03:22 AM
|
|
Senior Member
|
|
Join Date: Jun 2008
Posts: 178
|
|
Originally Posted by c26354
As far as I can tell, the problem with using a collection is that if I want to access a particular parameter, I need to remember its name. I won't get a nice drop down box letting me access it. This will be a problem since I can have up to a hundred parameters.
You seem to be mixing up data structure concepts with GUI concepts. If you have a data structure, making a GUI with a pretty drop-down box to access the data is a separate issue and actually relatively easy to do (if you understand Swing), but regardless again I can't emphasize enough that it is a totally separate issue.
More important at this stage is how to get your data, or in other words how to parse your XML, and for that you have two general choices: a SAX parser (Simple API for XML) or a DOM parser (Document Object Model). If you have a lot of data and don't want to hold all of it in memory, then SAX may be what you need. If on the other hand you want to parse it all into a tree structure, then DOM will be your best bet.
|
|

06-27-2008, 08:26 AM
|
|
Member
|
|
Join Date: Jun 2008
Posts: 1
|
|
As far as I can tell, the problem with using a collection is that if I want to access a particular parameter, I need to remember its name. I won't get a nice drop down box letting me access it. This will be a problem since I can have up to a hundred parameters.
sukatoa is right...you can use Java's Collection Framework..you also can access a particular parameter without remember its name.
You can use ArrayList/LinkedList class and through accessing element you will get appropriate result.
You need some more idea about Collection Framework. 
Thanxs
|
|

06-27-2008, 05:48 PM
|
|
Member
|
|
Join Date: Jun 2008
Posts: 11
|
|
|
Thanks guys, I'll give the collection framework another look. My background is mainly C++ and PHP so i'm still getting used to this.
|
|

06-28-2008, 02:24 AM
|
 |
Senior Member
|
|
Join Date: Dec 2007
Location: South Africa
Posts: 334
|
|
|
Java "for each" loops and Serializable objects.
Hello c26354
Originally Posted by c26354
for (parameter : value)
value = inputFromFile;
This example of a "for each" loop won't work properly.  Consider this:
Vector<Dog> dogs = new Vector<Dog>();
populate(dogs);
for (Dog dog : dogs){
dog = new Dog("Pete's clone");
}
The variable dog, carries a reference to an element of the vector dogs. So, if you assign a new dog to it, the original object or dog will remain unchanged. I always use "for each" loops when dealing with "read only" situations. With Java, what you are doing here can be dangerous.
Just a warning though. About your problem. The simplest way to do this is the make your objects implement the Serializable interface. Then you can use object streams to share the information, i.e. parameters and behavior. With good usage of Object Orientated concepts, you can make this a very dynamic system. This means that you do not have to use XML and extra API to store the information.
Good luck c26354 and ask if you would like to know more about object serialization. 
__________________
If your ship has not come in yet then build a lighthouse.
|
|

06-30-2008, 04:09 PM
|
|
Member
|
|
Join Date: Jun 2008
Posts: 11
|
|
|
Hi,
Thanks for the info tim! Again, because my background is C++ I assumed you could modify dog (in your example) and then modify dogs as well, not quite used to references yet.
I'll read up about serialization and then post if I have any specific questions, thanks.
|
|
| Thread Tools |
|
|
| Display Modes |
Linear Mode
|
Posting Rules
|
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts
HTML code is Off
|
|
|
|
|