1

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 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 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?

Neel
  • 428
  • 1
  • 4
  • 12
  • You can't do that. When you create another ArrayList, the value is copied to another data structure and modifications don't reflect to the array. You can use `Arrays.asList()` as stated in the answer. – mtyurt May 14 '15 at 07:55
  • `temp == "def"` This is not how you should compare Strings. [How do I compare strings in Java?](http://stackoverflow.com/q/513832) – Tom May 14 '15 at 07:57
  • @Tom .Yeah sorry,i was in a hurry so quickly wrote == instead of equals to provide an example. – Neel May 14 '15 at 09:39

2 Answers2

4

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)
Eran
  • 387,369
  • 54
  • 702
  • 768
  • Is this possible also for an Array of objects? – Neel May 14 '15 at 08:08
  • @Neal It is possible only for an array of objects. It doesn't work with an array of primitives (as it produces a List with a single element - the entire array). – Eran May 14 '15 at 08:10
  • Oh ok. Thanks for the information.Although i have to find another way to remove the object or stick to transferring the list back to array.:) – Neel May 14 '15 at 09:41
0

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.

jake
  • 124
  • 1
  • 10