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 : Adding elements in constructor

  1. #1
    Junior Member
    Join Date
    Mar 2012
    Posts
    2
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default LinkedList : Adding elements in constructor

    Hi,


    I am new to java and need help to understand following behavior I tried today.


    I have a class variable defined like:

    private static LinkedList<Integer> queue = new LinkedList<Integer>();

    In Constructor I am adding some elemets as following:

    Queue(){
    for(int i=0;i<10;i++){
    queue.add(i);
    }
    }


    While in main its shows LinkedList as empty. So my question is why adding elements in constructor is not working for me.

    Thanks in advance


  2. #2
    Crazy Cat Lady KevinWorkman's Avatar
    Join Date
    Oct 2010
    Location
    Washington, DC
    Posts
    5,283
    My Mood
    Hungover
    Thanks
    142
    Thanked 620 Times in 531 Posts

    Default Re: LinkedList : Adding elements in constructor

    If you want help, you'll have to provide an SSCCE that demonstrates exactly what you're doing. For example, we have no idea what your main looks like, as you have not provided it. Don't forget the highlight tags.
    Useful links: How to Ask Questions the Smart Way | Use Code Tags | Java Tutorials
    Static Void Games - Play indie games, learn from game tutorials and source code, upload your own games!

  3. #3
    Junior Member
    Join Date
    Mar 2012
    Posts
    2
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Re: LinkedList : Adding elements in constructor

    Hi ,
    Folowing is the the code:

    package datastructure;
     
    import java.util.LinkedList;
     
    public class QueueTest {
     
            private static LinkedList<Integer> queue =  new LinkedList<Integer>();
     
     
    	QueueTest(){ // static linked list's initialization in constructor
    		for(int i=0;i<10;i++){
    			System.out.println(i);
    			queue.add(i);
    			}
    	}
     
     
    	/**
    	 * @param args
    	 */
    	public static void main(String[] args) {
     
    		int i=0;
    		// I am getting queue will no elements at this point
    		while(i <5){
    			i++;
    			System.out.println(queue.removeFirst()); // gives java.util.NoSuchElementException
     
    		}
     
     
     
     
    	}
     
    }
    Last edited by Anusha870; March 8th, 2012 at 11:54 AM.

  4. #4
    Crazy Cat Lady KevinWorkman's Avatar
    Join Date
    Oct 2010
    Location
    Washington, DC
    Posts
    5,283
    My Mood
    Hungover
    Thanks
    142
    Thanked 620 Times in 531 Posts

    Default Re: LinkedList : Adding elements in constructor

    Where are your highlight tags? Unformatted code is very hard to read.

    When do you call the QueueTest constructor?

    Why are you referring to your queue variable in both a static and non-static context?
    Useful links: How to Ask Questions the Smart Way | Use Code Tags | Java Tutorials
    Static Void Games - Play indie games, learn from game tutorials and source code, upload your own games!

Similar Threads

  1. java: adding array elements
    By dambundos in forum Collections and Generics
    Replies: 3
    Last Post: November 4th, 2011, 06:30 AM
  2. Help with ArrayList ( Adding certain elements together)
    By williamsant in forum Collections and Generics
    Replies: 13
    Last Post: September 27th, 2011, 09:32 AM
  3. [SOLVED] ArrayList object's elements confusing??? doesnt replace the elements? set() method?
    By chronoz13 in forum What's Wrong With My Code?
    Replies: 10
    Last Post: June 21st, 2011, 01:20 PM
  4. Adding array elements inside a loop
    By Scotty in forum What's Wrong With My Code?
    Replies: 3
    Last Post: November 28th, 2010, 08:48 PM
  5. Method Adding elements to an array with certain restrictions
    By Newoor in forum Collections and Generics
    Replies: 1
    Last Post: December 13th, 2009, 10:13 AM