View RSS Feed

fatabass

Making your own class Iterable.

Rating: 1 votes, 1.00 average.
by , 02-09-2012 at 07:21 PM (2033 Views)
Java Code:
package myPaackkage;

import java.util.*;


public class SomeClass implements Iterable<String> 
{
	@Override
	public Iterator<String> iterator() 
	{
		System.out.println("Hello!!");
		
		return new Iterator<String>()
				{
			private int i = 0;

					@Override
					public boolean hasNext() {
						while(i<words.size())
							return true;
						return false;
					}

					@Override
					public String next() {
						// TODO Auto-generated method stub
						return words.get(i++);
					}

					@Override
					public void remove() {
						// TODO Auto-generated method stub
						
					}
			
				};
	}
	
	ArrayList<String> words = new ArrayList<String>(Arrays.asList("Hello","Mello"));
		
	public static void main(String[] args)
	{
	
		SomeClass testt = new SomeClass();
	
		for(String s: testt)
		{
			System.out.println(s);
			System.out.println(testt.iterator().getClass());
		}

		
	}

}

Submit "Making your own class Iterable." to Facebook Submit "Making your own class Iterable." to Digg Submit "Making your own class Iterable." to del.icio.us Submit "Making your own class Iterable." to StumbleUpon Submit "Making your own class Iterable." to Google

Updated 02-21-2012 at 10:33 PM by fatabass (Code tags.)

Tags: None Add / Edit Tags
Categories
Uncategorized

Comments