-
converting arraylist to array (6 messages)
- Posted by: Manu DG
- Posted on: September 26 2006 08:18 EDT
hi 2 all am having the problem with converting arraylist to array what i was doin is var nfhEvents =new Array(); <% ArrayList nfhList = (ArrayList)request.getAttribute("holidayList"); String[] stringArray = (String[])nfhList.toArray(new String[nfhList.size()]); %> this is inside the java sctript what i wanted is the values in the holidayList should be assigned to nfhEvents . am new to this programming language please help me, is am going in correct way, guide me.. Thank uThreaded Messages (6)
- Re: converting arraylist to array by Bhagvan Kommadi on September 26 2006 18:49 EDT
- Re: converting arraylist to array by Manu DG on September 27 2006 00:14 EDT
-
Re: converting arraylist to array by arijit dey on September 27 2006 01:59 EDT
-
Re: converting arraylist to array by Manu DG on September 27 2006 04:04 EDT
- Re: converting arraylist to array by arijit dey on September 28 2006 10:15 EDT
- Re: converting arraylist to array by Rene Zanner on October 09 2006 10:31 EDT
-
Re: converting arraylist to array by Manu DG on September 27 2006 04:04 EDT
-
Re: converting arraylist to array by arijit dey on September 27 2006 01:59 EDT
- Re: converting arraylist to array by Manu DG on September 27 2006 00:14 EDT
-
Re: converting arraylist to array[ Go to top ]
- Posted by: Bhagvan Kommadi
- Posted on: September 26 2006 18:49 EDT
- in response to Manu DG
Here is the code : <script type="text/javascript"> var nfhEvents =new Array(); <% ArrayList nfhList = (ArrayList)request.getAttribute("holidayList"); String[] stringArray = (String[])nfhList.toArray(new String[nfhList.size()]); for (i=0;i< stringArray.length;i++) { %> nfhEvents[<%= i%>] = <%= stringArray[i] %>; <% } %> </script> Bhagvan K http://www.jroller.com/page/bhagvank -
Re: converting arraylist to array[ Go to top ]
- Posted by: Manu DG
- Posted on: September 27 2006 00:14 EDT
- in response to Bhagvan Kommadi
thank you bhagvan but when i use the method you told i wil be getting the error say root cause java.lang.ArrayStoreException java.lang.System.arraycopy(Native Method) java.util.ArrayList.toArray(ArrayList.java:305) can you tel me why am getting this error: thank you -
Re: converting arraylist to array[ Go to top ]
- Posted by: arijit dey
- Posted on: September 27 2006 01:59 EDT
- in response to Manu DG
Hi Manu, You got an ArrayStoreException because when you called ArrayList.toArray()and passed String[] as the argument, the runtime will try to copy the objects in the list to the array u specified. If there is a type mismatch, you get ArrayStoreException. Look at the toArray method of ArrayList: public Object[] toArray(Object a[]) { if (a.length < size) a = (Object[])java.lang.reflect.Array.newInstance( a.getClass().getComponentType(), size); System.arraycopy(elementData, 0, a, 0, size); if (a.length > size) a[size] = null; return a; } Now in the System.arraycopy method , the doc says: * @exception IndexOutOfBoundsException if copying would cause * access of data outside array bounds. * @exception ArrayStoreException if an element in the src * array could not be stored into the dest array * because of a type mismatch. * @exception NullPointerException if either src or * dest is null. So you can see that you have some other object type in your ArrayList and your paasing a different array type as argument. check what type of object your ArrayList contains. cheers, http://javaicillusion.blogspot.com/ -
Re: converting arraylist to array[ Go to top ]
- Posted by: Manu DG
- Posted on: September 27 2006 04:04 EDT
- in response to arijit dey
Thank you, but while seting values to the Arraylist i done like this while(rs.next()){ String[] arr=rs.getString("date").split("-"); String holiday=arr[0]+arr[1]+arr[2]; dab.setNfh(holiday); holidayList.add(dab); } so i think am doin correct way, any wrong there plz help... -
Re: converting arraylist to array[ Go to top ]
- Posted by: arijit dey
- Posted on: September 28 2006 10:15 EDT
- in response to Manu DG
You do one thing, In order to ensure that you dont copy wrong type from ArrayList to Array, you can do this: Iterate over all the elements of the ArrayList, call getClass() on each element and print it. You will know what you ArrayList contains.If its String and your are copying to String Array, then it should not give ArrayStoreException. cheers, http://javaicillusion.blogspot.com/ -
Re: converting arraylist to array[ Go to top ]
- Posted by: Rene Zanner
- Posted on: October 09 2006 10:31 EDT
- in response to Manu DG
Sorry, but that is not correct. You put the String into an attribute of an object called "dab". Whatever this object's type is, it's definitely not java.lang.String, because you call dab.setNfh(String), and java.lang.String does not have a method called "setNfh". The object "dab" is not created inside the for loop, so I cannot tell which type it has - I'm assuming the name "DAB". Maybe you wanted to do something like this (I'm using Bhagvan K's example here - slightly modified to be compilable):<script type="text/javascript"> var nfhEvents =new Array(); <% ArrayList nfhList = (ArrayList)request.getAttribute("holidayList"); for (int i = 0; i < nfhList.size(); i++) { DAB dab = (DAB) nfhList.get(i); %> nfhEvents[<%= i %>] = '<%= dab.getNfh() %>'; <% } %> </script>
I'm afraid to tell you that there's possibly more wrong with your code, but without the complete source code I cannot tell more... Cheers, René