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

Popular posts from this blog

Change php variable from jquery value using ajax (same page) -

Pull out data related to my apps from Android Play Store and iOS App Store -

How can I fetch data from a web server in an android application? -