The Artima Developer Community
Sponsored Link

Java Buzz Forum
Array length vs ArrayList Size in Java

0 replies on 1 page.

Welcome Guest
  Sign In

Go back to the topic listing  Back to Topic List Click to reply to this topic  Reply to this Topic Click to search messages in this forum  Search Forum Click for a threaded view of the topic  Threaded View   
Previous Topic   Next Topic
Flat View: This topic has 0 replies on 1 page
Javin Paul

Posts: 1090
Nickname: javinpaul
Registered: Jan, 2012

Javin Paul is Java Programmer working on Finance domain.
Array length vs ArrayList Size in Java Posted: Apr 14, 2014 7:07 AM
Reply to this message Reply

This post originated from an RSS feed registered with Java Buzz by Javin Paul.
Original Post: Array length vs ArrayList Size in Java
Feed Title: Java67
Feed URL: http://www.java67.com/feeds/posts/default?alt=rss
Feed Description: Java and technology tutorials, tips, questions for all programmers.
Latest Java Buzz Posts
Latest Java Buzz Posts by Javin Paul
Latest Posts From Java67

Advertisement
One of the confusing part in learning Java for a beginner to understand how to find length of array and ArrayList in Java? Main reason for confusion is inconsistent way of calculating length between two. Calling size() method on arrays and length, or even length() on ArrayList is common programming error made by beginners. Main reason of confusion is special handling of array in Java.  Java native arrays has built-in length attribute but no size() method while the Java library containers, known as Collection classes e.g. ArrayList<>, Vector<>, etc,  all have a size() method. There is one more thing which adds to this confusion, that is capacity, at any point capacity of any collection class is the maximum number of elements collection can hold. size of collection must be less than or equal to its capacity. Though in reality, collection resize themselves even before it reaches its capacity, controlled by laod factor.  I have mentioned this before on my post difference between ArrayList and Array in Java, if you not read it already, you may find some useful detail there as well. So, use length attribute to get number of elements in a array, also known as length, and for same thing in Collection classes e.g. ArrayList, Vector, use size() method. To give you more context, consider following lines of code, can you spot the error, which is bothering our beginner friend:
Read more »

Read: Array length vs ArrayList Size in Java

Topic: A Docker Maven Plugin for Integration Testing Previous Topic   Next Topic Topic: Apache Olingo is now a top-level project

Sponsored Links



Google
  Web Artima.com   

Copyright © 1996-2019 Artima, Inc. All Rights Reserved. - Privacy Policy - Terms of Use