• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Jeanne Boyarsky
  • Bear Bibeault
  • Knute Snortum
  • Liutauras Vilda
Sheriffs:
  • Tim Cooke
  • Devaka Cooray
  • Paul Clapham
Saloon Keepers:
  • Tim Moores
  • Frits Walraven
  • Ron McLeod
  • Ganesh Patekar
  • salvin francis
Bartenders:
  • Tim Holloway
  • Carey Brown
  • Stephan van Hulst

Parameter to ArrayList constructor?  RSS feed

 
Greenhorn
Posts: 23
Java
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
What is the the difference between :
 

when output is same in both cases.
 
Master Rancher
Posts: 920
17
Firefox Browser Hibernate IntelliJ IDE Java MySQL Database Spring Tomcat Server
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
The first one set's an initial capacity of 5 elements
 
Saloon Keeper
Posts: 2268
290
Android Angular Framework Eclipse IDE Java Linux MySQL Database Redhat TypeScript
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Documentation: ArrayList(int initialCapacity)
 
Shubham Rai
Greenhorn
Posts: 23
Java
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Is there any way to find present capacity of ArrayList?
 
Daniel Demesmaecker
Master Rancher
Posts: 920
17
Firefox Browser Hibernate IntelliJ IDE Java MySQL Database Spring Tomcat Server
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Capacity: , how many elements it contains:
 
Ron McLeod
Saloon Keeper
Posts: 2268
290
Android Angular Framework Eclipse IDE Java Linux MySQL Database Redhat TypeScript
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

Daniel Demesmaecker wrote:Capacity:


Does ArrayList have a field named length?
 
Daniel Demesmaecker
Master Rancher
Posts: 920
17
Firefox Browser Hibernate IntelliJ IDE Java MySQL Database Spring Tomcat Server
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
lol just checked apperently not then:
 
Ron McLeod
Saloon Keeper
Posts: 2268
290
Android Angular Framework Eclipse IDE Java Linux MySQL Database Redhat TypeScript
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

Daniel Demesmaecker wrote:


 
Shubham Rai
Greenhorn
Posts: 23
Java
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

Shubham Rai wrote:Is there any way to find present capacity of ArrayList?



I am asking a way to check capacity of ArrayList not it's size
 
Ron McLeod
Saloon Keeper
Posts: 2268
290
Android Angular Framework Eclipse IDE Java Linux MySQL Database Redhat TypeScript
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
You can (possibly) increase the capacity using ensureCapacity(int minCapacity) or (possibly) reduce the capacity using trimToSize(), but I don't think there is any visibility in to the actual value of the capacity.
 
Daniel Demesmaecker
Master Rancher
Posts: 920
17
Firefox Browser Hibernate IntelliJ IDE Java MySQL Database Spring Tomcat Server
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Have a look at this tutorial
 
Ron McLeod
Saloon Keeper
Posts: 2268
290
Android Angular Framework Eclipse IDE Java Linux MySQL Database Redhat TypeScript
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

Daniel Demesmaecker wrote:Have a look at this tutorial


Sure, you can poke around the internals of a class using reflection, but that may stop working if the ArrayList class is refactored in the future, and is certainly not something that we should be recommending for new learners in the Beginning Java forum.
 
Sheriff
Posts: 12816
211
Android Debian Eclipse IDE IntelliJ IDE Java Linux Mac Spring Ubuntu
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
In all the years I have programmed in Java and used lists, I have never really needed to worry about what the current capacity is. Why do you think it's important to know this?
 
Junilu Lacar
Sheriff
Posts: 12816
211
Android Debian Eclipse IDE IntelliJ IDE Java Linux Mac Spring Ubuntu
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

Shubham Rai wrote:Is there any way to find present capacity of ArrayList?



And by the way, the short answer to your question is no, the ArrayList class does not provide any method that will allow you to query the current capacity. As mentioned already earlier, the best you can do is to call ensureCapacity() to set the minimum capacity.
 
Daniel Demesmaecker
Master Rancher
Posts: 920
17
Firefox Browser Hibernate IntelliJ IDE Java MySQL Database Spring Tomcat Server
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
But as you pointed out earlier, the greatest advantage of an List to me is that you don't have to set it's capacity
 
Shubham Rai
Greenhorn
Posts: 23
Java
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

Junilu Lacar wrote:In all the years I have programmed in Java and used lists, I have never really needed to worry about what the current capacity is. Why do you think it's important to know this?


Nothing important but when you learn something new  a lot of question came to your mind and this question was  of one of them.

Thanks everyone for helping me out!
 
It is sorta covered in the JavaRanch Style Guide.
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!