1

I know that the code to turn an arraylist into an array is:

private String[] arrayLst_to_array(ArrayList<String> al) {
    String[] arr = new String[al.size()];
    arr = al.toArray(arr);
    return arr;
}

But I want my new array to have a certain string in the beginning and then after that, I want the rest of the arraylist.

I know that I could just add the string that I want to the beginning of the arraylist and then convert it, but is there a more efficient way?

6
  • Where do you have the extra strings that you want to add? Do you know how many you have before hand? Commented Oct 2, 2013 at 14:51
  • I just want to add one string. Commented Oct 2, 2013 at 14:52
  • 2
    if you simply have to add one string, then adding it before conversion is better. I takes O(1) to add to arraylist. what more efficient you looking for, to add one string? Commented Oct 2, 2013 at 14:55
  • 2
    @Zeeshan He'll have to add it to the start of the list, which is O(n). Commented Oct 2, 2013 at 14:57
  • 1
    Are you sure this must be "efficient"? If not, go with the easiest to read option. If it must be efficient, please explain if this refers to speed of operation or consumption of memory (or both). The easiest to read will be to insert the string prior to conversion. Commented Oct 2, 2013 at 15:01

4 Answers 4

7

You can use System.arraycopy():

String[] arr = new String[al.size() + 1];
arr[0] = someStr;  // initial string

// copy the list:
System.arraycopy(al.toArray(), 0, arr, 1, al.size());

return arr;
Sign up to request clarification or add additional context in comments.

6 Comments

Thanks, I'm new to java so I didn't think to look in the System docs. I'll accept this in 3 mins (once the timer ends)
Why do you waste so much memory? You're allocating two String arrays here. And you are loosing type-safety checks here because toArray() returns Object[].
@Neet In 99.9% of cases that will be completely and utterly negligible. I dare say that even if the list is as large as possible (~2 billion) there will not be any noticeable difference. I'd rather not convolute the code by doing everything with a single array; using two is much cleared in my opinion.
@arshajii so beeing memory efficient can be neglected? Do you know how often this method has to be called? Each array pollutes the heap and causes more frequent GC which degrades overall performance.
@Neet This is undoubtedly a micro-optimization. If the OP, through extensive testing, arrives at the irrefutable conclusion that this is the source of some bottleneck, he should then think about altering this basic approach and bring into consideration the factors you detailed. Only then.
|
1

A memory efficient but maybe not so well performing solution would be:

public static String[] listPlusOne(final ArrayList<String> list, final String prepend)
{
    final String[] arr = list.toArray(new String[list.size() + 1]);
    System.arraycopy(arr, 0, arr, 1, list.size());
    arr[0] = prepend;
    return arr;
}

This solution allocates only one String array and performs a memory move using System.arrayCopy() to move all the elements one position up.

Generally speaking memory moving is always not the best solution. A LinkedList will allow pretty quick element prepending, but has O(n) complexity when accessing elements at random positions. The ArrayList is slower on prepending (memory moving, reallocation) but has O(1) when accessing elements.

So, either you use something like the code above, or you prepend the element to the list.

Comments

1

If you add an item to the beginning of the list, the contents of the entire list have to be moved one position up. This means each element is touched twice in the entire operation. If you export the list to an array and then use System.arrayCopy to make room for one in the beginning, again each item is touched twice.

The easiest solution that touches each item only once seems to be creating the array, adding the string, and then iterating over the list to add its elements.

String[] arr = new String[al.size() + 1];
arr[0] = someStr;
int i=1;
for (String s: al) {
    arr[i++] = s;
}

Whether this is faster than the approaches that iterate over the items twice but benefit from the efficiency of System.arrayCopy should be shown by benchmarks.

Comments

-2

In Short Answer to your question is No.The Option you gave i believe is the best way to do it.

1 Comment

inserting the string in first and then converting is the fastest right?

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.