java - Why does LinkedList.indexOf() return -1 if the object is contained in the list? -


i have declared linkedlist, frame list item.

private linkedlist<frame> linkedlist = new linkedlist<frame>(); 

i realized when test indexof -1 returned though list contains queried item. documentation states "(...) or -1 if list not contain element.".

https://docs.oracle.com/javase/7/docs/api/java/util/linkedlist.html#indexof(java.lang.object)

look @ these strange results:

linkedlist.size() -> 1 linkedlist.get(0) -> frame linkedlist.contains(linkedlist.get(0)) -> false linkedlist.indexof(linkedlist.get(0)) -> -1 

did overlook anything? ideas what's going on?

i accessing list various threads without synchronization. have caused problem?

--

log (see below):

12-05 20:30:00.101  16446-16461/cc.closeup i/system.out﹕ **** test              0 12-05 20:30:00.301  16446-16476/cc.closeup i/system.out﹕ **** test              -1 12-05 20:30:00.856  16446-16461/cc.closeup i/system.out﹕ **** test              0 12-05 20:30:01.051  16446-16476/cc.closeup i/system.out﹕ **** test              -1 12-05 20:30:01.601  16446-16461/cc.closeup i/system.out﹕ **** test              0 12-05 20:30:01.801  16446-16476/cc.closeup i/system.out﹕ **** test              -1 12-05 20:30:02.356  16446-16461/cc.closeup i/system.out﹕ **** test              0 12-05 20:30:02.551  16446-16476/cc.closeup i/system.out﹕ **** test              -1 12-05 20:30:03.101  16446-16461/cc.closeup i/system.out﹕ **** test              0 12-05 20:30:03.301  16446-16476/cc.closeup i/system.out﹕ **** test              -1 

look @ javadocs contains , indexof. states methods determine if element in collection using equals method.

if contains , indexof saying "it isn't there" object present in list different 1 one testing ... according object's implementation of equals(object).


the other possibility accessing / updating collection different threads, , haven't synchronized properly. can lead 1 thread seeing stale or inconsistent version of list.

would think fine if synchronize list itself?

    collections.synchronizedlist(new linkedlist<>()) 

if perform operations via synchronized list , don't use iterator, each individual operation thread-safe , atomic. however:

  • the iterator of "synchronized list" not synchronized, and
  • this doesn't if need sequence of operations synchronized / performed atomically.

for example:

    list<integer> l =  collections.synchronizedlist(new linkedlist<>());      // make list visible other threads ...      (int = 0; < l.size(); i++) {         integer ii = l.get(i);         ...     } 

while l.size() give current size, size might change between l.size() , l.get(i) calls, potentially resulting in exception.

in short ... collections.synchronizedlist(...) not solution thread-safety problems involving lists. still need think doing.


Comments

Popular posts from this blog

python - mat is not a numerical tuple : openCV error -

c# - MSAA finds controls UI Automation doesn't -

wordpress - .htaccess: RewriteRule: bad flag delimiters -