Visit every couple of element of a generic List in Java only once -


i have list in java, , exploit visit every couple of element once. example if had array have done like:

o[] x = ...; for(int = 0; i<x.length; i++){      for(int j=i+1; j<x.length;j++){           someoperation(x[i],x[j]);      } } 

the problem have list (and suppose don't know if list arraylist or linkedlist). in order have same complexity case listed before, write in pseudo-code, like:

 listiterator<o> it1 = list.listiterator();  while(it1.hasnext()){       o x = it1.next();       it2 = it1.clone();   //it2 have same "status" of it1, different object in memory       while(it2.hasnext()){            y= it.next();            someoperation(x,y);       }  } 

as far know don't have it1.clone(). way similar stuff more or less:

 int = it1.nextindex();  it2 = list.listiterator(i); 

but, far know,

 list.listiterator(i); 

could have complexity of o(n) - in case of linkedlist, , absolutely avoidable in other languages. on other side, implementing same algorithm using random access (like list.get(i)), worst. correct way write code assuming list linkedlist?

can't swap i , j?

list<e> x = ...; int j = 0; (e ej : x) {     int imax = j++;     int = 0;     (e ei : x) {         if (i++ >= imax)             break;         someoperation(ei, ej);     } } 

Comments

Popular posts from this blog

sublimetext3 - what keyboard shortcut is to comment/uncomment for this script tag in sublime -

java - No use of nillable="0" in SOAP Webservice -

ubuntu - Laravel 5.2 quickstart guide gives Not Found Error -