Go Back   TechArena Community > Software > Software Development
Become a Member!
Forgot your username/password?
Tags Active Topics RSS Search Mark Forums Read

Reply
 
Thread Tools Search this Thread
  #1  
Old 09-02-2010
Member
 
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.
Reply With Quote
  #2  
Old 09-02-2010
Member
 
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.
Reply With Quote
  #3  
Old 09-02-2010
Member
 
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.
Reply With Quote
  #4  
Old 09-02-2010
Member
 
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.
Reply With Quote
  #5  
Old 09-02-2010
Member
 
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());
}
}

}
Reply With Quote
  #6  
Old 09-02-2010
Member
 
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());
		}
	}
Reply With Quote
Reply

  TechArena Community > Software > Software Development
Tags: , , , , , ,



Thread Tools Search this Thread
Search this Thread:

Advanced Search


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


All times are GMT +5.5. The time now is 04:53 AM.