permaculture playing cards*
The moose likes Beginning Java and the fly likes find sublist Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Java » Beginning Java
Bookmark "find sublist" Watch "find sublist" New topic
Author

find sublist

sam White
Ranch Hand

Joined: Feb 18, 2011
Posts: 208
Hi,

I tired to implement my own version of findSubLit function, but its test is failed.

Here is my implementation:



Test case:



What is wrong with my code?

Thanks
SS
Jesper de Jong
Java Cowboy
Saloon Keeper

Joined: Aug 16, 2005
Posts: 13875
    
  10

Can you explain, in English, how your findSubList method is supposed to find the correct answer? In other words, what is the logic behind your implementation? To ask some concrete questions about it: Why does j start with target.size() and why does the loop run as long as j <= ssize?

Go through the method with the example from the test code in your head, step by step. Keep track of what happens exactly in the method (what are the values of the variables at each step?). That way you'll understand exactly what your method is doing, and you'll see why it is not doing what it is supposed to do.

Java Beginners FAQ - JavaRanch SCJP FAQ - The Java Tutorial - Java SE 7 API documentation
Scala Notes - My blog about Scala
sam White
Ranch Hand

Joined: Feb 18, 2011
Posts: 208
thanks for the suggestion.
The problem is not within the method itself, but in the parameter source and target.

Thanks
SS
Raymond Tong
Ranch Hand

Joined: Aug 15, 2010
Posts: 230
    
    2

sam White wrote:thanks for the suggestion.
The problem is not within the method itself, but in the parameter source and target.

Thanks
SS

You are trying to compare the object reference of subList with that of target List.
But I think you want to compare the elements inside these Lists?
sam White
Ranch Hand

Joined: Feb 18, 2011
Posts: 208
I am not sure if I am reading the correct api manual.
I found the following description about list equals api:

public boolean equals(Object o)
Compares the specified object with this list for equality. Returns true if and only if the specified object is also a list, both lists have the same size, and all corresponding pairs of elements in the two lists are equal. (Two elements e1 and e2 are equal if (e1==null ? e2==null : e1.equals(e2)).) In other words, two lists are defined to be equal if they contain the same elements in the same order. This definition ensures that the equals method works properly across different implementations of the List interface.
Specified by:
equals in interface Collection
Overrides:
equals in class Object
Parameters:
o - the object to be compared for equality with this list.
Returns:
true if the specified object is equal to this list.
See Also:
Object.hashCode(), Hashtable
sam White
Ranch Hand

Joined: Feb 18, 2011
Posts: 208
I found that when use Arrays.asList to convert array to list, it converts the array into a 2 dimensional list
eg.

it converts

int[] subArray = {8,9,10};

into

target[0][0] = 8
target[0][1] = 9
target[0][2] = 10
sam White
Ranch Hand

Joined: Feb 18, 2011
Posts: 208
as it converted the array into 2 dimensional list,
the source.size() and target.size() always returns 1.

sam White
Ranch Hand

Joined: Feb 18, 2011
Posts: 208
I switched back to the indexOfSubList() method from Collections, I found there is something wrong when searching on an empty list (as the sublist).
It returns index 0 which is the first element of the source list.

Here is the test case:



The above test case is failed. indexOfSubList() returns 0 rather than -1.

Here is how I used the method Collections.indexOfSubList():

Raymond Tong
Ranch Hand

Joined: Aug 15, 2010
Posts: 230
    
    2

sam White wrote:I found that when use Arrays.asList to convert array to list, it converts the array into a 2 dimensional list
eg.

it converts

int[] subArray = {8,9,10};

into

target[0][0] = 8
target[0][1] = 9
target[0][2] = 10

I just read the API doc.
Try to use Integer[] instead of int[].
Arrays.asList doesn't work as expected for primitives array because Generics does not apply to primitives.
sam White
Ranch Hand

Joined: Feb 18, 2011
Posts: 208
Thanks very much for the suggestion. that Explained why.

Thanks
s
 
wood burning stoves
 
subject: find sublist
 
Similar Threads
Arrays.asList(...) problem
indexOfSubList in Collections
Comparing values
how to while loop with the condition being multiple elements of an array?
Simple ArrayList conundrum?