File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
http://aspose.com/file-tools
The moose likes Java in General and the fly likes Convert ListArray to string array Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Java » Java in General
Bookmark "Convert ListArray to string array" Watch "Convert ListArray to string array" New topic
Author

Convert ListArray to string array

Mark Hughes
Ranch Hand

Joined: Jul 14, 2006
Posts: 146
Hey guys, im not sure how to convert a listarray into a string array

My list is as follows,
java.util.List<String> lines = new ArrayList<String>();

but now i want to put content of lines into a string array
i tried this

String[] textfile = {};
textfile = lines.Toarray();

but it will not compile, some kind of error sytax error on token expecting ;
All help appreciated, thanks
Mark
Adam Nace
Ranch Hand

Joined: Jul 17, 2006
Posts: 117
Originally posted by Mark Hughes:



I'm not sure if this is why you're getting the compile time error, but I see no reason for the array initializer (the open and closing brace brackets on the second line of code. Since you are not supplying any elements, this is unnecessary, and is the equivalent of this:



This means that you are CREATING a new instance of a String array. But then on line 3, you are calling lines.toArray(); (watch the use of capitals, this is not technically the correct name for the method), which assigns textfile to refer to a completely DIFFERENT array, and LOSING the reference to the original array that you created, so the net effect (other than making the garbage collector work harder) is this:

[/QB]

As far as I can tell, this SHOULD compile, so if this doesn't fix your problem, then your problem is probably in some other line of code that you didn't post here.

- Adam
Garrett Rowe
Ranch Hand

Joined: Jan 17, 2006
Posts: 1296
Well you can't be posting your code in full because the code as posted would not produce that error. It would however tell you that the mehtod Toarray() cannot be found in java.util.List. Once you correct that it will inform you of an incompatible return type as the toArray() method in java.util.List that takes no argument returns an Object[] and you are trying to assign it to a String[].
[ July 23, 2006: Message edited by: Garrett Rowe ]

Some problems are so complex that you have to be highly intelligent and well informed just to be undecided about them. - Laurence J. Peter
Mark Hughes
Ranch Hand

Joined: Jul 14, 2006
Posts: 146
Thanks Adam,
I tried that but it said error can not convert onject to a string, is it even possible to convert an arraylist to a string array?
Garrett Rowe
Ranch Hand

Joined: Jan 17, 2006
Posts: 1296
Originally posted by Mark Hughes:
Thanks Adam,
I tried that but it said error can not convert onject to a string, is it even possible to convert an arraylist to a string array?

Naseem Khan
Ranch Hand

Joined: Apr 25, 2005
Posts: 809
Thanks Adam,
I tried that but it said error can not convert onject to a string, is it even possible to convert an arraylist to a string array?


Yes possible.



Read javadoc about <T> T[] toArray(T[] a) method.


<T> T[] toArray(T[] a)

Returns an array containing all of the elements in this collection; the runtime type of the returned array is that of the specified array. If the collection fits in the specified array, it is returned therein. Otherwise, a new array is allocated with the runtime type of the specified array and the size of this collection.
If this collection fits in the specified array with room to spare (i.e., the array has more elements than this collection), the element in the array immediately following the end of the collection is set to null. This is useful in determining the length of this collection only if the caller knows that this collection does not contain any null elements.)


Naseem
[ July 23, 2006: Message edited by: Naseem Khan ]

Asking Smart Questions FAQ - How To Put Your Code In Code Tags
Mark Hughes
Ranch Hand

Joined: Jul 14, 2006
Posts: 146
cool, thanks very much
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Convert ListArray to string array