java - ArrayList.indexOf versus sequential searching of an array of objects -


does jvm run through array of objects faster arraylist of objects?

to elaborate, have 2 variables

object theobject = someobject;  object[] objarr; arraylist objarrlist; 

assuming both objarr , objarrlist have same elements, iterating on elements of objarray until find element of interest take same amount of time using objarrlist.indexof(...)

i.e. this:

int length = objarray.length; for(int i=0; i<length; i++){     if(objarray[i].equals(someobject)){       idx = i; break;    } } 

basically same as

objarrlist.indexof(theobject);

or arraylists (and lists in general) have optimized search better simple -go-through-each-element-in-sequence method?

thanks!

the implemntation of arraylist.indexof(object o) copied below

public int indexof(object o) {     if (o == null) {         (int = 0; < size; i++)             if (elementdata[i]==null)                 return i;     } else {         (int = 0; < size; i++)             if (o.equals(elementdata[i]))                 return i;     }     return -1; } 

it same exact thing did on array. (with different checks null).

so jvm run through both in same amount of time.


Comments