Win a copy of Programmer's Guide to Java SE 8 Oracle Certified Associate (OCA) this week in the OCAJP forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Convert a String[] array

 
Peter Simon
Greenhorn
Posts: 14
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi,
A method returns a String[]array.
Could someone tell me how can i can get to the elements of this array, (since I don't know how many elements this array contains cannot write a for loop for this)
Can I convert or cast this array into some sort of a collection(Vector or ArrayList f.e.) so that i can use the methods .size() or .iterator()?
thanks,
Peter

this is my code:
 
Bear Bibeault
Author and ninkuma
Marshal
Pie
Posts: 65118
89
IntelliJ IDE Java jQuery Mac Mac OS X
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
The .length member will give you the number of elements in the array. No conversions are necessary.
hth,
bear
[ December 15, 2002: Message edited by: Bear Bibeault ]
 
Peter Simon
Greenhorn
Posts: 14
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
thanks,
and if wanted to convert because the elements are easyer to return from a collection,
how would I do this?
Peter
 
Arun Boraiah
Ranch Hand
Posts: 233
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
if you want to convert in to collection then you have populate the value of array to collection element.
Example create a ArrayList and populate each value of the array to ArrayList object and from ArrayList you can get collection.
-arun
 
Ilja Preuss
author
Sheriff
Posts: 14112
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
You can simply use the Arrays.asList(Object[]) method.
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic