• Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 213
  • Last Modified:

Maps - Urgent

Hi I have the values of maps which is stored in outer...My problem is that I dont know how to set the size of the arraylist outer as it depends on the content of the maps..pls help


ArrayList<ArrayList<Double>> outer;
ArrayList<Double> inner;
outer=new ArrayList<ArrayList<Double>>(????);
for(int i = 0;i < maps.size();i++)
{
Iterator<Map.Entry<BigDecimal,Integer>> iter = maps.get(i).entrySet().iterator();
            
while(iter.hasNext())
{
Map.Entry<BigDecimal,Integer> e = iter.next();
System.out.printf("Year = %d, value = %f, frequency = %d\n", i, e.getKey().doubleValue(), e.getValue());
               

double a=((double)e.getValue()/(double)5)*100;
inner=new ArrayList<Double>(4);
outer.add(inner);
inner.add((double)i);
inner.add(e.getKey().doubleValue());
inner.add(e.getValue().doubleValue());
inner.add(a);
                 
0
Jasbir21
Asked:
Jasbir21
  • 6
  • 5
  • 3
1 Solution
 
CEHJCommented:
You don't need to set the size of outer if you can't
0
 
CEHJCommented:
outer=new ArrayList<ArrayList<Double>>();

will do if size unknown
0
 
Mayank SAssociate Director - Product EngineeringCommented:
You should not set the size of collections, BTW.
0
Industry Leaders: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 
Jasbir21Author Commented:
Thanks a lot :-)..
0
 
Jasbir21Author Commented:
I have another question on maps too,pls look at that ..
0
 
CEHJCommented:
:-)

>>You should not set the size of collections, BTW.

What makes you say that?
0
 
Mayank SAssociate Director - Product EngineeringCommented:
Basically, you want the collection to expand. You can set the initial size or by how much you want it to expand by, but ultimately you want it to remain growable. I don't think you can set the final limit on the size of a collection. I'd use an array if I want fixed size.

>> inner=new ArrayList<Double>(4);

Doesn't mean that the size will remain 4. The initial size will be 4, but if there are expansions required, they will be done. So effectively, you can just live with inner = new ArrayList<Double> () ;
0
 
CEHJCommented:
>>but ultimately you want it to remain growable.

Not necessarily.

>>I'd use an array if I want fixed size.

That may not be convenient or even possible
0
 
Mayank SAssociate Director - Product EngineeringCommented:
I haven't come across a case in production where I wanted to keep a collection fixed size, but at least in this case I don't think that is required :)
0
 
CEHJCommented:
Well it's sometimes required, which is why the ctor x(int) is provided, together with making it possible to optimise allocations
0
 
Mayank SAssociate Director - Product EngineeringCommented:
The ctor is for initial size, right? But if you add beyond those many items, expansion will happen....?
0
 
CEHJCommented:
Expansion will happen, yes
0
 
Mayank SAssociate Director - Product EngineeringCommented:
That's what I meant :)
0

Featured Post

Industry Leaders: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

  • 6
  • 5
  • 3
Tackle projects and never again get stuck behind a technical roadblock.
Join Now