#1
  1. No Profile Picture
    Registered User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Apr 2012
    Posts
    5
    Rep Power
    0

    Sublist of a list


    Hey,

    I want to get a sublist of a list

    Code:
    public static List sublist(List list, int index) throws IndexOutOfBoundsException
    	{
    		if (list == null || index < 0 || (size(list) == index)){
    			
    			throw new IndexOutOfBoundsException();
    		}
    	
    		else{
    			if (list.next == null){
    			index = size(list)-1;
    			list.next = sublist(list,index-1);
    			}
    			return list;
    		
    		}
    		
    	}
    a little example:

    my list:

    5, 4, 3, 2, 1

    0, 1, 2, 3, 4 -> Index

    My sublist should f.e. start at index 1 so that the sublist is 4, 3, 2, 1

    when it starts from index 2, then it would be 3, 2, 1, and so on...

    but i dont get it :/
  2. #2
  3. Contributing User
    Devshed Expert (3500 - 3999 posts)

    Join Date
    Aug 2010
    Location
    Eastern Florida
    Posts
    3,735
    Rep Power
    351
    i dont get it
    Can you print out what the program does? Print out the values used and the results.
  4. #3
  5. No Profile Picture
    Registered User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Apr 2012
    Posts
    5
    Rep Power
    0
    i only receive a IndexOutOfBoundsExeption

    the part til:

    throw new IndexOutOfBoundsException seems to be correct, because it passes the 4 junit testcases

    but

    else{
    if (list.next == null){
    index = size(list)-1;
    list.next = sublist(list,index-1);
    }
    return list;

    }

    has a logical failure i think
  6. #4
  7. Contributing User
    Devshed Expert (3500 - 3999 posts)

    Join Date
    Aug 2010
    Location
    Eastern Florida
    Posts
    3,735
    Rep Power
    351
    Can you make a small, simple program that compiles, executes and show the problem?

    To find logic errors, add some println statements that show the values of variables as the code executes. The print out will show you what the computer sees and should help you find the problem
  8. #5
  9. No Profile Picture
    Registered User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Apr 2012
    Posts
    5
    Rep Power
    0
    i cant compile it, i work with eclipse and a junit test

    Code:
    @Test
    	public void sublist()
    	{
    		checkSublist(create(1), 0, "1, null");
    		checkSublist(create(1,2), 0, "2, 1, null");
    		checkSublist(create(1,2), 1, "1, null");
    		checkSublist(create(1,2,3), 0, "3, 2, 1, null");
    		checkSublist(create(1,2,3), 1, "2, 1, null");
    		checkSublist(create(1,2,3), 2, "1, null");
    	}
    the recursive implementation has to pass this test above

    but i dont find the way to get this, because i dont understand how i can return the sublist that starts at a certain index, the interaction of index and the list is my problem

    (sry, that i cant describe it well, my english is not that perfect =( )

    normally you have sublist(int fromIndex, int toIndex), but i have to find a sublist of a list, that starts at an index and give the rest of the whole rest of the list, index is then the head of the sublist

    i know its difficult to help on this way, i am sry, but thanks for your hint
  10. #6
  11. Contributing User
    Devshed Expert (3500 - 3999 posts)

    Join Date
    Aug 2010
    Location
    Eastern Florida
    Posts
    3,735
    Rep Power
    351
    Sorry, if it can't be compiled and tested, there is nothing I can do to help you find the problem.

    Your problem looks like you could create a small simple program that creates a list and then calls the sublist method to return a sublist.
    It should not be hard to make that program. It would not need many other methods or classes.
    Last edited by NormR; April 2nd, 2012 at 04:10 PM.
  12. #7
  13. No Profile Picture
    Registered User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Apr 2012
    Posts
    5
    Rep Power
    0
    i thout junit would do exactly this?

    create a new list and test with my sublist method if my return is equal to the expected return, whic is given in the junit test?
  14. #8
  15. Contributing User
    Devshed Expert (3500 - 3999 posts)

    Join Date
    Aug 2010
    Location
    Eastern Florida
    Posts
    3,735
    Rep Power
    351
    To find logic problems, add some printlns to the code that show you the values of the variables as the program executes. The printed output will show you what the computer sees when it executes the code. That should help you find the problem.
    Of use an interactive debugger to trace through the execution to see what the code is doing.
  16. #9
  17. Contributing User
    Devshed Expert (3500 - 3999 posts)

    Join Date
    Aug 2010
    Location
    Eastern Florida
    Posts
    3,735
    Rep Power
    351
    Can you explain the logic of your method in pseudo code?
  18. #10
  19. No Profile Picture
    Registered User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Apr 2012
    Posts
    5
    Rep Power
    0
    Originally Posted by NormR
    Can you explain the logic of your method in pseudo code?

    i got it

    Code:
    public static List sublist(List list, int index) throws IndexOutOfBoundsException
    	{
    		
    		if (list == null || index < 0 || (size(list) == index))
    		{	
    		throw new IndexOutOfBoundsException();
    		}
    		
    		if (index == 0){
    			return list;
    		}else{
    			if(list.next != null);
    		}
    				
    			return sublist(list.next, index-1);
    			
    	}
    thanks
  20. #11
  21. Contributing User
    Devshed Expert (3500 - 3999 posts)

    Join Date
    Aug 2010
    Location
    Eastern Florida
    Posts
    3,735
    Rep Power
    351
    That code does not look like it will do what you expect.
    Code:
    if(list.next != null);
    The ending ; makes that statement useless.

IMN logo majestic logo threadwatch logo seochat tools logo