![]() ![]() Spare (i.e., the array has more elements than the collection), theĮlement in the array immediately following the end of the collection If the collection fits in the specified array with room to With the runtime type of the specified array and the size of thisĬollection. If the collection fits in the specifiedĪrray, it is returned therein. The documentation makes it clear that the purpose of the passed array is essentially to define the type of array to return (which is exactly your use case):Įlements in this collection the runtime type of the returned array is ![]() At first this seems like what you're looking for, but it's unclear exactly why you're passing in an array (are you adding to it, using it for just the type, etc). As you can see, this method returns a T where T is the type of the array you pass in. Right below that method, though, is the Javadoc for toArray(T a). (In other words, this method must allocate a new array even if theĬollection is backed by an Array). "safe" in that no references to it are maintained by the collection. Must return the elements in the same order. To what order its elements are returned by its iterator, this method If the collection makes any guarantees as As you can see, this method returns an Object and not String which is an array of the runtime type of your list:Įlements in this collection. The Javadoc for toArray() (no arguments) is here. I'd like to add to the other great answers here and explain how you could have used the Javadocs to answer your question. The correct way to do this is: String stockArr = stock_list.toArray(new String) Or String arr = list.toArray(new String) //if size of array is smaller then list it will be automatically adjusted. So this solution is not perfect.īest solution to this problem is explicitly tell what type of array toArray() should be returned by passing this type as method argument like String arr = list.toArray(new String) Problem with this array is that while compiler would allow you to edit its content by adding new A() element to it, you would get ArrayStoreException because B array can hold only elements of class B or its subclass, to make sure that all elements will support interface of B, but instance of A may not have all methods/fields of B. Now most common type is B, not A so toArray() A arr = elements.toArray() You could say that one way to resolve this problem is to find at runtime most common type between all list elements and create array of that type, but this wont work in situations where all elements of list will be of one type derived from generic one. String arr2 = (String) arr //OK - `arr2` reference will hold same array of same type as ^^^^^^^^ `arr` holds array of subtype of Object (String) If it was, then you could do this: Object objects = new Object //OK - because Otherwise a new array of the same type and a larger size is allocated and returned as the result.)ġ - In Java, an Object is not assignment compatible with a String. (If the supplied array is big enough to hold the list elements, it is used. By contrast, in the other case, the array parameter gives the base type of the array. All it knows is that the raw element type is Object. See for details.)įrom a technical perspective, the reason for this API behavior / design is that an implementation of the List.toArray() method has no information of what the is at runtime. (Surprisingly, it is faster to do this than to preallocate. The latter version uses the zero-length array to determine the type of the result array. Or even String stockArr = stockList.toArray(new String) įor more details, refer to the javadocs for the two overloads of List.toArray. The correct code would be: String stockArr = stockList.toArray(new String) Sun has already provided the AbstractTableModel class for you to extend to make your life a little easier.What is happening is that stock_list.toArray() is creating an Object rather than a String and hence the typecast is failing 1. You can write a custom class that implements the TableModel interface. Instead of converting an ArrayList into an Object, try using the JTable(TableModel) constructor. I presume you are using the JTable(Object, Object) constructor. ![]()
0 Comments
Leave a Reply. |
AuthorWrite something about yourself. No need to be fancy, just an overview. ArchivesCategories |