Page 1 of 2 12 LastLast
Results 1 to 20 of 22
  1. #1
    rokko is offline Member
    Join Date
    Apr 2012
    Posts
    9
    Rep Power
    0

    Default last element of recursive list

    Hey,

    i have a problem to find the last element of my recursive list

    Java Code:
    import java.util.NoSuchElementException;
    import list.List;
    
    
    public class Lists {
    	
    	/**
    	 * 
    	 * 
    	 *
    	 * Returns a string representation of the list.
    	 * 
    	 * @return a comma-separated list of the list values
    	 */
    	public static String toString(List list)
    	{
    		if (list == null) return "null";
    		
    		StringBuffer buf = new StringBuffer();
    		buf.append(list.value).append(", ").append(toString(list.next));
    		return buf.toString();
    	}
    	
    	/**
    	 * Adds a value to at the beginning of a list.
    	 * 
    	 * @param list the list to which the value is added
    	 * @param value the value which is added to the list
    	 * @return a new list with the new element first followed by the given list
    	 */
    	public static List add(List list, int value)
    	{
    		List newlist = new List(value, list);
    		list = newlist;
    		return list;
    	}
    	
    	/**
    	 * Retrieves the size of a list.
    	 * 
    	 * @param list the list in question
    	 * @return the number of values in the list.
    	 */
    	public static int size(List list)
    	{
    		return list == null ? 0 : 1 + size(list.next);
    		
    	}
    	/**
    	 * Aggregates the sum of all list values.
    	 * 
    	 * @param list the list in question
    	 * @return the sum of all list values
    	 */
    	public static int sum(List list)
    	{
    		return list == null ? 0 : list.value + sum(list.next);
    		
    	}	
    
    	/**
    	 * Retrieves the last element of a list.
    	 * 
    	 * The last element of the empty list is the empty list.
    	 * 
    	 * @param list the list in question.
    	 * @return the last element of the given list.
    	 */
    	public static List last(List list) 
        {
    		
    			if (list != null && list.next == null) {
    				
    			return list.next;
    			} else { 
    				return list;
    			}
    
    				
    			}
    it has to pass a jUnit test,


    Java Code:
    import org.junit.Assert;
    
    
    import u5a1.Lists;
    
    public class Tests {
    	public void check(List list, String expected) 
    	{ 
    		Assert.assertEquals(expected, Lists.toString(list));	
    	}
    	
    	public List create(int... values) 
    	{
    		List list = null;
    		for (int i=0; i<values.length; i++) {
    			list = Lists.add(list, values[i]);
    		}
    		return list;
    	}
    }
    
    -----
    
    private void checkLast(List list, String expected)
    	{
    		check(Lists.last(list), expected);
    	}
    	
    	@Test
    	public void last()
    	{
    		checkLast(null, "null");
    		checkLast(create(1), "1, null");
    		//checkLast(create(1,2), "1, null");
    		checkLast(create(1,2,3), "1, null");
    	}


    but i receive the whole list, not only the last element and i dont find the failure
    if its list.next == null, it means i am at the end of the list, so i can return list.next, or not?

    i hope somebody can help me

    gruss
    Last edited by rokko; 04-02-2012 at 03:07 PM.

  2. #2
    Norm's Avatar
    Norm is online now Moderator
    Join Date
    Jun 2008
    Location
    Eastern Florida
    Posts
    17,791
    Rep Power
    25

    Default Re: last element of recursive list

    Can you post the program's output and explain what is wrong with the output and show what the output should be?

    Do you have a testing program? I do not see a main() method in the code you posted.
    If you don't understand my response, don't ignore it, ask a question.

  3. #3
    Tolls is offline Moderator
    Join Date
    Apr 2009
    Posts
    12,172
    Rep Power
    20

    Default Re: last element of recursive list

    It's JUnit, which executes in its own framework.
    The entry point (as far as this is concerned) is the method marked as @Test.
    Please do not ask for code as refusal often offends.

    ** This space for rent **

  4. #4
    rokko is offline Member
    Join Date
    Apr 2012
    Posts
    9
    Rep Power
    0

    Default Re: last element of recursive list

    Quote Originally Posted by Norm View Post
    Can you post the program's output and explain what is wrong with the output and show what the output should be?

    Do you have a testing program? I do not see a main() method in the code you posted.
    the first code block is my code, which i have to test with junit (second code block), i have to return a list with the last element, the test-program makes a string of this result and checks it with junit

    the output of junit is:

    org.junit.ComparisonFailure: expected:<[1, ]null> but was:<[]null>

  5. #5
    Norm's Avatar
    Norm is online now Moderator
    Join Date
    Jun 2008
    Location
    Eastern Florida
    Posts
    17,791
    Rep Power
    25

    Default Re: last element of recursive list

    So this code can't be executed as a java program for testing?
    If you don't understand my response, don't ignore it, ask a question.

  6. #6
    JosAH's Avatar
    JosAH is offline Moderator
    Join Date
    Sep 2008
    Location
    Voorschoten, the Netherlands
    Posts
    13,728
    Blog Entries
    7
    Rep Power
    21

    Default Re: last element of recursive list

    I'd say your last( ... ) method is goofy; the (recursive) logic has to be:

    Java Code:
    List last(List list) {
       if (list == null) return null;
       if (list.next == null) return list;
       return last(list.next);
    }
    kind regards,

    Jos
    cenosillicaphobia: the fear for an empty beer glass

  7. #7
    rokko is offline Member
    Join Date
    Apr 2012
    Posts
    9
    Rep Power
    0

    Default Re: last element of recursive list

    thank you, it works!

  8. #8
    rokko is offline Member
    Join Date
    Apr 2012
    Posts
    9
    Rep Power
    0

    Default Re: last element of recursive list

    And to find a sublist

    Java Code:
    /**
    	 * Retrieves a sublist of a list.
    	 * 
    	 * @param list the list in question
    	 * @param index a position in the list starting at zero for the head value
    	 * @return the sublist with the element indexed by index as head element
    	 * @throws IndexOutOfBoundsException if the list is smaller than index
    	 */
    	public static List sublist(List list, int index) throws IndexOutOfBoundsException
    	{
    		index = size(list)-x;
    		return sublist(list, index);
    		
    	}
    i have to use size(list), right?

  9. #9
    Norm's Avatar
    Norm is online now Moderator
    Join Date
    Jun 2008
    Location
    Eastern Florida
    Posts
    17,791
    Rep Power
    25

    Default Re: last element of recursive list

    What happens when you execute the code? Does it do what you want it to do?
    If you don't understand my response, don't ignore it, ask a question.

  10. #10
    rokko is offline Member
    Join Date
    Apr 2012
    Posts
    9
    Rep Power
    0

    Default Re: last element of recursive list

    no no, it cant, because f.e. x is undefined... it was just a question to see if im on the right way or if this idea is completley wrong

  11. #11
    Norm's Avatar
    Norm is online now Moderator
    Join Date
    Jun 2008
    Location
    Eastern Florida
    Posts
    17,791
    Rep Power
    25

    Default Re: last element of recursive list

    If it does not compile, then you must be doing something wrong.
    If you don't understand my response, don't ignore it, ask a question.

  12. #12
    rokko is offline Member
    Join Date
    Apr 2012
    Posts
    9
    Rep Power
    0

    Default Re: last element of recursive list

    org.junit.ComparisonFailure: expected:<[1, ]null> but was:<[]null>
    Last edited by rokko; 04-03-2012 at 05:44 AM.

  13. #13
    Norm's Avatar
    Norm is online now Moderator
    Join Date
    Jun 2008
    Location
    Eastern Florida
    Posts
    17,791
    Rep Power
    25

    Default Re: last element of recursive list

    How can the posted code be executed for testing?
    If you don't understand my response, don't ignore it, ask a question.

  14. #14
    rokko is offline Member
    Join Date
    Apr 2012
    Posts
    9
    Rep Power
    0

    Default Re: last element of recursive list

    In eclipse you can run it as Junit-Test, the code and the test are in the same package

  15. #15
    Norm's Avatar
    Norm is online now Moderator
    Join Date
    Jun 2008
    Location
    Eastern Florida
    Posts
    17,791
    Rep Power
    25

    Default Re: last element of recursive list

    I don't use the IDE so I'd need a class with a main() method to test the code.
    If you don't understand my response, don't ignore it, ask a question.

  16. #16
    rokko is offline Member
    Join Date
    Apr 2012
    Posts
    9
    Rep Power
    0

    Default Re: last element of recursive list

    of course you need the whole test
    Last edited by rokko; 04-03-2012 at 05:44 AM.

  17. #17
    Norm's Avatar
    Norm is online now Moderator
    Join Date
    Jun 2008
    Location
    Eastern Florida
    Posts
    17,791
    Rep Power
    25

    Default Re: last element of recursive list

    Where is the starting class? The one with the main() method.
    If you don't understand my response, don't ignore it, ask a question.

  18. #18
    rokko is offline Member
    Join Date
    Apr 2012
    Posts
    9
    Rep Power
    0

    Default Re: last element of recursive list

    i doubt there is a main method^^

  19. #19
    Norm's Avatar
    Norm is online now Moderator
    Join Date
    Jun 2008
    Location
    Eastern Florida
    Posts
    17,791
    Rep Power
    25

    Default Re: last element of recursive list

    The code can not be executed for testing as an application without a class with a main() method.
    If you don't understand my response, don't ignore it, ask a question.

  20. #20
    Tolls is offline Moderator
    Join Date
    Apr 2009
    Posts
    12,172
    Rep Power
    20

    Default Re: last element of recursive list

    Quote Originally Posted by rokko View Post
    Java Code:
    	/**
    	 * Retrieves a sublist of a list.
    	 * 
    	 * @param list the list in question
    	 * @param index a position in the list starting at zero for the head value
    	 * @return the sublist with the element indexed by index as head element
    	 * @throws IndexOutOfBoundsException if the list is smaller than index
    	 */
    	public static List sublist(List list, int index) throws IndexOutOfBoundsException
    	{
    		if (list == null)
    			return list;
    		if (list.next == null){
    			index = size(list);
    			return list.next;
    		}
    		return list;
    		
    	}
    it does run, but dont pass the junit test:

    Java Code:
    @Test(expected = IndexOutOfBoundsException.class)
    	public void sublistFail1()
    	{
    		Lists.sublist(null, 0);
    	}


    org.junit.ComparisonFailure: expected:<[1, ]null> but was:<[]null>
    You should be able to say what that failure comes from.
    JUnit tells you which test has failed.

    I suspect it's the one I've posted here anyway.
    If 'list' is null then all you do is return null. You do not throw an IndexOutOfBoundsException, so that test will fail.
    Please do not ask for code as refusal often offends.

    ** This space for rent **

Page 1 of 2 12 LastLast

Similar Threads

  1. ArrayList remove an element from the list
    By ralf in forum New To Java
    Replies: 37
    Last Post: 07-12-2011, 08:08 PM
  2. Recursive Linked List method: RemoveAll
    By cjw92 in forum New To Java
    Replies: 5
    Last Post: 03-11-2011, 03:51 AM
  3. Replies: 4
    Last Post: 02-21-2011, 10:34 AM
  4. Recursive File List - Help me problem solve please
    By caps_lock in forum New To Java
    Replies: 17
    Last Post: 01-14-2009, 07:44 PM
  5. adding list to an array element
    By Preethi in forum New To Java
    Replies: 5
    Last Post: 09-25-2008, 05:23 AM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •