# recirsive and iterator help

Page 1 of 1

## 11 Replies - 963 Views - Last Post: 11 November 2008 - 09:51 PMRate Topic: //<![CDATA[ rating = new ipb.rating( 'topic_rate_', { url: 'http://www.dreamincode.net/forums/index.php?app=forums&module=ajax&section=topics&do=rateTopic&t=70660&amp;s=085c3bdb33ef21123a541f2cd02d3a6a&md5check=' + ipb.vars['secure_hash'], cur_rating: 0, rated: 0, allow_rate: 0, multi_rate: 1, show_rate_text: true } ); //]]>

### #1 webbywebb

Reputation: 0
• Posts: 95
• Joined: 30-January 07

# recirsive and iterator help

Posted 05 November 2008 - 05:45 PM

i need to calulate the sum of 1+2+3+4+5+6+7+8+9+10 using a iterator and a recursive to implement it. i am so lost
Is This A Good Question/Topic? 0

## Replies To: recirsive and iterator help

### #2 gabehabe

• GabehabeSwamp

Reputation: 1399
• Posts: 10,965
• Joined: 06-February 08

## Re: recirsive and iterator help

Posted 05 November 2008 - 06:04 PM

[rules][/rules]

```int total = 0
for loop: i = 1; i <= 10; i++
total += i
```

### #3 pbl

• There is nothing you can't do with a JTable

Reputation: 8365
• Posts: 31,956
• Joined: 06-March 08

## Re: recirsive and iterator help

Posted 05 November 2008 - 07:12 PM

gabehabe, on 5 Nov, 2008 - 05:04 PM, said:

[rules][/rules]

```int total = 0
for loop: i = 1; i <= 10; i++
total += i
```

Not really recursive and does not implement the Iterator

### #4 pbl

• There is nothing you can't do with a JTable

Reputation: 8365
• Posts: 31,956
• Joined: 06-March 08

## Re: recirsive and iterator help

Posted 06 November 2008 - 08:35 PM

webbywebb, on 5 Nov, 2008 - 04:45 PM, said:

i need to calulate the sum of 1+2+3+4+5+6+7+8+9+10 using a iterator and a recursive to implement it. i am so lost

Re thinking of it... it is one or the other
You want to do it by iteration or recursively ? Can't be both unless Baavgay has a solution that satisfy both... this is the kind of challenge he likes

### #5 Gloin

• Expert Schmexpert...

Reputation: 235
• Posts: 4,489
• Joined: 04-August 08

## Re: recirsive and iterator help

Posted 10 November 2008 - 03:19 PM

I think he was looking for one of each solution.

### #6 Locke

• Sarcasm Extraordinaire!

Reputation: 526
• Posts: 5,604
• Joined: 20-March 08

## Re: recirsive and iterator help

Posted 10 November 2008 - 03:20 PM

gabehabe, on 5 Nov, 2008 - 05:04 PM, said:

[rules][/rules]

```int total = 0
for loop: i = 1; i <= 10; i++
total += i
```

Don't just give him the answer...-ish.

### #7 gabehabe

• GabehabeSwamp

Reputation: 1399
• Posts: 10,965
• Joined: 06-February 08

## Re: recirsive and iterator help

Posted 10 November 2008 - 03:49 PM

@PBL:
Iteratively doesn't just mean by using an iterator. The term can be used to refer to a loop, as I demonstrated.

If I'm not mistaken, an iterator object is merely a tool used to speed up the iterative process.

They're also used to point more specifically to a piece of data. For example, when you use a map in C++ you would do something like this:
```map <string, char> _map;
// do some stuff to add values
map <string, char>::iterator it; // create the iterator
for (it = _map.begin(); it != _map.end(); ++it) {
cout << it->first << '\t' << it->second;
}
```

In other words, our iterator actually takes the form of the item that it is pointing to. Using a simpler iterative approach, such as that which I demonstrated at the beginning of this thread, requires the use of an array subscript to get the value of that data.

### #8 pbl

• There is nothing you can't do with a JTable

Reputation: 8365
• Posts: 31,956
• Joined: 06-March 08

## Re: recirsive and iterator help

Posted 10 November 2008 - 06:31 PM

gabehabe, on 10 Nov, 2008 - 02:49 PM, said:

If I'm not mistaken, an iterator object is merely a tool used to speed up the iterative process.

To speed up the programmer's writing of an iterative process

### #9 webbywebb

Reputation: 0
• Posts: 95
• Joined: 30-January 07

## Re: recirsive and iterator help

Posted 11 November 2008 - 02:26 PM

mmmmm i c i am just so confused i though i had it before but i didnt :'(

### #10 BigAnt

• May Your Swords Stay Sharp

Reputation: 102
• Posts: 2,392
• Joined: 16-August 08

## Re: recirsive and iterator help

Posted 11 November 2008 - 02:57 PM

Do you mean this iterator. In the JCF the classes written can implement the iterable interface, which has one method that returns an iterator object. This iterator object has three methods: hasNext which determines if the collection has more elements, next() which returns the next element in the current iteration, and remove() which removes from the collection the last element returned by the current iterator.

If this is the iterator you need to use you will have to write your own collection class that implements the iterable interface. If you do this then you can loop through the collection using the for-each loop, which is like this:
```for(<Object/primitivetype> <variableName> : <Collection>){ //etc

```

The for-each uses the iterator to go through the Collection just like if you use a regular for loop with indexes.

### #11 webbywebb

Reputation: 0
• Posts: 95
• Joined: 30-January 07

## Re: recirsive and iterator help

Posted 11 November 2008 - 03:13 PM

oh i c

### #12 pbl

• There is nothing you can't do with a JTable

Reputation: 8365
• Posts: 31,956
• Joined: 06-March 08

## Re: recirsive and iterator help

Posted 11 November 2008 - 09:51 PM

gabehabe, on 10 Nov, 2008 - 02:49 PM, said:

@PBL:
Iteratively doesn't just mean by using an iterator. The term can be used to refer to a loop, as I demonstrated.

If I'm not mistaken, an iterator object is merely a tool used to speed up the iterative process.

They're also used to point more specifically to a piece of data. For example, when you use a map in C++ you would do something like this:
```map <string, char> _map;
// do some stuff to add values
map <string, char>::iterator it; // create the iterator
for (it = _map.begin(); it != _map.end(); ++it) {
cout << it->first << '\t' << it->second;
}
```

In other words, our iterator actually takes the form of the item that it is pointing to. Using a simpler iterative approach, such as that which I demonstrated at the beginning of this thread, requires the use of an array subscript to get the value of that data.

OK you are 100% right Gabehabe but this what I would called an Iterator in Java

```	  int[] list = {1,2,3,4,5,6,7,8,9,10};
int sum = 0;
for(int x : list)
sum += x;
System.out.println("Sum: " + sum);

```

but you are right for C++... even Boost library has a FOREACH macro that performs what your code does

Page 1 of 1

 .related ul { list-style-type: circle; font-size: 12px; font-weight: bold; } .related li { margin-bottom: 5px; background-position: left 7px !important; margin-left: -35px; } .related h2 { font-size: 18px; font-weight: bold; } .related a { color: blue; }