# indexOfSubList in Collections

sam White

Ranch Hand

Posts: 229

posted 5 years ago

Hi,

I found the logic of indexOfSubList() in Collections is not so correct, as shown below:

1. if source list is not empty and target list is empty, the function returns 0.

2. if source list and target list is empty , the function returns 0.

if 0 means the first element, the function should returns -1 as per the above two points.

Here is my own implementation to address the above issues:

any suggestion is highly appreciated.

Thanks

S

I found the logic of indexOfSubList() in Collections is not so correct, as shown below:

1. if source list is not empty and target list is empty, the function returns 0.

2. if source list and target list is empty , the function returns 0.

if 0 means the first element, the function should returns -1 as per the above two points.

Here is my own implementation to address the above issues:

any suggestion is highly appreciated.

Thanks

S

posted 5 years ago

An empty list can be found between any two elements of a list, as well as before the first and after the last. This is similar to how there's an empty String between any two characters of any String, as well as before the first and after the last.

Following the above logic, indexOfSubList should return 0 (before the first element) when looking for an empty list. -1 means the empty list cannot be found, and that's certainly not the case.

Following the above logic, indexOfSubList should return 0 (before the first element) when looking for an empty list. -1 means the empty list cannot be found, and that's certainly not the case.

SCJP 1.4 - SCJP 6 - SCWCD 5 - OCEEJBD 6 - OCEJPAD 6

How To Ask Questions How To Answer Questions

Campbell Ritchie

Sheriff

Posts: 50666

83