• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

Convert ListArray to string array

 
Ranch Hand
Posts: 146
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
 
Ranch Hand
Posts: 117
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

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
 
Ranch Hand
Posts: 1296
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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 ]
 
Mark Hughes
Ranch Hand
Posts: 146
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
Posts: 1296
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

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?


 
Ranch Hand
Posts: 809
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

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 ]
 
Mark Hughes
Ranch Hand
Posts: 146
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
cool, thanks very much
 
Eliminate 95% of the weeds in your lawn by mowing 3 inches or higher. Then plant tiny ads:
a bit of art, as a gift, that will fit in a stocking
https://gardener-gift.com
reply
    Bookmark Topic Watch Topic
  • New Topic