Help coderanch get a
new server
by contributing to the fundraiser
  • 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
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

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.
 
Saloon Keeper
Posts: 15656
367
  • 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?
 
The only taste of success some people get is to take a bite out of you. Or this tiny ad:
We need your help - Coderanch server fundraiser
https://coderanch.com/t/782867/Coderanch-server-fundraiser
reply
    Bookmark Topic Watch Topic
  • New Topic