• 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
  • Tim Cooke
  • paul wheaton
  • Ron McLeod
  • Jeanne Boyarsky
Sheriffs:
  • Paul Clapham
Saloon Keepers:
  • Tim Holloway
  • Roland Mueller
Bartenders:

Doubts with Iterator

 
Ranch Hand
Posts: 66
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I have an Iterator object alone. I dont have access to collections. Its difficult for me to do sorting when I have only Iterator.

I want to parse thro using Iterator and move the values to an Array which will help me to sort. But I am not able to find out number of values which will be available(necessary to allocate memory). I could think of parsing through Iterator and finding the length before I allocate memory. But it is not optimal solution. Any other suggestions?

As I have project deadline coming up soon, Please help me.
 
Bartender
Posts: 15743
368
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Why don't you have access to collections? Is it a test?

If so, you can consider making your own primitive list implementation. Just start with an array of arbitrary size, and a counter. For each element you retrieve from the iterator, add it to the array and increment the counter. When the counter reaches the size of the array, you copy all the elements of the array to a new array that's twice the size of the old array.

Finally, you could optionally trim the size of the array down when you get the last element.
 
Ranch Hand
Posts: 808
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
When you say that you don't have access to collections, do you mean that you aren't allowed to use the classes in the Java Collections framework?
 
Your mother was a hamster and your father was a tiny ad:
Clean our rivers and oceans from home
https://www.kickstarter.com/projects/paulwheaton/willow-feeders
reply
    Bookmark Topic Watch Topic
  • New Topic