I need to parse an xml file in java and store it in an array for sorting later. The xml file has this format
<Experiments>
<Experiment ID="312" RIndex="3" DIndex="3">40231</Experiment>
<Experiment ID="481" RIndex="2" DIndex="5">23801</Experiment>
<Experiment ID="102" RIndex="1" DIndex="5">41231</Experiment>
</Experiments>
For each experiment, the RIndex, DIndex and experiment values has to be stored. The most straight forward way I thought was a 2D array, but there’s a function where I need to sort by one of those attributes (RIndex, DIndex or Experiment value). I thought of using ArrayList of collections too. Is this the best way to do it?
I’d definitely use an ArrayList at the top level, and provide a custom
Comparatorto sort the values according to whatever sorting criteria you have in mind. For usage of comparators see:Choosing how to store the values is going to be your most important choice. The best options are likely to be:
Experimentwhich contains the fields you need. This will be most efficient.HashMapwhich maps fields to values. This will be less efficient but might be a useful approach if you have different types of experiment with different attributes etc.