views:

77

answers:

5

Hello,

By a question that I made, I figured out that tho copy elements from one list to an array I just need to use the method toArray() for this.

But let's suppose I have a List with n objects. I want to copy then into a array sized n+1 and add into the first position another object and in the other n positions the n data of the list.

This is the way I'm doing it for now, but I'm just wondering if there is a better way for do that:

    Object array[] = new Object[list.size() + 1];

    Object chk = new Object();

    array[0] = chk;

    for(int i = 1; i < array.length; i++){
        array[i] = list.get(i);
    }
A: 

If you're looking for a way to avoid having to loop through the array and the list, I think you're out of luck. Java doesn't provide a way to do a mass copy except for ones that just use a API call that does the looping through the collection behind the scenes, hidden from view.

Paul Tomblin
+2  A: 

You could used a LinkedList and use offerFirst() and then toArray(), but I doubt it really matters.

Frank Meulenaar
Where is this offerFirst method from?
marionmaiden
`offerFirst(E)` is a new funtion in Java 1.6 (http://java.sun.com/javase/6/docs/api/java/util/LinkedList.html#offerFirst%28E%29) which adds a given element at the start of the list. It has the same effect on the list as `addFirst(E)` and `add(0, E)`.
Christian Semrau
It seems that offerFirst is only available in the LinkedLists. The other implementations of list interface doesn't have this method.
marionmaiden
A: 

The only way to do this without your loop would be:

Object array[] = new Object[list.size() + 1];
Object oldValues[] = list.toArray();

Object chk = new Object();
array[0] = chk;
System.arrayCopy(oldValues, 0, array, 1, oldValues.length);

System.arrayCopy is a bit faster than looping (not too much, but faster nevertheless), although the performance boost would probably be mitigated by the toArray, depending on the implementation of your List.

All in all, what you have is a decent approach as long as List.get(int) is an acceptable cost. (If it's O(n), you'll end up with O(n^2) for your operation, which is suboptimal when Frank's solution is O(n).)

glowcoder
That's not the ONLY way, please see above
Chepech
+2  A: 

use an iterator:

...

int i = 1;
for(Object item:list){
    array[i] = item;
    i++;
}
Tim Bender
+1: Iteration is more efficient than the `get(i)` solution for some kinds of lists, like `LinkedList`. And this solution does not change the list, unlike some other answers given here.
Christian Semrau
+1  A: 

I didn't understood what you are trying to achieve with that, but if I understood the problem correctly, this is how I would do it:

    List<Object> elementList = new ArrayList<Object>();
    Object additionalElement = new Object();
    Object array[] = null;

    //[Add code to populate the List]

    //Add the additional element
    elementList.add(0,additionalElement);

    array = elementList.toArray();

This would do the trick with the advantage of not iterating anything.

Chepech
But would't this add method change the element located in the position 0?
marionmaiden
This approach would change the list: It adds a new element at position 0, moving all other elements by one place (position 0 to position 1 etc.).
Christian Semrau
Thanks @Christian. I didn't know this add implementation shfted up the indices of the subsequent values.
marionmaiden
The `set` method overwrites the element at the given index instead of inserting it.
Christian Semrau