Results 1 to 13 of 13
Thread: Collecton of Variables
- 06-25-2008, 09:25 PM #1
Member
- Join Date
- Jun 2008
- Posts
- 11
- Rep Power
- 0
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, 04:45 AM #2
- Join Date
- Jul 2007
- Location
- Colombo, Sri Lanka
- Posts
- 11,374
- Blog Entries
- 1
- Rep Power
- 18
You saying that data in the file is not in a specific order in data type?
-
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, 03:29 PM #4
Member
- Join Date
- Jun 2008
- Posts
- 11
- Rep Power
- 0
Hi, thanks for your replies.
The file will look like this:
As you can see, the file is .xml and some of the perameters are integers(num and len) and some are strings(id, accession).Java 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>
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:
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.Java Code:classInstance.params.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, 03:57 PM #5
HashMap<String, ArrayList>
Where String is your key/node and ArrayList must be Object implemented....
You can try it....freedom exists in the world of ideas
- 06-26-2008, 03:59 PM #6
Member
- Join Date
- Jun 2008
- Posts
- 11
- Rep Power
- 0
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 getJava Code:String g;
to reference g, now I can doJava Code:array[0]
and thenJava Code:array[0] = "dude";
will print "dude".Java Code:System.out.println(g);
Is this possible?
- 06-26-2008, 04:11 PM #7
yes you can.... but you will be amazed if you try the Java's Collection Framework
freedom exists in the world of ideas
- 06-26-2008, 04:25 PM #8
Member
- Join Date
- Jun 2008
- Posts
- 11
- Rep Power
- 0
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, 07:26 AM #10
Member
- Join Date
- Jun 2008
- Posts
- 1
- Rep Power
- 0
sukatoa is right...you can use Java's Collection Framework..you also can access a particular parameter without remember its name.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 can use ArrayList/LinkedList class and through accessing element you will get appropriate result.
You need some more idea about Collection Framework.:o
Thanxs
- 06-27-2008, 04:48 PM #11
Member
- Join Date
- Jun 2008
- Posts
- 11
- Rep Power
- 0
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, 01:24 AM #12
Java "for each" loops and Serializable objects.
Hello c26354
This example of a "for each" loop won't work properly. :eek: Consider this:
Originally Posted by c26354
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.Java Code:Vector<Dog> dogs = new Vector<Dog>(); populate(dogs); for (Dog dog : dogs){ dog = new Dog("Pete's clone"); }
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. :DEyes dwelling into the past are blind to what lies in the future. Step carefully.
- 06-30-2008, 03:09 PM #13
Member
- Join Date
- Jun 2008
- Posts
- 11
- Rep Power
- 0
Similar Threads
-
boolean variables
By ravian in forum New To JavaReplies: 3Last Post: 12-31-2007, 04:58 AM -
Variables
By mew in forum New To JavaReplies: 3Last Post: 12-11-2007, 12:44 PM -
JSP - session variables
By Java Tip in forum Java TipReplies: 0Last Post: 12-02-2007, 09:22 PM -
variables-methods
By Warren in forum New To JavaReplies: 1Last Post: 11-28-2007, 04:14 PM -
Saving Variables
By Fish in forum New To JavaReplies: 6Last Post: 06-25-2007, 08:20 PM


LinkBack URL
About LinkBacks
Reply With Quote

Bookmarks