Results 1 to 13 of 13
  1. #1
    c26354 is offline Member
    Join Date
    Jun 2008
    Posts
    11
    Rep Power
    0

    Default 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.

  2. #2
    Eranga's Avatar
    Eranga is offline Moderator
    Join Date
    Jul 2007
    Location
    Colombo, Sri Lanka
    Posts
    11,372
    Blog Entries
    1
    Rep Power
    19

  3. #3
    Fubarable's Avatar
    Fubarable is offline Moderator
    Join Date
    Jun 2008
    Posts
    19,316
    Blog Entries
    1
    Rep Power
    25

    Default

    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?

  4. #4
    c26354 is offline Member
    Join Date
    Jun 2008
    Posts
    11
    Rep Power
    0

    Default

    Hi, thanks for your replies.

    The file will look like this:

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

    Java 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.

  5. #5
    sukatoa's Avatar
    sukatoa is offline Senior Member
    Join Date
    Jan 2008
    Location
    Cebu City, Philippines
    Posts
    556
    Rep Power
    7

    Default

    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

  6. #6
    c26354 is offline Member
    Join Date
    Jun 2008
    Posts
    11
    Rep Power
    0

    Default

    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

    Java Code:
    String g;
    and somehow, I get

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

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

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

    Is this possible?

  7. #7
    sukatoa's Avatar
    sukatoa is offline Senior Member
    Join Date
    Jan 2008
    Location
    Cebu City, Philippines
    Posts
    556
    Rep Power
    7

    Default

    yes you can.... but you will be amazed if you try the Java's Collection Framework
    freedom exists in the world of ideas

  8. #8
    c26354 is offline Member
    Join Date
    Jun 2008
    Posts
    11
    Rep Power
    0

    Default

    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.

  9. #9
    Fubarable's Avatar
    Fubarable is offline Moderator
    Join Date
    Jun 2008
    Posts
    19,316
    Blog Entries
    1
    Rep Power
    25

    Default

    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.

  10. #10
    samrat is offline Member
    Join Date
    Jun 2008
    Posts
    1
    Rep Power
    0

    Default

    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.:o
    Thanxs

  11. #11
    c26354 is offline Member
    Join Date
    Jun 2008
    Posts
    11
    Rep Power
    0

    Default

    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.

  12. #12
    tim's Avatar
    tim
    tim is offline Senior Member
    Join Date
    Dec 2007
    Posts
    435
    Rep Power
    7

    Default Java "for each" loops and Serializable objects.

    Hello c26354
    Quote Originally Posted by c26354
    Java Code:
    for (parameter : value)
        value = inputFromFile;
    This example of a "for each" loop won't work properly. :eek: Consider this:
    Java 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. :D
    Eyes dwelling into the past are blind to what lies in the future. Step carefully.

  13. #13
    c26354 is offline Member
    Join Date
    Jun 2008
    Posts
    11
    Rep Power
    0

    Default

    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.

Similar Threads

  1. boolean variables
    By ravian in forum New To Java
    Replies: 3
    Last Post: 12-31-2007, 04:58 AM
  2. Variables
    By mew in forum New To Java
    Replies: 3
    Last Post: 12-11-2007, 12:44 PM
  3. JSP - session variables
    By Java Tip in forum Java Tip
    Replies: 0
    Last Post: 12-02-2007, 09:22 PM
  4. variables-methods
    By Warren in forum New To Java
    Replies: 1
    Last Post: 11-28-2007, 04:14 PM
  5. Saving Variables
    By Fish in forum New To Java
    Replies: 6
    Last Post: 06-25-2007, 08:20 PM

Posting Permissions

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