Fill a array with List data with one more element

Posted by marionmaiden on Stack Overflow See other posts from Stack Overflow or by marionmaiden
Published on 2010-05-03T15:01:46Z Indexed on 2010/05/03 15:38 UTC
Read the original article Hit count: 302

Filed under:
|
|
|

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);
    }

© Stack Overflow or respective owner

Related posts about java

Related posts about list