• Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

find sublist

 
sam White
Ranch Hand
Posts: 229
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 15495
43
Android IntelliJ IDE Java Scala Spring
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.
 
sam White
Ranch Hand
Posts: 229
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 255
2
IntelliJ IDE Java Spring
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 229
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 229
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 229
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
as it converted the array into 2 dimensional list,
the source.size() and target.size() always returns 1.

 
sam White
Ranch Hand
Posts: 229
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 255
2
IntelliJ IDE Java Spring
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 229
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thanks very much for the suggestion. that Explained why.

Thanks
s
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic