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

Thread: Need help with a for loop

  1. #1
    Junior Member
    Join Date
    Sep 2011
    Posts
    2
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Need help with a for loop

    Hi, I have to do program that uses a for loop to traverse a linked list. I have 99% of the program done and ready to go but our professor wanted us to use a specific loop which is the loop on line 31. I don't understand what b is suppose to be. like... how would i declare b and what would it be declared as... I don't think its an iterator. ill post my code below that so you can get an idea of what i'm trying to do and what i'm missing. I dont know the data type....

    Here is the loop (its in my method):

    for(b=list;b != null;b=b.rest)
          System.out.print(b.first);

    Heres my entire code:

    import java.util.*;
     
    public class homeWorkThree{
      public static void main(String[] args)
      {
        LinkedList <Integer>list = new LinkedList<Integer>();
        int one=1, two=2, three=3, four=4, five=5, n=0;
        list.add(one);
        list.add(two);
        list.add(three);
        list.add(four);
        list.add(five);
     
        occur(list,n=4);
        boolean trueOrFalse = occur(list, n);
        if(trueOrFalse==true)
          System.out.println(trueOrFalse+": "+n+" does occur in the list");
        else
          System.out.println(trueOrFalse+": "+n+" does NOT occur in the list");
     
        occur(list,n=9);
        boolean trueOrFalseTwo = occur(list, n);
        if(trueOrFalseTwo==true)
          System.out.println(trueOrFalseTwo+": "+n+" does occur in the list");
        else
          System.out.println(trueOrFalseTwo+": "+n+" does NOT occur in the list");
      }
      public static boolean occur(LinkedList <Integer>list, int n){
        boolean isItTrue = false;
        for(b=list;b != null;b=b.rest)
          System.out.print(b.first);
        for(int i = 0; i < list.size(); ++i){
          if(list.get(i)==n)
            isItTrue=true;
          else{}
        }
       return isItTrue;
      }
    }


  2. #2
    Super Moderator Sean4u's Avatar
    Join Date
    Jul 2011
    Location
    Tavistock, UK
    Posts
    637
    Thanks
    5
    Thanked 103 Times in 93 Posts

    Default Re: Need help with a for loop

    That's how a theoretician would program iteration on a list - it's meant to be pseudo-code, isn't it? Perhaps the closest you would get with java.util.LinkedList is its pollFirst() method.

  3. #3
    Crazy Cat Lady KevinWorkman's Avatar
    Join Date
    Oct 2010
    Location
    Washington, DC
    Posts
    5,424
    My Mood
    Hungover
    Thanks
    144
    Thanked 636 Times in 540 Posts

    Default Re: Need help with a for loop

    Yeah, that sure does look like you're missing something, or like it wasn't meant to be real code.

    Also, doesn't it defeat the point of a LinkedList to use the get(i) function? Maybe that's what he was getting at.
    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!

  4. #4
    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: Need help with a for loop

    See the many posts on this cross post at Simple for loop traversing linkedlist - Page 4 - Java

  5. #5
    Super Moderator Sean4u's Avatar
    Join Date
    Jul 2011
    Location
    Tavistock, UK
    Posts
    637
    Thanks
    5
    Thanked 103 Times in 93 Posts

    Default Re: Need help with a for loop

    The pseudo-code author refers to a recursive definition of a list: a list is an element and a list (rest). There's nothing quite like that in Java at all.

    I think your professor intends for you to write a recursive linked list. It's not as hard as writing a list-of-nodes - you'd use lists-of-nodes for performance reasons, recursive lists for academic reasons!

    I just typed up a recursive list out of interest, but posting it here would certainly be spoonfeeding! Here's my main method:
      public static void main(String[] args)
      {
        CrazinsList<Character> listJPF = new CrazinsList<Character>();
        for (char c : "JavaProgrammingForums.Com".toCharArray())
          listJPF.add(c);
        System.out.println(listJPF + " is " + listJPF.size() + " long");
        System.out.println(listJPF.get(0).toString() + listJPF.get(4).toString() + listJPF.get(15).toString());
        System.out.println("Removed: " + listJPF.remove(20));
        System.out.println(listJPF);
        for (CrazinsList tmp = listJPF; tmp != null; tmp = tmp.rest)
          System.out.println(tmp + " is " + tmp.size() + " long");
      }
    - you can see your 2-line 'for' statement at the bottom. And here's the output:
    JavaProgrammingForums.Com is 25 long
    JPF
    Removed: s
    JavaProgrammingForum.Com
    JavaProgrammingForum.Com is 24 long
    avaProgrammingForum.Com is 23 long
    vaProgrammingForum.Com is 22 long
    aProgrammingForum.Com is 21 long
    ProgrammingForum.Com is 20 long
    rogrammingForum.Com is 19 long
    ogrammingForum.Com is 18 long
    grammingForum.Com is 17 long
    rammingForum.Com is 16 long
    ammingForum.Com is 15 long
    mmingForum.Com is 14 long
    mingForum.Com is 13 long
    ingForum.Com is 12 long
    ngForum.Com is 11 long
    gForum.Com is 10 long
    Forum.Com is 9 long
    orum.Com is 8 long
    rum.Com is 7 long
    um.Com is 6 long
    m.Com is 5 long
    .Com is 4 long
    Com is 3 long
    om is 2 long
    m is 1 long
    Done
    Every method inside the CrazinsList class is implemented with recursion - no loops anywhere.

Similar Threads

  1. [SOLVED] My while loop has run into an infinite loop...?
    By kari4848 in forum Loops & Control Statements
    Replies: 3
    Last Post: March 1st, 2011, 12:05 PM
  2. Odd Even - For loop
    By clevel211 in forum Loops & Control Statements
    Replies: 2
    Last Post: October 11th, 2010, 08:09 PM
  3. for loop and while loop problems
    By Pulse_Irl in forum Loops & Control Statements
    Replies: 4
    Last Post: May 3rd, 2010, 02:09 AM
  4. hi. i want to rewrite this do loop into a while loop.
    By etidd in forum Loops & Control Statements
    Replies: 3
    Last Post: January 26th, 2010, 05:27 PM
  5. Need help with loop
    By SwEeTAcTioN in forum Loops & Control Statements
    Replies: 8
    Last Post: October 25th, 2009, 05:59 PM