java - Convert ArrayList to LinkedList -


if want change implementation arraylist linkedlist have change

arraylist<character> characters = new arraylist<character>(); 

to

linkedlist<character> characters = new list<character>(); 

or how do this?

arraylist<character> characters = new arraylist<character>(); try {     while((str = bufferedreader.readline()) != null){         char [] characterarray = str.tochararray();          (int = 0; <  characterarray.length; i++){              characters.add(characterarray[i]);         }     } } catch (ioexception e) {     e.printstacktrace(); } char [] patternarray = phrase.tochararray(); arraylist<character> pattern = new arraylist<character>(); (int = 0; <  patternarray.length; i++) {      pattern.add(patternarray[i]); } 

this code offers nice illustration why programming interfaces thing. keep using declaration pattern:

arraylist<character> characters = new arraylist<character>(); 

if wish make characters linked list, have make changes in 2 places. if used interface name declaration, this

list<character> characters = new arraylist<character>(); 

the rest of code continue working, able make single change convert linkedlist:

list<character> characters = new linkedlist<character>(); 

same goes declaration of pattern variable.

it slow , uses memory

of course does! long linked lists take more memory, it's impractical use them character-by-character representations. generally, conversions of strings character lists practical when deal small number of short strings. in other situations better off string objects, or stringbuilder objects if need mutability.


Comments