dcsimg
CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Results 1 to 10 of 10

Thread: Is it possible to dynamically create variables?

  1. #1
    Join Date
    May 2010
    Posts
    3

    Is it possible to dynamically create variables?

    I was wondering if it's actually possible to read in a text file of the following,

    Code:
    Item1,Brand1
    Item2,Brand1
    Item3,Brand2
    Item4,Brand2
    Then dynamically create individual array lists according to the different brand names (ignoring duplicates), instead of manually counting how many different brands there are and manually creating.

    For example, if there are three different brands found, there would be three individual array lists. If I append the text file and have four brands, there would be four array lists automatically created. So on and so for.

    I was planning on putting the array lists into a single hashmap<String, ArrayList>, called Brands (with the brand name as key), where I can simply extract keys and values out to do object comparison for something like a search filter/result according to the different brands.

    Is it possible? What other ways can I do and what exactly should I look into?


    Thanks in advanced,
    Daniel

  2. #2
    Join Date
    Jun 1999
    Location
    Eastern Florida
    Posts
    3,856

    Re: Is it possible to dynamically create variables?

    Yes, that sounds OK.
    Norm

  3. #3
    Join Date
    May 2010
    Posts
    3

    Re: Is it possible to dynamically create variables?

    Err, may I know how to go about it?

    Cause I would usually go manually by:

    Code:
    ArrayList _brand1 = new ArrayList();
    ArrayList _brand2 = new ArrayList();
    If I appended the text file to have three different brands then I would have to manually add another one:

    Code:
    ArrayList _brand3 = new ArrayList();
    So how do I go about with the dynamic creation? (Sorry if it's not the right term)

  4. #4
    Join Date
    May 2006
    Location
    UK
    Posts
    4,473

    Re: Is it possible to dynamically create variables?

    In your first post you described a way of doing it by using a Map to hold the ArrayLists. The following adds a little more detail to this approach.

    You read in each line and for each line check to see if the map already has a key with the brand name and if it does not you add a new key and ArrayList pair for this brand. Finally you add the item to ArrayList referenced by the brand.
    Posting code? Use code tags like this: [code]...Your code here...[/code]
    Click here for examples of Java Code

  5. #5
    Join Date
    Jun 2007
    Location
    Aurora CO USA
    Posts
    137

    Re: Is it possible to dynamically create variables?

    You'd want to do something like this:

    Code:
    while(file has entries)
    {
        read item
        read brand
        if (hashmap contains brand)
        {
            itemlist = get arraylist from hashmap for brand
        }
        else
        {
            itemlist = create a new arraylist
            add itemlist to hashmap
        }
        add item to itemlist
    }
    Try to work up some code based on this algorithm and let us know where you need help.

  6. #6
    Join Date
    Jun 2007
    Location
    Aurora CO USA
    Posts
    137

    Re: Is it possible to dynamically create variables?

    Quote Originally Posted by keang View Post
    In your first post you described a way of doing it by using a Map to hold the ArrayLists. The following adds a little more detail to this approach.

    You read in each line and for each line check to see if the map already has a key with the brand name and if it does not you add a new key and ArrayList pair for this brand. Finally you add the item to ArrayList referenced by the brand.
    Dang it, too slow again!

  7. #7
    Join Date
    May 2006
    Location
    UK
    Posts
    4,473

    Re: Is it possible to dynamically create variables?

    Quote Originally Posted by ajhampson
    Dang it, too slow again!
    Yes but my description and your pseudo code match perfectly and together give a more complete answer
    Posting code? Use code tags like this: [code]...Your code here...[/code]
    Click here for examples of Java Code

  8. #8
    Join Date
    May 2010
    Posts
    3

    Re: Is it possible to dynamically create variables?

    It works!! Thanks, all! (^^,) I was worried about the itemlist getting overwritten when you new it with the same name in the loop. But it didn't! Thanks again~

  9. #9
    Join Date
    Jun 1999
    Location
    Eastern Florida
    Posts
    3,856

    Re: Is it possible to dynamically create variables?

    Yes, itemlist's value changes but then its value is stored in the HashMap before a new value is assigned.
    Norm

  10. #10
    dlorde is offline Elite Member Power Poster
    Join Date
    Aug 1999
    Location
    UK
    Posts
    10,163

    Re: Is it possible to dynamically create variables?

    Remember that in Java object variables are references, i.e. they are pointers to the object created by 'new'. When you assign one variable to another, you're transferring a pointer/reference value.

    Java keeps track of objects, and when an object no longer has any (usable) reference/pointers, it can be removed from memory. This is called 'garbage collection'.

    They know enough who know how to learn...
    J. Adams
    Please use &#91;CODE]...your code here...&#91;/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
  •  


Windows Mobile Development Center


Click Here to Expand Forum to Full Width




On-Demand Webinars (sponsored)