• Post Reply Bookmark Topic Watch Topic
  • New Topic

Doubts with Iterator  RSS feed

 
Madhumitha Baskaran
Ranch Hand
Posts: 66
  • Mark post as helpful
  • send pies
  • 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.
 
Stephan van Hulst
Saloon Keeper
Posts: 7993
143
  • Mark post as helpful
  • send pies
  • 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.
 
dennis deems
Ranch Hand
Posts: 808
  • Mark post as helpful
  • send pies
  • 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?
 
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!