473,396 Members | 1,725 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,396 software developers and data experts.

using the iterable interface

thatos
105 100+
i have a class table which implements iterable row,i have implemented the following methods:hasnext,next and remove.my problem is that i dont know how to implement the following method iterator.just in case you are wondering i have a global variable rows which is an array of row
Jun 24 '08 #1
10 4098
r035198x
13,262 8TB
Those methods you have implemented are not required for an Iterable. All you need is the iterator method implementation. Are you implementing Iterable, Iterator or both?
Jun 24 '08 #2
thatos
105 100+
Those methods you have implemented are not required for an Iterable. All you need is the iterator method implementation. Are you implementing Iterable, Iterator or both?
i just want to implement the iterable but i dont know how to implement the iterator method
Jun 24 '08 #3
r035198x
13,262 8TB
i just want to implement the iterable but i dont know how to implement the iterator method
What is your table based upon?
Jun 24 '08 #4
thatos
105 100+
What is your table based upon?
the table class has a reference to rows which is an array of type row
Jun 24 '08 #5
r035198x
13,262 8TB
the table class has a reference to rows which is an array of type row
A common approach is to wrap the data items in a Java collection that already is Iterable and then just call its iterator method to get the Iterator. That depends of course on how you want to iterate through your data items.
Jun 24 '08 #6
thatos
105 100+
A common approach is to wrap the data items in a Java collection that already is Iterable and then just call its iterator method to get the Iterator. That depends of course on how you want to iterate through your data items.
What do you mean by getting an iterator which depends on how i would like to iterate over my data items,can you please give an example
Jun 24 '08 #7
JosAH
11,448 Expert 8TB
What do you mean by getting an iterator which depends on how i would like to iterate over my data items,can you please give an example
Your Iterator should be a little class that implements the Iterator<Row> interface.
An object of this little class should be returned by your (other) class that implements
the Iterable<Row> interface.

kind regards,

Jos
Jun 24 '08 #8
thatos
105 100+
Your Iterator should be a little class that implements the Iterator<Row> interface.
An object of this little class should be returned by your (other) class that implements
the Iterable<Row> interface.

kind regards,

Jos
so the other class must have those methods hasnext,next and remove .
Jun 24 '08 #9
JosAH
11,448 Expert 8TB
so the other class must have those methods hasnext,next and remove .
Yep, the little class that is supposed to implement the Iterator interface. The
class that implemenents the Iterable interface should just return an object of that
first class; all according to the documented API specifications.

kind regards,

Jos
Jun 24 '08 #10
BigDaddyLH
1,216 Expert 1GB
How about a little code? Here is a sketch that compiles.

Expand|Select|Wrap|Line Numbers
  1. import java.util.*;
  2.  
  3. class Row{}
  4.  
  5. public class Example implements Iterable<Row> {
  6.     private Row[] rows;
  7.  
  8.     @Override public Iterator<Row> iterator() {
  9.         return Arrays.asList(rows).iterator();
  10.     }
  11. }
I would prefer to get rid of the array and work just with collections. Why mix?
Jun 24 '08 #11

Sign in to post your reply or Sign up for a free account.

Similar topics

0
by: Francis Avila | last post by:
A few days ago (see the 'itertools.flatten()?' thread from October 28) I became obsessed with refactoring a recursive generator that yielded the leaves of nested iterables. When the dust settled,...
5
by: Leif K-Brooks | last post by:
Is there a word for an iterable object which isn't also an iterator, and therefor can be iterated over multiple times without being exhausted? "Sequence" is close, but a non-iterator iterable could...
4
by: Michele Simionato | last post by:
According to the standand library (http://docs.python.org/lib/typeiter.html) an *iterable* is something with an __iter__ method. This means that strings are *not* iterable. However I can loop...
0
by: Michael Spencer | last post by:
Wow - Alex Martelli's 'Black Magic' Pycon notes http://www.python.org/pycon/2005/papers/36/pyc05_bla_dp.pdf include this gem: > Functions 'r descriptors > def adder(x, y): return x + y > ...
70
by: py | last post by:
I have function which takes an argument. My code needs that argument to be an iterable (something i can loop over)...so I dont care if its a list, tuple, etc. So I need a way to make sure that...
7
by: Tim N. van der Leeuw | last post by:
Hi, I'd like to know if there's a way to check if an object is a sequence, or an iterable. Something like issequence() or isiterable(). Does something like that exist? (Something which, in...
11
by: Erich | last post by:
Hello all, Today I found myself once again defining two functions that I use all the time: nsplit and iterable. These little helper functions of mine get used all the time when I work. Im sick...
83
by: liketofindoutwhy | last post by:
I am learning more and more Prototype and Script.aculo.us and got the Bungee book... and wonder if I should get some books on jQuery (jQuery in Action, and Learning jQuery) and start learning about...
2
by: mmiikkee13 | last post by:
>>a_list = range(37) .... print k, v .... Traceback (most recent call last): File "<stdin>", line 1, in <module> TypeError: 'int' object is not iterable What 'int' object is this...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...
0
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.