Welcome to the Java Programming Forums


The professional, friendly Java community. 21,500 members and growing!


The Java Programming Forums are a community of Java programmers from all around the World. Our members have a wide range of skills and they all have one thing in common: A passion to learn and code Java. We invite beginner Java programmers right through to Java professionals to post here and share your knowledge. Become a part of the community, help others, expand your knowledge of Java and enjoy talking with like minded people. Registration is quick and best of all free. We look forward to meeting you.


>> REGISTER NOW TO START POSTING


Members have full access to the forums. Advertisements are removed for registered users.

Results 1 to 4 of 4

Thread: LinkedList outputs ONLY last element

  1. #1
    Member hexwind's Avatar
    Join Date
    May 2011
    Location
    Tunisia
    Posts
    48
    Thanks
    4
    Thanked 1 Time in 1 Post

    Default LinkedList outputs ONLY last element

    Hello guys
    I'm working on Collection framework and doing random exercises off my head. I want to fill a linked list with objects and then display the elements of the linked list, it only output the last element of the list.
    here is my code
     
    import java.util.*;
    class MyClass{
    	String name;
    	int age;
    	Scanner scan = new Scanner(System.in);
    	void setName(){
    		System.out.println("name:");
    		this.name = scan.next();
    	}
     
    	String getName(){
    		return this.name;
    	}
    }
    public class Napster {
    	public static void main(String[] args){
    	MyClass c = new MyClass();
    	List<MyClass> l = new LinkedList<MyClass>();
     
    	c.setName();
    	l.add(c);
    	c.setName();
    	l.add(c);
     
    	for(MyClass s : l){
    		System.out.println("Name =>"+ s.getName());
    	}
     
    	}
    }
    My website : MediDev


  2. #2
    Member OutputStream's Avatar
    Join Date
    Apr 2011
    Posts
    32
    My Mood
    Fine
    Thanks
    1
    Thanked 4 Times in 3 Posts

    Default Re: LinkedList outputs ONLY last element

    c.setName();
    	l.add(c);
    	c.setName();
    	l.add(c);

    You update the name of an object and add it to the list. Then you update the name again of the same object and add it again to the list again.
    Do you see what's wrong here?

  3. The Following User Says Thank You to OutputStream For This Useful Post:

    hexwind (June 30th, 2011)

  4. #3
    Member hexwind's Avatar
    Join Date
    May 2011
    Location
    Tunisia
    Posts
    48
    Thanks
    4
    Thanked 1 Time in 1 Post

    Default Re: LinkedList outputs ONLY last element

    Quote Originally Posted by OutputStream View Post
    c.setName();
    	l.add(c);
    	c.setName();
    	l.add(c);

    You update the name of an object and add it to the list. Then you update the name again of the same object and add it again to the list again.
    Do you see what's wrong here?
    thanks a lot! now I see my problem.
    I edited as follow (in case this helps anybody) :
    l.add(new MyClass());
    	l.get(0).setName();
    	l.add(new MyClass());
    	l.get(1).setName();
     
    	for(MyClass s : l){
    		System.out.println("Name => "+ s.getName());
    	}
    My website : MediDev

  5. #4
    Member OutputStream's Avatar
    Join Date
    Apr 2011
    Posts
    32
    My Mood
    Fine
    Thanks
    1
    Thanked 4 Times in 3 Posts

    Default Re: LinkedList outputs ONLY last element

    Glad you solved it

Similar Threads

  1. New to Java Can not figure out how to create a table with my codes outputs!
    By shoppa028 in forum What's Wrong With My Code?
    Replies: 1
    Last Post: May 12th, 2011, 09:21 AM
  2. Reversing lines using LinkedList
    By velop in forum What's Wrong With My Code?
    Replies: 7
    Last Post: February 14th, 2011, 06:10 AM
  3. LinkedList Objects
    By thedolphin13 in forum What's Wrong With My Code?
    Replies: 11
    Last Post: October 13th, 2010, 03:14 PM
  4. Writing clone() method for LinkedList
    By vluong in forum Collections and Generics
    Replies: 6
    Last Post: October 27th, 2009, 08:41 AM
  5. Implementing LinkedList as a user?
    By vluong in forum Collections and Generics
    Replies: 3
    Last Post: October 15th, 2009, 03:00 AM