Results 1 to 6 of 6

Thread: Iterator interface problem in an ArrayList

  1. #1
    Join Date
    Dec 2009
    Posts
    204

    Iterator interface problem in an ArrayList

    Hello everyone,
    I have a problem when using the iterator interface in an ArrayList. Here is my code
    Code:
    Public class Person {
    	private String name; 
    Public Person (string name) {
    this.name = Name;
    }
    Public String getName() {
     return name;
    }
    I need a help with this, if any one knows how to do this, then please help me.

  2. #2
    Join Date
    Nov 2009
    Posts
    583

    Re: Iterator interface problem in an ArrayList

    Hello,
    I think to get the desired output you must overload the toString person in your class
    Code:
    Public String toString() {
      return name;
    }
    Just do this and see if the program runs, if not then you can try some other alternative, but till now I do not know one. I will search for it and when I will know about it, I will post back with it.

  3. #3
    Join Date
    Nov 2009
    Posts
    347

    Re: Iterator interface problem in an ArrayList

    Hello,
    Even I think you must overload the toString method of your class person. Why? Attribute name is a String, it may well use its getter for display in a System.out.println. Otherwise your code looks good! (except for errors in the code posted: typos probably ) Only thing you can do:
    Code:
    for (Iterator <person> i = lsper.iterator (); i.hasNext ())
    This is why it is necessary. Just include this in your code and see if it works, by my knowledge it should work.

  4. #4
    Join Date
    Nov 2009
    Posts
    335

    Re: Iterator interface problem in an ArrayList

    Hello,
    Your code for typos almost works. As you use parameterized types (generics) I concluded that you are using Java 1.5 or higher. You can then use the extended loop that I find more convenient than iterators:
    Code:
    for (Person Person: lsper) {
      System.out.System.out.println(person.getName());
    }
    I hope that you have included this in your code and the program runs successfully.

  5. #5
    Join Date
    Nov 2009
    Posts
    518

    Re: Iterator interface problem in an ArrayList

    Hello,
    I have a sample code with me , I have posted it, see below. If this is what you are find then well and good
    Code:
    public class tic
    {
    private int nmln;
    private ArrayList ltln;
    
    public tic()
    {
    ltln = new ArrayList();
    nmln = 1;
    }
    public void showNumbers()
    {
    Iterator it = ltln.iterator();
    while(it.hasNext()){
    Numbers ltln = (Numbers) it.next();
    System.out.println(ltln.toString());
    }
    }
    
    }

  6. #6
    Join Date
    Dec 2009
    Posts
    178

    Re: Iterator interface problem in an ArrayList

    Hello,
    Even I was find a solution for this problem but unfortunately I could not find one. If you have any solution or any idea about it then please help me with posting. Here is my code
    Code:
    Public class PersonneTest {
    	Public static void hand(String[] args) {
    			
    	ArrayList <ps> lsp = new ArrayList <ps>();
    
    lsp.add(new Person("sm"));
    lsp.add(new Person("sj"));
    		
    		for (Iterator i = lsp.iterator()i.hasNext();)
    		{
    		Person ps = (Person)i.next();
    System.out.System.out.println(ps.getName());
    		}
    	}

Similar Threads

  1. ArrayList Collection problem in java
    By Bricklayer in forum Software Development
    Replies: 4
    Last Post: 23-07-2010, 01:18 PM
  2. Designing LIFO and Iterator
    By Remedy in forum Software Development
    Replies: 5
    Last Post: 23-02-2010, 05:57 AM
  3. Synchronize an iterator in java
    By Remedy in forum Software Development
    Replies: 5
    Last Post: 18-02-2010, 01:28 AM
  4. What is the difference between Iterator and Enumeration in Java?
    By Karsenman in forum Software Development
    Replies: 4
    Last Post: 28-01-2010, 09:56 AM
  5. Problem in modifying value of ArrayList Object of JAVA
    By Praetor in forum Software Development
    Replies: 3
    Last Post: 16-11-2009, 09:58 AM

Tags for this Thread

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  
Page generated in 1,751,803,434.93953 seconds with 16 queries