简体   繁体   中英

How do i convert array into arraylist and make changes to it that will be reflect in the array?

I want to convert an array into Arraylist.But i don't want to make a new copy of that array.I want that Arraylist to be reference of that array.

For eg

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 this is a test example of what i actually want to do .And here i am fist manipulating the list then converting it into array ,then replacing the "arr" with new array from the list. But is it possible that if i remove a value from the templist then it gets removed from arr like value by reference?

You can achieve that with Arrays.asList if you don't add or remove elements from the List.

Using Arrays.asList(arr) will give you a List backed by that array. You'll be able to change the elements stored in the List (by calling set(int index, E element) ), and have the changes reflected in the array. But you can't add or remove elements, since the array has a fixed length.

/**
 * Returns a fixed-size list backed by the specified array.  (Changes to
 * the returned list "write through" to the array.)  This method acts
 * as bridge between array-based and collection-based APIs, in
 * combination with {@link Collection#toArray}.  The returned list is
 * serializable and implements {@link RandomAccess}.
 *
 * <p>This method also provides a convenient way to create a fixed-size
 * list initialized to contain several elements:
 * <pre>
 *     List&lt;String&gt; stooges = Arrays.asList("Larry", "Moe", "Curly");
 * </pre>
 *
 * @param a the array by which the list will be backed
 * @return a list view of the specified array
 */
public static <T> List<T> asList(T... a)

What you can do is write a method that updates an ArrayList from an array (which would be the parameter). The method would update ArrayList so that it would hold the values of the array passed in. To give it the illusion that both the array and the ArrayList are "in sync", you would have to call this method each time you have changed the original array. To optimize the method, I would give it two parameters: the array and the also the ArrayList. Because an ArrayList is an object, it would not need to be returned from the method because you would actually be changing he object itself in the method. Just make sure that you initialize the ArrayList before passing it to the method (only for the first time though). Here's a quick implementation of this idea:

private void updateArrayList(String[] arr, ArrayList<String> alist) {
    for(int i = 0, n < arr.length; i < n; i++) {
        alist.set(i, arr[i]);
    }
}

If I made a mistake, please comment, and ask me to clarify if I wasn't clear in certain parts of the answer. Also, tell me if this answer helps or not, or even if I interpreted the question correctly.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM