• Register
Welcome to Kodlogs, programming questions and answer website.
0 votes
What is the max size of ArrayList in Java? How do you clear an ArrayList in Java? Can we give size to ArrayList? What is the default size of ArrayList in Java?
by (1.6k points)  
reshown by

5 Answers

0 votes

length of ArrayList in Java: 

ArrayList is a default implementation of the list interface. As with any implementation of list, you can

have duplicate elements in your ArrayList and you can go from element to element in

the same order as the elements were inserted. As it is based on arrays, ArrayList provides

fast access but inserting or removing an element at a random position requires more time as

this will require reorganizing the list. Fast access, however, is crucial for most applications

which is why ArrayList is the most commonly used collection. To store data that changes

frequently, however, consider using an alternative container, for example LinkedList. 

let me introduce

you to two different terms which are important to understand in context with ArrayList – size

and capacity. Size is the number of elements the ArrayList currently holds. For every element

you add to the list, the size will grow by one. Capacity, however, is the number of elements

the currently underlying array can hold. The capacity of the ArrayList grows in intervals.

by (9.8k points)  
0 votes

Java.util.ArrayList.size() Method

The ArrayList starts with an initial capacity. Every time you exceed the capacity of the

array, the ArrayList copies the data over to a new array that is about 50% larger than

the previous one. Let’s say you want to add 100 elements to an ArrayList of an initial

capacity of 10. As the list grows, the system will create 6 more arrays to take the place

of the first. First, one array that can hold 15 elements, then one for a maximum of 22

elements, then arrays with the capacity of 33, 49, 73 and finally, 109 elements to hold

the growing list. These restructuring arrangements can negatively impact performance.

You can instantly create an array of the correct size to minimize these merging activities

by defining the correct capacity at creation time. In case you don’t know the final size

of the ArrayList at creation time, estimate it as close possible. Choosing too large capacity,

however, can also negatively impact performance so choose this value carefully. I advise you

to always explicitly set the capacity at creation time as it documents your intentions.

by (9.8k points)  
0 votes

ArrayList size() method in Java with Examples

most projects you won’t have to worry about optimizing performance to too powerful hardware
but this is no excuse for sloppy design and poor implementation. Here you can see a simplified
extract of the class ArrayList. Keep in mind the real class looks a bit more complicated.
This is just meant to give you a more concrete idea of what the class ArrayList looks like.
As you can see, ArrayList is just a class anyone could have written given enough time
and knowledge. There is no black magic. You can find the actual source code online. However,
don’t rely too much on internals that you spot on the source code as they may change
any time if they are not defined in the Java language specification. Default capacity is
the initial size of the array when you don’t specify it as I recommended before. ElementData
is the array used to store the elements of the ArrayList. Int size is the number of elements
the ArrayList currently holds. Get, add and remove are some of the many functions ArrayList
provides. We will look at those methods now. So let me give you a short overview of the
methods of the ArrayList class. To make things easy for you, I have broken up the overview
into methods belonging to the java.util.Collection interface and methods belonging to the java.util.List
interface. When I talk about other containers in later episodes implementing the list, on
the collection interface I will refer back to this episode instead of repeating myself
over and over again. Make sure you are subscribed to my YouTube channel so you will be informed
when I release episodes about these classes also.
Okay, so let’s start with the methods belonging to the java.util.Collection interface. The
contract of the collection interface does not guarantee any particular order and therefore
does not provide any index or order-related methods. So here you can see the first set
of methods that implement the collection interface. So what I say about these methods does not
only apply to ArrayList but also to all classes that implement the collection interface. The
method Boolean add appends the element to the end of the collection to the next empty
cell of the underlying array. Boolean addAll appends all given elements to the end of the
collection. The stuff in angle brackets is related to generics. In short, it ensures
that no one can call such a method with the wrong arguments. From now relax, I will tell
you more in my upcoming episode about generics. Boolean remove removes the first occurrence
of the element you specify from the collection. Boolean removeAll removes the given elements
from the collection. The iterator method returns an object you
usually use in a loop to move from one element to the next element of the collection step
by step. 
by (9.8k points)  
0 votes

find the length of an array list: 


You say I iterate over the collection hence the name iterator. Int size returns

the number of elements of the collection. Boolean contains returns true if the collection

contains at least one instance of the element you specify.

Void clear removes all elements from the collection. Boolean isEmpty, this returns true if the

collections contains no elements. And toArray returns an array containing all of the elements

of the collection. Alright, let’s move on to the methods of

the java.util.List interface. The methods are similar in part to the methods we’ve

just looked at but they differ in that they require an order on the elements of the list.

So here again you should know that everything I say about these methods does not only apply

to ArrayList but to all classes that implement the list interface. This add method with an

index parameter acts actually more like an insert method. It allows you to insert an

element at any index position of the list, instead of just adding the element to the

end of the list. In the process, the elements of the underlying array will be shifted to

the right and migrated to a larger array if necessary. The remove index method allows

removing an element from any index position of the list. Similar to the add method we

just looked at, this might require to shift the remaining elements of the underlying array

to the left. The get by index method returns an element from any given position of the

list. The indexOf method takes an object and returns the index of the first occurrence

of the element in the list or minus one if the element is not found. Int lastIndexOf

returns the index of the last occurrence of the element in the list and as before, minus

one if the element is not found. List sublist returns a view of the list starting

with the position you specify as fromIndex and ending one position before the one you

specify as toIndex. Last but not least, the sort method sorts the list following the order

of the given comparator. So let’s end the lecture session. Time to

move on to some practical coding. So now I’m in my IDE and you can see I have prepared

a little test method so we can do some practical coding with ArrayList. I have already created

an instance of ArrayList and I’ve assigned it to a reference variable of type collection.

Now this might be a bit counterintuitive at first because why is that not ArrayList or

at least list, well there is a reason to it and let me cite Robert C. Martin, he says,

“A good architecture is one that maximizes the number of decisions not made or the number

of decisions deferred.” And this is such a situation because by making this collection,

you would create this instance for a reason of course and you probably would forward it

to either other methods and/or other objects so now if you make this an ArrayList, this

will really limit all your other objects because they all only have to work on an ArrayList

but now the requirements of your business… requirements usually they change very fast

as you know and if they change like for example an ArrayList can hold duplicate elements but

now there might be a new requirement that tells you, you cannot have duplicate elements,

you have to prevent that you have duplicate elements, so you might want to use HashSet

here. Now if you use list or ArrayList everywhere in the code, it will be very tough to change

your mind. You will have to touch a lot of code which is very dangerous but if this is

a collection, a collection can be so, so many things and so it will be much easier to change

your mind because all you will have to do is change the instantiation of the object

that is actually being used. So if you can, like of course this limits the number of functions

that you can use, because the collection interface is less powerful than the list or the ArrayList

by (9.8k points)  
0 votes

How To Find Length/Size Of An ArrayList In Java With Example

And then we can also print the ArrayList as
it is now. There are two different ways. ArrayList has also a toString method so I could directly
say System.out.println(elements) but I am doing it a bit more complicated because I want to show you how we actually iterate
over the ArrayList and this is because ArrayList and every collection implements the iterable
interface which is used in this new for each loop and so we can iterate over the ArrayList
by saying String str and then colon and then elements. And what is required to iterate
over this list will be done here inside and you don’t see it. This is all done by the
compiler for us internally. In short I can tell you there is an integrator method that
I showed you shortly before which will then be used. You might also check my last episode
where I introduced you to the iterable and the interator interface, both will be used
here. For now, it’s enough if you see you can use the ArrayList in the for each loop
and now we can print out each element separately, which of course gives us a bit more flexibility
like I could print something else here also. So this is called iteration going over the
collection element by element and I also want to show you, which is a feature of list, this
order how we added the elements A, B, A, C, E, E will be also exactly the same order as
the elements I printed out which would be different if we had a set.
But enough of talking, let’s already execute it and see it in action. So here you see A,
B, A, C, E, E. Exactly like we added that, which is a feature of list. Now that we added
elements, we could also remove some elements which are also just as easily removed. And
now on the collection interface, I can’t remove by index but I can remove by stating
the object that I want to be removed which by the way internally uses the equals method.
If you don’t know about the equals method, I also have a video about the equals method.
So it goes over each element and checks by using equals which is implemented in the string
class if this A is equal to one of those elements here and we’ll then remove it. You might
also notice I have two A’s, so now it’s interesting to know will the first A be removed,
will the second A be removed or will both be removed. And there is another tip I have
for you, if you have such questions, you can look for a book, for a video tutorial, you
can look in the API and so on and so forth, but you will never be as sure and probably
also never be as fast if you just write a simple test like this and you just try it
out for yourself. How pragmatic? How cool is that? Because the code doesn’t lie, if
you see it, you will be assured that it behaves like you can see it. So let’s do this and
check which element is removed. And here you see B, A, C, E, E, so the first A is missing,
so the answer is the first element is removed, not all A’s are removed.
So what else can we do? Let’s check! We can also ask for the size. Let’s make this
a bit simpler now to have it as short as possible. So now we have one, two, three, four elements.
I’ll copy that. Copying some code that you will reuse is just safer not to introduce
any typo. And as long as you are just copying the code and not duplicating the code, it’s
also fine. So this supposed to give us the number of elements. Let me document this out
so we just see the number of elements. Let’s also do it directly after instantiation of
the object so I expect here to see a zero, no elements included. I mean, this would be
another alternative possibility that this would throw an exception if the list is empty
so let’s check that. I execute the test. It was actually not really a test I am just
experimenting. So you see it returns zero in the first line and then four, so this is
the size method. You can even ask are you empty? Which should probably here say yes,
true, and here probably false. And there it is, true and false.
So let’s now switch over to the list interface and let’s also play a bit around with that.
You will see I have now a reference variable of type list and I can use all the methods
exactly like I used them before because the list interface extends the collections interface
so it includes all the methods that the collection interface has, but on top of that, we now
have more methods than just the methods of the collection interface. For example I can
insert an element by giving an index position. So I could for example say at index position
0 add me T and at S add index position 2. And then let’s use the simple and fast printout
method. Let’s remove this because it will format the list in a one liner but let me
just show you what I mean. We still have the true, we can also remove this, but for now
it’s fine. So we have T because we added that at index position 0 which means this
will be inserted where A was before, before I added that at index 0, this was the index
0, so let’s put it to where A was before and then shifted everything else to the right.
And this is why now this is nice to see it like this in one line because you will see
this has all shifted to the right which when I remove an element, the opposite happens
like if I remove the S, everything will be shifted to the left but let’s see this later.
And we added the S at index 2 so this is now index 0, 1 and 2 which again shifted the rest
to the right. So let’s also remove something. Let’s do this here. Let’s say I removed
index and now let’s see, I remove index 1 which should be the B. And there it seems I am wrong because the
A is still there, the B is still there, let us see. Well of course because it’s a bit
confusing, as I added the T at index position 0, A is now index position 1 and this one
was removed. So removing by index is always not so easy. Really, I have to tell you these
index-based methods I hardly ever use them. Normally you will find actually with most
of the methods of the collection interface which again tells you often it’s a good
idea to use collection here. So this actually inserts an element.
By the way, let’s increment this a bit just for demonstrations purposes. This is not so
flexible. Really, as I said in my slides, you should really think carefully the number
of elements that you need. Just here it’s different because we are playing around. Because
I want to show you the difference again in the example between the capacity and the size
because the size of the list is the number of elements, let’s do this here, and not
the capacity, because my array with a capacity of 100, the array will be 100 elements big
even though they are empty but the size, however, will be much smaller. And here you see the
size is 4 and not 100 so there is a difference. Now we can also ask for the index of a certain
element. Let’s do this here. IndexOf and there is also last indexOf and also isEmpty,
this we should also use. So indexOf, let’s use the A. Maybe we should remove that to not make it
by (9.8k points)