![]() Integer numberArray = (Integer) listOfNumber.toArray() String namesArray = (String) listOfNames.toArray() Let’s see the example of how it will work? import If we do not typecast, the compiler throws the compilation error. Note: The toArray() method returns an array of type Object that is Object.Here Object is topmost class so we need to typecast it. Object numberArray = listOfNumber.toArray() Object namesArray = listOfNames.toArray() import ĪrrayList listOfNumber = new ArrayList() Parameter, It doesn’t take any parameter. ![]() Return type, It returns an array of Object, Here Object is the class that is the topmost class in Java. Let’s see how to convert list to array in java public Object toArray()ĪccessModifier, This method is public so it can be accessed from any class. It retains the same order of elements as in the ArrayList. It returns an array that contains all the elements of the ArrayList. The toArray() method is used to convert an ArrayList to Array in java. In this post we will discuss how we convert ArrayList to array by use of different methods.ģ. There are many methods that allow us to convert ArrayList to array. There may be many situations when we need to Convert list to array in java. ![]() Before move further we should read what is Array and What is ArrayList in Java? ![]()
0 Comments
Leave a Reply. |
AuthorWrite something about yourself. No need to be fancy, just an overview. ArchivesCategories |