Home arrow static arrow Java Programming [Archive] - iterating thru numbers
Warning: Creating default object from empty value in /www/htdocs/w008deb8/wiki/components/com_staticxt/staticxt.php on line 51
Java Programming [Archive] - iterating thru numbers
This topic has 7 replies on 1 page.

Posts:211
Registered: 5/12/03
iterating thru numbers  
Jul 30, 2004 11:18 AM



 
Hi all

I was wondering if there is a class that iterates thru a set of numbers, given a start and an interval? Kind of like a ListIterator (with next() and previous()).

If not not too difficult to write one...

Thanx in advance.
 

Posts:6,147
Registered: 11/9/00
Re: iterating thru numbers  
Jul 30, 2004 11:33 AM (reply 1 of 7)



 
It's hard to imagine a situation where a for loop wouldn't serve you better.
 

Posts:3,055
Registered: 18/06/98
Re: iterating thru numbers  
Jul 30, 2004 11:35 AM (reply 2 of 7)



 
Are you a C++ / STL fan?
Such requirement appears to be from someone that knows the "algorithm" header and wants to use something like the "generate" function template.
There is the http://jga.sourceforge.net/ (generic algorithms for Java) package from David A Hall, a frequent poster in this forum.
 

Posts:211
Registered: 5/12/03
Re: iterating thru numbers  
Jul 30, 2004 11:37 AM (reply 3 of 7)



 

well,

sometimes we might want to just loop thru, but sometimes we might was to be able to get the nth number in the list, and sometimes we might need to be able to use next() and previous().

loop wont do that by itself.
 

Posts:173
Registered: 3/6/00
series of numbers  
Jul 30, 2004 1:21 PM (reply 4 of 7)



 
You could try javax.swing.SpinnerNumberModel.
 

Posts:105
Registered: 6/8/04
Re: iterating thru numbers  
Jul 30, 2004 1:30 PM (reply 5 of 7)



 
class NumberIterator{     private int now;     private int max;      public NumberIterator()     {          now = 0;          max = -1;     }          public NumberIterator(int interval)     {          now = 0;          max = interval;     }      public NumberIterator(int start, int interval)     {          now = start;          max = interval;     }      public boolean hasNext()     {          return (now == max);     }      public int next()     {          now++;          return (now - 1);     } }


Untested, uncommented code. Just thought it sounded like a fun quickie.

Note that, if you supply -1 for the interval, it'll keep going indefinitely. In case you want it to.

Although a for loop would be better, methinks...
 

Posts:4,906
Registered: 23/07/02
Re: iterating thru numbers  
Jul 30, 2004 2:31 PM (reply 6 of 7)



 
Optimisation:
    public int next()    {        return now++;    }
You want to change that == to != in hasNext(), too.
 

Posts:211
Registered: 5/12/03
Re: iterating thru numbers  
Aug 2, 2004 8:20 AM (reply 7 of 7)



 
thanx for the help guys -- I didnt expect to see implementations. I went ahead and did my own implementation because its an easy problem. Itll be fun to update my implementation to handle multidimensional iterators, for data sets where teh values lie on more than one axis.
 
This topic has 7 replies on 1 page.