Win a copy of The Little Book of Impediments (e-book only) this week in the Agile and Other Processes forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

ArrayList function toArray Dimension of Array?

 
Ben Pittens
Greenhorn
Posts: 29
1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hello,

I have a question about the length(number of slots) of an array when it is created through a list.toArray.



After this I have an array str4 with 4 elements "Dog", "Cat", "Monkey", "Elefant".
For me that is strange because I dimensioned it with "new String[1]" as a parameter of the toArray function !
I should excpect that only the "Dog" is in the array because the length of the str4 array is 1.

What use it has then to set a dimension parameter like in new String[1] ?
Does that parameter mention something else?


Kind Regards
Ben

 
Maneesh Godbole
Saloon Keeper
Posts: 11331
16
Android Eclipse IDE Google Web Toolkit Java Mac Ubuntu
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
From the API docs for List#toArray(T[] a)
Parameters:
a - the array into which the elements of this list are to be stored, if it is big enough; otherwise, a new array of the same runtime type is allocated for this purpose.

Since your animalList container more elements, a new array was allocated by the JVM
 
Roel De Nijs
Sheriff
Posts: 10662
144
AngularJS Chrome Eclipse IDE Hibernate Java jQuery MySQL Database Spring Tomcat Server
  • Likes 2
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Ben Pittens wrote:After this I have an array str4 with 4 elements "Dog", "Cat", "Monkey", "Elefant".
For me that is strange because I dimensioned it with "new String[1]" as a parameter of the toArray function !
I should excpect that only the "Dog" is in the array because the length of the str4 array is 1.

What use it has then to set a dimension parameter like in new String[1] ?
Does that parameter mention something else?

The array parameter will only be used to store the elements of the list if the array is big enough. If it isn't, a new array will be created of the same type as the array parameter and with the same number of elements as the list. That's already explained in Maneesh's post (and in the javadoc of the toArray(T[] a) method of Collection/List).

You could use any reference type for the array, but this type should of course be compatible with the elements in the list. So if you would try to execute this statementyou'll get a runtime ArrayStoreException because String IS-NOT-A Integer. But this statement will execute normally (because String IS-A CharSequence)

The following code snippet illustrates all the aboveOutput:
arr1: [Dog, Cat, Monkey, Elefant]
same array? false

arr2: [Dog, Cat, Monkey, Elefant]
same array? true

arr3: [Dog, Cat, Monkey, Elefant, null, <Animal>, <Animal>, <Animal>, <Animal>, <Animal>]
same array? true


Now you can easily see that when the array parameter is big enough, the array is used to store all elements from the list (and that's why "same array? true" is printed). And if the array has more elements than the list, the element in the array immediately following the end of the list is set to null (as you can see from the contents of arr3).

Hope it helps!
Kind regards,
Roel
 
Ramya Subraamanian
Ranch Hand
Posts: 178
17
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Sorry to intrude. But arr[3] contains only <animal> right . How does this change the value of arr[3] to the contents of the array that is created in toArray

 
Roel De Nijs
Sheriff
Posts: 10662
144
AngularJS Chrome Eclipse IDE Hibernate Java jQuery MySQL Database Spring Tomcat Server
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Ramya Subraamanian wrote:But arr[3] contains only <animal> right . How does this change the value of arr[3] to the contents of the array that is created in toArray

Say what? There is no arr[3] in the code snippet. You have arr3 and you could even have arr3[3]. So it's very hard to know what you are asking.

But every array created with the createArray() method has length elements and each element is set to "<Animal>". So this applies to arr1 (1 element), arr2 (4 elements), and arr3 (10 elements). The toArray(T[] a) method will create a new array if the array parameter is not big enough to hold all elements of the list. That's illustrated with arr1 in the code snippet: its length is 1 and that's not big enough to hold 4 elements, so a new array is created to hold all list elements. When the array is big enough (as with arr2 and arr3 in the code snippet), there's no need to create another array and the list elements are set to the specified array. If the specified array has more elements than the list, the element in the array immediately following the end of the list is set to null.
 
Ramya Subraamanian
Ranch Hand
Posts: 178
17
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
i am sooo sorry !!! i meant arr3[]

i print the contents of arr3[] before and after the toArray. The contents of arr3[] is <animal> before . but gets modified to the new array that is created in animalList.toArray(arr3). We are creating a new array by passing arr3, as a parameter, why would that modify the content of arr3.

 
Roel De Nijs
Sheriff
Posts: 10662
144
AngularJS Chrome Eclipse IDE Hibernate Java jQuery MySQL Database Spring Tomcat Server
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Ramya Subraamanian wrote:The contents of arr3[] is <animal> before . but gets modified to the new array that is created in animalList.toArray(arr3). We are creating a new array by passing arr3, as a parameter, why would that modify the content of arr3.

I think you should re-read my previous post again but very carefully this time. Especially this part
Roel De Nijs wrote:When the array is big enough (as with arr2 and arr3 in the code snippet), there's no need to create another array and the list elements are set to the specified array.
And if you are still unsure, have a look at the output of the ToArray class, especially these two line:
arr3: [Dog, Cat, Monkey, Elefant, null, <Animal>, <Animal>, <Animal>, <Animal>, <Animal>]
same array? true


The line in bold proves that the arr3 and the array returned by the toArray(T[] a) method are the same array. This can only be the case if the toArray(T[] a) method does not create a new array (as you suggests).
 
Ben Pittens
Greenhorn
Posts: 29
1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi Roel and Ramya,

Nice to see those examples, now it is clear to me how the list.toArray() method works.
Thanks
Kind Regards
Ben
 
Winston Gutkowski
Bartender
Pie
Posts: 10571
64
Eclipse IDE Hibernate Ubuntu
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Ben Pittens wrote:Nice to see those examples, now it is clear to me how the list.toArray() method works.

Great! I would just add however, that you should rarely, if ever, have a need to convert a List into an array, except possibly for legacy code - especially in these halcyon days of version 8.

Why? Because ArrayList.get(n) takes about a nanosecond longer to run on most machines - ie, the amount of time it takes to call the method - ArrayList.set(n, newValue) perhaps two or three; but the advantages you get from having a List far outweigh the fact that it's microscopically slower.

And you get to use generics too.

Winston
 
Roel De Nijs
Sheriff
Posts: 10662
144
AngularJS Chrome Eclipse IDE Hibernate Java jQuery MySQL Database Spring Tomcat Server
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Winston Gutkowski wrote:I would just add however, that you should rarely, if ever, have a need to convert a List into an array, except possibly for legacy code - especially in these halcyon days of version 8.

That's definitely true! But for the OCA (and OCP) certification exam you still need to know how this method works (ArrayList is one of the exam topics). And "I don't know this method because Winston said I should rarely, if ever, use it" is probably not one of the possible answers
 
Winston Gutkowski
Bartender
Pie
Posts: 10571
64
Eclipse IDE Hibernate Ubuntu
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Roel De Nijs wrote:And "I don't know this method because Winston said I should rarely, if ever, use it" is probably not one of the possible answers

Touché.

@Ben: Roel's quite right. Forget my misoracle ramblings until after you've passed the exam.

Winston
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic