• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Paul Clapham
  • Tim Cooke
  • Jeanne Boyarsky
  • Liutauras Vilda
Sheriffs:
  • Frank Carver
  • Henry Wong
  • Ron McLeod
Saloon Keepers:
  • Tim Moores
  • Frits Walraven
  • Tim Holloway
  • Stephan van Hulst
  • Carey Brown
Bartenders:
  • Al Hobbs
  • Piet Souris
  • Himai Minh

Creating an array using ArrayList()

 
Greenhorn
Posts: 23
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi,

I need to store the names and values of attributes from an XML document. I feel an ArrayList will be right for it. But will Arraylist store both names and values? I need to retrive names and values from it later on.
Or will a hashtable be better for it? Or maybe just a two dimensional array?

Shweta
 
Ranch Hand
Posts: 1071
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
It would seem to me by putting it into an ArrayList or Map or some other collection that you will lose some of the scope information. You won't be able to distinguish which element belongs to which other element. If you are using XML you probably want to look at an XML parser. Or if the data is not complex enough for that a simple properties file might be a better choice.
 
Ranch Hand
Posts: 1646
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
It depends on how you need to use those names and values. If as Steven suggests you need to relate those back to the Elements that created them, you'll want to use a more detailed structure.

However, if all you need are the names and values extracted from the XML file, an ArrayList or HashMap would suffice, but again it depends on the nature of how they'll be used.

Do you want to be able to find the values for a given name? In other words, do you need to perform a lookup from name to values? This is precisely what the various Map subclasses give you. While you can still iterate over the names or values individually, the key concept is looking up values using keys, the names in this case.

If instead you just want a list of name/value pairs, you can create a simple class that holds a single name/value pair and store those classes inside an ArrayList. This is obviously a little more work than using a HashMap, and it gives you less functionality (no lookup). However, if you need to pass name/value pairs to other parts of your program, this extra class could provide benefit.

Without knowing more, I'd say that HashMap is probably what you want in this case. Also, don't use Hashtable as it's been replaced by HashMap (as ArrayList has replaced Vector).
 
Friends help you move. Good friends help you move bodies. This tiny ad will help:
the value of filler advertising in 2021
https://coderanch.com/t/730886/filler-advertising
reply
    Bookmark Topic Watch Topic
  • New Topic