This week's book giveaway is in the Python forum.
We're giving away four copies of Python Continuous Integration and Delivery and have Moritz Lenz on-line!
See this thread for details.
Win a copy of Python Continuous Integration and Delivery this week in the Python forum!
  • 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
  • Liutauras Vilda
  • Bear Bibeault
  • Paul Clapham
  • Jeanne Boyarsky
Sheriffs:
  • Devaka Cooray
  • Junilu Lacar
  • Tim Cooke
Saloon Keepers:
  • Tim Moores
  • Ron McLeod
  • Tim Holloway
  • Claude Moore
  • Stephan van Hulst
Bartenders:
  • Winston Gutkowski
  • Carey Brown
  • Frits Walraven

Parameter to ArrayList constructor?  RSS feed

 
Ranch Hand
Posts: 37
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: 1161
18
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: 2406
296
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
Ranch Hand
Posts: 37
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: 1161
18
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: 2406
296
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: 1161
18
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: 2406
296
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
Ranch Hand
Posts: 37
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: 2406
296
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: 1161
18
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: 2406
296
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: 13155
219
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: 13155
219
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: 1161
18
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
Ranch Hand
Posts: 37
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!
 
No prison can hold Chairface Chippendale. And on a totally different topic ... my stuff:
Become a Java guru with IntelliJ IDEA
https://www.jetbrains.com/idea/
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!