java - How do i convert array into arraylist and make changes to it that will be reflect in the array? -
i want convert array arraylist.but don't want make new copy of array.i want arraylist reference of array.
for e.g
var arr[] = {"abc","def","ghi"} list templist = new arraylist(); for(string val:arr){ templist.add(val) } for(iterator iterator= templist.listiterator();templist.hasnext()){ string temp = iterator.next(); if(temp == "def"){ templist.remove(temp); } } arr = templist.toarray(templist.size());
now test example of want .and here fist manipulating list converting array ,then replacing "arr" new array list. possible if remove value templist gets removed arr value reference?
you can achieve arrays.aslist
if don't add or remove elements list.
using arrays.aslist(arr)
give list backed array. you'll able change elements stored in list (by calling set(int index, e element)
), , have changes reflected in array. can't add or remove elements, since array has fixed length.
/** * returns fixed-size list backed specified array. (changes * returned list "write through" array.) method acts * bridge between array-based , collection-based apis, in * combination {@link collection#toarray}. returned list * serializable , implements {@link randomaccess}. * * <p>this method provides convenient way create fixed-size * list initialized contain several elements: * <pre> * list<string> stooges = arrays.aslist("larry", "moe", "curly"); * </pre> * * @param array list backed * @return list view of specified array */ public static <t> list<t> aslist(t... a)
Comments
Post a Comment