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 14 of 14

Thread: collection problem

  1. #1
    Junior Member
    Join Date
    Jun 2012
    Location
    Bangalore
    Posts
    20
    My Mood
    Fine
    Thanks
    2
    Thanked 0 Times in 0 Posts

    Default collection problem

    Hi all,

    I am new to this forum.

    I am using this code but it is not giving any out put

    import java.util.*;
    public class ListiteratorDemo {

    public static void main(String args[])
    {
    ArrayList ob = new ArrayList();
    ob.add(4);
    ob.add(5);
    ob.add(6);
    ob.add(7);
    ListIterator lit = ob.listIterator();
    while(lit.hasPrevious())
    {
    System.out.println(lit.previous());
    }
    }
    }





    please help me!!!!!!!


  2. #2
    Administrator copeg's Avatar
    Join Date
    Oct 2009
    Location
    US
    Posts
    5,318
    Thanks
    181
    Thanked 833 Times in 772 Posts
    Blog Entries
    5

    Default Re: collection problem

    Moved to Collections and Generics

    See the API on ListIterator (Java Platform SE 6) Iterator's move from start to end...and prior to ever calling next there is no previous element

  3. #3
    Junior Member
    Join Date
    Jun 2012
    Location
    Bangalore
    Posts
    20
    My Mood
    Fine
    Thanks
    2
    Thanked 0 Times in 0 Posts

    Default Re: collection problem

    previous is there man . why it is not showing my elements ?

  4. #4
    Member
    Join Date
    Jan 2012
    Location
    Hellas
    Posts
    284
    Thanks
    11
    Thanked 59 Times in 57 Posts

    Default Re: collection problem

    Hello freakycoder!
    Quote Originally Posted by freakycoder View Post
    previous is there man . why it is not showing my elements ?
    From the API:hasPrevious() Returns true if this list iterator has more elements when traversing the list in the reverse direction. (In other words, returns true if previous would return an element rather than throwing an exception.)

    Therefore if you print lit.previous() before the loop you will see if there is a previous element in your list.

  5. #5
    Junior Member
    Join Date
    Jun 2012
    Location
    Bangalore
    Posts
    20
    My Mood
    Fine
    Thanks
    2
    Thanked 0 Times in 0 Posts

    Default Re: collection problem

    Not getting

  6. #6
    Member
    Join Date
    Jan 2012
    Location
    Hellas
    Posts
    284
    Thanks
    11
    Thanked 59 Times in 57 Posts

    Default Re: collection problem

    Quote Originally Posted by freakycoder View Post
    Not getting
    Did you put
    System.out.println(lit.previous());
    before the while loop in your code?
    You don't get what is printed from the above statement?

  7. #7
    Junior Member
    Join Date
    Jun 2012
    Location
    Bangalore
    Posts
    20
    My Mood
    Fine
    Thanks
    2
    Thanked 0 Times in 0 Posts

    Default Re: collection problem

    No , but i but it there it will show NoSuchElementException and putting it as show above it show no out put just blank console

  8. #8
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,140
    Thanks
    65
    Thanked 2,720 Times in 2,670 Posts

    Default Re: collection problem

    To see what the code is doing, printout the value of the condition used in the while() statement before executing the while statement. That will show you why the while is not executing.
    If you don't understand my answer, don't ignore it, ask a question.

  9. #9
    Junior Member
    Join Date
    Jun 2012
    Location
    Bangalore
    Posts
    20
    My Mood
    Fine
    Thanks
    2
    Thanked 0 Times in 0 Posts

    Default Re: collection problem

    System.out.println(lit.hasPrevious()); it is printing false . what its mean

  10. #10
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,140
    Thanks
    65
    Thanked 2,720 Times in 2,670 Posts

    Default Re: collection problem

    The while loop will never execute.

    Add this just before the println statement you just added:
          lit.next(); lit.next(); //<<<< Move 2 slots to make for previous
    Last edited by Norm; June 26th, 2012 at 10:47 AM.
    If you don't understand my answer, don't ignore it, ask a question.

  11. #11
    Junior Member
    Join Date
    Jun 2012
    Location
    Bangalore
    Posts
    20
    My Mood
    Fine
    Thanks
    2
    Thanked 0 Times in 0 Posts

    Default Re: collection problem

    yeah it is working but is it compulsory to write next() method before each of previous method?

  12. #12
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,140
    Thanks
    65
    Thanked 2,720 Times in 2,670 Posts

    Default Re: collection problem

    If you are at the beginning of a list, what would be the previous element in the list?
    Can you give me an example?
     1st   2nd     3rd
    ^    ^       ^       ^
    If you are before 1st what would be the previous element?
    Last edited by Norm; June 26th, 2012 at 11:02 AM.
    If you don't understand my answer, don't ignore it, ask a question.

  13. #13
    Junior Member
    Join Date
    Jun 2012
    Location
    Bangalore
    Posts
    20
    My Mood
    Fine
    Thanks
    2
    Thanked 0 Times in 0 Posts

    Default Re: collection problem

    Now its working but I want to know one more thing that is it compulsory to write next method before each of previous() method ?

  14. #14
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,140
    Thanks
    65
    Thanked 2,720 Times in 2,670 Posts

    Default Re: collection problem

    See post #12
    If you don't understand my answer, don't ignore it, ask a question.

  15. The Following User Says Thank You to Norm For This Useful Post:

    freakycoder (June 27th, 2012)

Similar Threads

  1. Replies: 2
    Last Post: February 17th, 2012, 02:13 AM
  2. Synchronized Collection
    By tcstcs in forum Java Theory & Questions
    Replies: 2
    Last Post: March 29th, 2011, 12:09 AM
  3. Looking for Collection class to do first if first out
    By snytkine in forum Collections and Generics
    Replies: 5
    Last Post: October 7th, 2010, 09:18 PM
  4. Collection.sort problem
    By Minken in forum What's Wrong With My Code?
    Replies: 12
    Last Post: September 21st, 2010, 06:24 PM
  5. Garbage Collection?
    By kalees in forum Collections and Generics
    Replies: 6
    Last Post: September 23rd, 2009, 03:07 AM