Discussions

General J2EE: converting arraylist to array

  1. converting arraylist to array (6 messages)

    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 u

    Threaded Messages (6)

  2. Re: converting arraylist to array[ Go to top ]

    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
  3. Re: converting arraylist to array[ Go to top ]

    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
  4. Re: converting arraylist to array[ Go to top ]

    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/
  5. Re: converting arraylist to array[ Go to top ]

    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...
  6. Re: converting arraylist to array[ Go to top ]

    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/
  7. Re: converting arraylist to array[ Go to top ]

    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é