Java Forums

Main Menu
Home
Today's Posts
FAQ
Search
Contact Us

Java Network
Java Tips
Java Tips Blog

Sponsored Links





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.

Reply
 
LinkBack Thread Tools Display Modes
  #1 (permalink)  
Old 06-25-2008, 10:25 PM
Member
 
Join Date: Jun 2008
Posts: 11
c26354 is on a distinguished road
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.
Bookmark Post in Technorati
Reply With Quote
Sponsored Links
  #2 (permalink)  
Old 06-26-2008, 05:45 AM
Eranga's Avatar
Moderator
 
Join Date: Jul 2007
Location: Colombo, Sri Lanka
Posts: 2,959
Eranga has a spectacular aura aboutEranga has a spectacular aura about
Send a message via Yahoo to Eranga
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.
Bookmark Post in Technorati
Reply With Quote
  #3 (permalink)  
Old 06-26-2008, 05:55 AM
Senior Member
 
Join Date: Jun 2008
Posts: 178
Fubarable is on a distinguished road
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?
Bookmark Post in Technorati
Reply With Quote
  #4 (permalink)  
Old 06-26-2008, 04:29 PM
Member
 
Join Date: Jun 2008
Posts: 11
c26354 is on a distinguished road
Hi, thanks for your replies.

The file will look like this:

Code:
<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:

Code:
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.
Bookmark Post in Technorati
Reply With Quote
  #5 (permalink)  
Old 06-26-2008, 04:57 PM
sukatoa's Avatar
Senior Member
 
Join Date: Jan 2008
Location: Cebu City, Philippines
Posts: 524
sukatoa is on a distinguished road
Send a message via Yahoo to sukatoa
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.
Bookmark Post in Technorati
Reply With Quote
  #6 (permalink)  
Old 06-26-2008, 04:59 PM
Member
 
Join Date: Jun 2008
Posts: 11
c26354 is on a distinguished road
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

Code:
String g;
and somehow, I get

Code:
array[0]
to reference g, now I can do

Code:
array[0] = "dude";
and then

Code:
System.out.println(g);
will print "dude".

Is this possible?
Bookmark Post in Technorati
Reply With Quote
  #7 (permalink)  
Old 06-26-2008, 05:11 PM
sukatoa's Avatar
Senior Member
 
Join Date: Jan 2008
Location: Cebu City, Philippines
Posts: 524
sukatoa is on a distinguished road
Send a message via Yahoo to sukatoa
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.
Bookmark Post in Technorati
Reply With Quote
  #8 (permalink)  
Old 06-26-2008, 05:25 PM
Member
 
Join Date: Jun 2008
Posts: 11
c26354 is on a distinguished road
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.
Bookmark Post in Technorati
Reply With Quote
  #9 (permalink)  
Old 06-27-2008, 03:22 AM
Senior Member
 
Join Date: Jun 2008
Posts: 178
Fubarable is on a distinguished road
Quote:
Originally Posted by c26354 View Post
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.
Bookmark Post in Technorati
Reply With Quote
  #10 (permalink)  
Old 06-27-2008, 08:26 AM
Member
 
Join Date: Jun 2008
Posts: 1
samrat is on a distinguished road
Quote:
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
Bookmark Post in Technorati
Reply With Quote
  #11 (permalink)  
Old 06-27-2008, 05:48 PM
Member
 
Join Date: Jun 2008
Posts: 11
c26354 is on a distinguished road
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.
Bookmark Post in Technorati
Reply With Quote
  #12 (permalink)  
Old 06-28-2008, 02:24 AM
tim's Avatar
tim tim is offline
Senior Member
 
Join Date: Dec 2007
Location: South Africa
Posts: 334
tim is on a distinguished road
Java "for each" loops and Serializable objects.
Hello c26354
Quote:
Originally Posted by c26354
Code:
for (parameter : value) value = inputFromFile;
This example of a "for each" loop won't work properly. Consider this:
Code:
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.
Bookmark Post in Technorati
Reply With Quote
  #13 (permalink)  
Old 06-30-2008, 04:09 PM
Member
 
Join Date: Jun 2008
Posts: 11
c26354 is on a distinguished road
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.
Bookmark Post in Technorati
Reply With Quote
Sponsored Links
Reply


Thread Tools
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

vB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On


Similar Threads
Thread Thread Starter Forum Replies Last Post
boolean variables ravian New To Java 3 12-31-2007 05:58 AM
Variables mew New To Java 3 12-11-2007 01:44 PM
JSP - session variables Java Tip Java Tips 0 12-02-2007 10:22 PM
variables-methods Warren New To Java 1 11-28-2007 05:14 PM
Saving Variables Fish New To Java 6 06-25-2007 09:20 PM


All times are GMT +3. The time now is 08:35 AM.


VBulletin, Copyright ©2000 - 2008, Jelsoft Enterprises Ltd.
Content Relevant URLs by vBSEO ©2007, Crawlability, Inc.
Copyright ©2006 - 2007, www.java-forums.org