Home | Contact Us | FAQ | Search & Site Map | Link to Us
Sign In | Join | Other 45 Sites in Network
HomeAnnouncementsWhite Papers
Discussion GroupsFirst AidDatabasesJavaBeansGUIJava 3DVirtual MachineCORBASecurityToolsGeneral
Java DirectoryOpen Source ProjectsSample Book ChaptersUser GroupsWeb Resources
Related Topics
Databases.NETMore Topics ...

Java Forum / General / November 2007

Tip: Looking for answers? Try searching our database.

simple program using interface " iterable"

Thread view: 
Thomas - 12 Nov 2007 14:26 GMT
Hello, I have to write a simple program containing a list which would use a
interface iterable or iterate (don't know exaclty whitch one it ). The list
( which would be programmed on my own) would return the iterator, which
would use the standard
methods as hasNext(), remove() and next(). I tried to find some examples
over the internet and the Gosling book " The Java ..." but neither one help
me. I need a simple template which would work or a link where it is
explained.
Lew - 12 Nov 2007 14:38 GMT
> Hello, I have to write a simple program containing a list which would use a
> interface iterable or iterate (don't know exaclty whitch one it ). The list

Did you mean Iterable, as in java.util.Iterable?  (Spelling counts.)

> ( which would be programmed on my own) would return the iterator, which
> would use the standard
> methods as hasNext(), remove() and next(). I tried to find some examples
> over the internet and the Gosling book " The Java ..." but neither one help
> me. I need a simple template which would work or a link where it is
> explained.

If you extend AbstractList you get Iterable for free.

If you simply add "implements Iterable" to your class declaration (they did
cover this in class right?  You attended?  You took notes?) and implement all
the methods described in Iterable's Javadocs (you do read the Javadocs,
right?), you're home free.

Signature

Lew

Lew - 12 Nov 2007 14:39 GMT
> Did you mean Iterable, as in java.util.Iterable?  (Spelling counts.)

So does the right package name: java.lang.Iterable.  Oops.

I got confused with java.util.Iterator.

Signature

Lew

Thomas - 13 Nov 2007 01:15 GMT
>> Hello, I have to write a simple program containing a list which would use
>> a interface iterable or iterate (don't know exaclty whitch one it ). The
[quoted text clipped - 13 lines]
> If you simply add "implements Iterable" to your class declaration (they
> did cover this in class right?
Yes I TOOK notes, but this topic wasn't covered and I have to write it on
Thursday before the deadline and have six other
curses so please be less sarcastic.

This template does work :

**********************************

import java.util.Iterator;

public class My_iterator implements Iterator {

 public My_iterator(){
 }

 public boolean hasNext() {
 return false;
 }

 public Object  next() {
 return new Character('A');
 }

 public void remove() {

 }
}
****************************************

but this doesn't :
***************************************
import java.util.Iterator;
public class ListNode implements java.lang.Iterable {

 private String str;
 private int count = 0;
 public ListNode(){

  }

 public Iterator iterator(){};
}
*****************************************
since I got:
cannot resolve class Iterator
 cannot resolve class Iterable

You attended?  You took notes?) and implement all
> the methods described in Iterable's Javadocs (you do read the Javadocs,
> right?), you're home free.
Arne Vajhøj - 13 Nov 2007 01:27 GMT
> but this doesn't :
> ***************************************
[quoted text clipped - 13 lines]
>  cannot resolve class Iterator
>   cannot resolve class Iterable

I get:

Z.java:10: missing return statement
  public Iterator iterator(){};
                             ^
1 error

which seems as very descriptive error message.

Arne
Lew - 13 Nov 2007 02:06 GMT
>> but this doesn't :
>> ***************************************
[quoted text clipped - 22 lines]
>
> which seems as very descriptive error message.

That last semicolon has to go, also.

Signature

Lew

Arne Vajhøj - 13 Nov 2007 02:36 GMT
> That last semicolon has to go, also.

It is not necessary, but it compiles with it.

Arne
Lew - 13 Nov 2007 02:43 GMT
>> That last semicolon has to go, also.
>
> It is not necessary, but it compiles with it.

Are you saying that it should not go?

If so, why should it stay?

Signature

Lew

Arne Vajhøj - 13 Nov 2007 03:01 GMT
>>> That last semicolon has to go, also.
>>
>> It is not necessary, but it compiles with it.
>
> Are you saying that it should not go?

As stated - it compiles with it.

> If so, why should it stay?

I think the original poster has better things
to spend his time on learning than optional
semicolons or not.

Arne
Lew - 13 Nov 2007 03:36 GMT
>>>> That last semicolon has to go, also.
>>>
[quoted text clipped - 9 lines]
> to spend his time on learning than optional
> semicolons or not.

Ok.

I think it's a bad idea to put spurious empty constructor lines in strange
places in one's source.

It's a bad habit to start, and the beginning is when it's easiest to form
habits for good or ill.

Signature

Lew



Free Magazines

Get these publications absolutely FREE for up to 12 months. There are no hidden fees and no obligation. Simply choose a title, complete the application form and submit it. Read more ...

Oracle MagazineNetwork ComputingComputer WorldBio-IT WorldeWeekInformation WeekInfosecurity
 
Sign In
Join
My Latest Posts
My Monitored Threads
My Blog
My Photo Gallery
My Profile
My Homepage

Start New Thread
Enable EMail Alerts
Rate this Thread



©2008 Advenet LLC   Privacy Policy - Terms of Use
This website includes both content owned or controlled by Advenet as well as content owned or controlled by third parties.