Results 1 to 20 of 22
Thread: last element of recursive list
- 04-02-2012, 02:04 PM #1
Member
- Join Date
- Apr 2012
- Posts
- 9
- Rep Power
- 0
last element of recursive list
Hey,
i have a problem to find the last element of my recursive list
it has to pass a jUnit test,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; } }
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
grussLast edited by rokko; 04-02-2012 at 02:07 PM.
- 04-02-2012, 02:28 PM #2
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.
- 04-02-2012, 03:16 PM #3
Moderator
- Join Date
- Apr 2009
- Posts
- 10,438
- Rep Power
- 16
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.
- 04-02-2012, 03:23 PM #4
Member
- Join Date
- Apr 2012
- Posts
- 9
- Rep Power
- 0
Re: last element of recursive list
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>
- 04-02-2012, 03:25 PM #5
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.
- 04-02-2012, 03:28 PM #6
- Join Date
- Sep 2008
- Location
- Voorschoten, the Netherlands
- Posts
- 11,380
- Blog Entries
- 7
- Rep Power
- 17
Re: last element of recursive list
I'd say your last( ... ) method is goofy; the (recursive) logic has to be:
kind regards,Java Code:List last(List list) { if (list == null) return null; if (list.next == null) return list; return last(list.next); }
JosWhen people rob a bank they get a penalty; when banks rob people they get a bonus.
- 04-02-2012, 03:33 PM #7
Member
- Join Date
- Apr 2012
- Posts
- 9
- Rep Power
- 0
Re: last element of recursive list
thank you, it works!
- 04-02-2012, 04:31 PM #8
Member
- Join Date
- Apr 2012
- Posts
- 9
- Rep Power
- 0
Re: last element of recursive list
And to find a sublist
i have to use size(list), right?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); }
- 04-02-2012, 04:32 PM #9
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.
- 04-02-2012, 04:36 PM #10
Member
- Join Date
- Apr 2012
- Posts
- 9
- Rep Power
- 0
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
- 04-02-2012, 04:37 PM #11
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.
- 04-02-2012, 04:51 PM #12
Member
- Join Date
- Apr 2012
- Posts
- 9
- Rep Power
- 0
Re: last element of recursive list
org.junit.ComparisonFailure: expected:<[1, ]null> but was:<[]null>
Last edited by rokko; 04-03-2012 at 04:44 AM.
- 04-02-2012, 04:52 PM #13
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.
- 04-02-2012, 04:56 PM #14
Member
- Join Date
- Apr 2012
- Posts
- 9
- Rep Power
- 0
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
- 04-02-2012, 04:57 PM #15
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.
- 04-02-2012, 04:59 PM #16
Member
- Join Date
- Apr 2012
- Posts
- 9
- Rep Power
- 0
Re: last element of recursive list
of course you need the whole test
Last edited by rokko; 04-03-2012 at 04:44 AM.
- 04-02-2012, 05:02 PM #17
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.
- 04-02-2012, 05:06 PM #18
Member
- Join Date
- Apr 2012
- Posts
- 9
- Rep Power
- 0
Re: last element of recursive list
i doubt there is a main method^^
- 04-02-2012, 05:08 PM #19
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.
- 04-02-2012, 05:10 PM #20
Moderator
- Join Date
- Apr 2009
- Posts
- 10,438
- Rep Power
- 16
Re: last element of recursive list
Please do not ask for code as refusal often offends.
Similar Threads
-
ArrayList remove an element from the list
By ralf in forum New To JavaReplies: 37Last Post: 07-12-2011, 07:08 PM -
Recursive Linked List method: RemoveAll
By cjw92 in forum New To JavaReplies: 5Last Post: 03-11-2011, 02:51 AM -
How to access an element of a linked list inside another linked list?
By smtwtfs in forum New To JavaReplies: 4Last Post: 02-21-2011, 09:34 AM -
Recursive File List - Help me problem solve please
By caps_lock in forum New To JavaReplies: 17Last Post: 01-14-2009, 06:44 PM -
adding list to an array element
By Preethi in forum New To JavaReplies: 5Last Post: 09-25-2008, 04:23 AM


LinkBack URL
About LinkBacks
Reply With Quote

Bookmarks