tag:blogger.com,1999:blog-694855878384792308.post3385386323853760507..comments2024-03-21T06:26:49.387-07:00Comments on Java67: How to convert an ArrayList to Array in Java? Example javin paulhttp://www.blogger.com/profile/15028902221295732276noreply@blogger.comBlogger5125tag:blogger.com,1999:blog-694855878384792308.post-84545461856018621632015-10-13T11:30:39.898-07:002015-10-13T11:30:39.898-07:00Much easier still: String[] y = x.toArray(new Stri...Much easier still: String[] y = x.toArray(new String[0]); (see http://docs.oracle.com/javase/7/docs/api/java/util/List.html#toArray%28T[]%29)Anonymousnoreply@blogger.comtag:blogger.com,1999:blog-694855878384792308.post-53376721968740964812014-02-11T11:43:26.778-08:002014-02-11T11:43:26.778-08:00Or easier, the code I made:
import java.util.Arra...Or easier, the code I made:<br /><br />import java.util.ArrayList;<br /><br />public class month {<br /> <br /> public static void main(String args[]){<br /> <br /> //ArrayList containing string objects<br /> ArrayList aListMonth = new ArrayList();<br /> aListMonth.add("Jan");<br /> aListMonth.add("Feb");<br /> aListMonth.add("Mar");<br /> aListMonth.add("Apr");<br /> aListMonth.add("Jun");<br /> <br /> String strMnts[]=new String[aListMonth.size()];<br /> strMnts=aListMonth.toArray(strMnts);<br /> for(int i=0;i<strMnts.length;i++)<br /> System.out.print(strMnts[i]+" ");<br /> <br /> }<br /> }Anonymousnoreply@blogger.comtag:blogger.com,1999:blog-694855878384792308.post-14317384102862638642012-09-22T11:25:48.633-07:002012-09-22T11:25:48.633-07:00@jmzc: The object array objMnt is of type Object[]...@jmzc: The object array objMnt is of type Object[] and contains String references, that's why you can downcast a reference in the array. You can store String references in an Object[] because the class String is derived from the class Object and therefore you can substitute objects of type String for objects with type Object.<br /><br />The Object[] itself cannot be downcasted to String[] because String[] is a derived class of Object[]. This means even tough the Object[] object contains references to Strings, the Object[] object itself is _NOT_ a String[] object.<br /><br />But back to the problem: You could also convert the ArrayList of strings in an easier way:<br /><br />String[] strMnts = aListMonth.toArray( new String[aListMonth.size()] );<br /><br />This method exists (AFAIK) because of the exact reason mentioned above. By passing in the pre-allocated array of type String[], the actual return type of toArray is of type String[] and not of type Object[].freakyhttps://www.blogger.com/profile/17327481201251234265noreply@blogger.comtag:blogger.com,1999:blog-694855878384792308.post-9216732484117520142012-09-22T01:58:40.294-07:002012-09-22T01:58:40.294-07:00Your code is not HTML safe use pastebin or stuff l...Your code is not HTML safe use pastebin or stuff like that ! Bharat Vermahttps://www.blogger.com/profile/12817316332686822982noreply@blogger.comtag:blogger.com,1999:blog-694855878384792308.post-86873945797592560332012-09-22T01:43:04.924-07:002012-09-22T01:43:04.924-07:00Thanks for article.
One question:
if each element...Thanks for article.<br />One question:<br /><br />if each element into objMnt is really a Object reference to String instance ( and I can make a downcast such String e = (String)objMnt[0] )<br /><br />Why I cannot make a downcast <br /><br />String[] a = (String[]) objMnt<br /><br />?<br /><br />Arrays are covariant <br /><br />Regardsjmzcnoreply@blogger.com