This question already has answers here:
Why start an ArrayList with an initial capacity?
(11 answers)
Closed 8 years ago.
Is it considered a best pratice to initialize Collections ( eg: Arraylist ) with initial capacity ?
From my observation through docjar, it looked like initialization reduced need for 'expansion' of initial array and also reduce the size, of internal datastructure due to resizing.
As the comment mentions, if you know the number of elements that will be eventually needed, then it will gain you some performance since you do not have to re-allocate the backing array when the number of elements increases to exceed the current capacity.
However, there is a tradeoff because if you can't estimate it well (within an order of magnitude), then you may waste memory by allocating too much at the beginning.
Related
This question already has answers here:
Do Java arrays have a maximum size?
(10 answers)
Closed 6 years ago.
How many integers can an array in java and python have? And if I want to enter more integers to an array and I reached that limit how to handle this?
Java arrays can have 2^31-1 element in theory, although some JVM cannot allocate an object larger than 2^31-8 elements due to OS limitations. (See the ArrayList code for an example of this limit in practice)
If you have more elements, you can either
have an array of arrays.
use a memory mapped file to store the data. This has the advantage of potentially greater than main memory and is persisted.
This question already has answers here:
Finding out the minimum difference between elements in an array
(9 answers)
Closed 7 years ago.
Well I have to find the smallest difference between any two elements of a large array. By large i mean really large like 10 million or more elements array. Although this is not a practical scenario i like to know the fastest possible ones. Getting an element, comparing with the rest of the elements , sorting the difference and getting the least difference is really unimaginable. Is there some efficient way to do this task? I would be happy.
How fast this can be done depends entirely on your notion of 'difference' - for example, if you just have an array of numbers and want the smallest difference between two numbers (that is, you have a total ordering available for your elements) then you can just sort and get the smallest difference between neighbors. If there is no total ordering, you'll have to minimize your checks by clustering your elements somehow (e.g. creating a spatial tree for an array of 3D points).
This question already has answers here:
Java dynamic array sizes?
(19 answers)
Closed 8 years ago.
So I am assigned with a project where I have an array and as the user puts elements into this array it will have to double in length once it gets full. We are not permitted to use array lists or anything in the collections interface. What I was trying to do was to make a new array once the old one was full, and then I would copy the values over to the new array. The problem is I don't know how many times I will have to make a new array, so I was wondering how to go about solving this.
Arrays are fixed length. If you want a data structure that has variable length use an ArrayList
Since the poster explicitly stated the requirements of the homework exclude the use of Collections an alternative approach would be to use System.arraycopy(). In such an approach you only maintain a single array and as you add items to it you use System.arraycopy() to copy the old array into a new larger array. System.arraycopy() is relatively fast and is actually how ArrayList expands its size.
If you are concerned about the cost of using System.arraycopy() you could use it only when your array is full and create a new array with more space. E.g.
Create and array of size 20. When it gets full copy it into and array of size 40. When that gets full copy it into an array of size 60...
Interestingly ArrayList increases its size by
int newCapacity = (oldCapacity * 3)/2 + 1;
when the old array is full. Presumably the writers of this put some considerable thought into how much to grow the array by when needed. It might be worth doing the same.
This question already has answers here:
How much data can a List can hold at the maximum?
(8 answers)
Closed 9 years ago.
I am building a chat application. Current I have all messages in an ArrayList, which got me thinking - How many elements are the ArrayList design to hold? 100? 1.000? 10.000?
ArrayList can't hold more than Integer.MAX_VALUE elements.
So 2147483647 is the max.
The size of ArrayList is Integer.MAX_VALUE.
/**
* Returns the number of elements in this list. If this list contains
* more than <tt>Integer.MAX_VALUE</tt> elements, returns
* <tt>Integer.MAX_VALUE</tt>.
*
* #return the number of elements in this list
*/
int size();
It is because ArrayList uses array internally and theoretically an array can be of Integer.MAX_VALUE in size at maximum. For further information, you can see this.
ArrayList which is backed by an array, and is limited to the size of the array - i.e. Integer.MAX_VALUE.
A LinkedList isn't limited in the same way, though, and can contain any amount of elements.
see similar question max. length of List in Java
How many data a list can hold at the maximum
to have other aspects on max size of list
ArrayList can hold any number of elements up to Integer.MAX_VALUE - this is due to a design decision to use int datatype for indexes. However what's important is how you are allocating memory for it - the memory allocation is slow - and how you're processing/accessing elements. From the storage aspect alone, though, you're limited by MAX_VALUE. In Java, this is 2^31-1 = 2,147,483,647.
For any normal application this should be enough. Yet, if you need more, you can easily get the source code for it and modify it to use long as the index datatype - and then be limited by Long.MAX_VALUE.
This question already has answers here:
Closed 12 years ago.
Possible Duplicate:
How many data a list can hold at the maximum
What is the maximum number of elements a list can hold?
Assuming you mean implementations of the java.util.List interface, methods like get() and size() use int, so the upper theoretical boundary would be Integer.MAX_VALUE entries. You might run out of memory before you reach this limit though!
The index type in Java arrays is int too, so you're definitely limited to Integer.MAX_VALUE entries for regular arrays.
If you're talking about an ArrayList, they're indexed using integers (which are always signed in Java), so they can theoretically hold 2^31 elements (not 2^32). At that point you're probably going to have memory issues anyway.
LinkedList is also an implementation of List which stores the elements as a Linked List. So theoretically its size is equivalent to the amount of memory you can allocate.