How can I change the length of an array? [duplicate] - java

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.

Related

If I create an array that can hold 10 objects, is it possible for that array to less then 10 objects?

I need to create an array that can hold 10 objects. If I make this array will it be able to hold less then the 10 object. An example would be the array has 7 objects and room for 3 more. Is this possible?
Yes. Assign null at some indices. Also, you can create the array without any objects stored in it, specifying the length.
Yes. Arrays can hold any number of items up to, but not including the length you have allocated for it. In Java, arrays of primitive types (int, bool, double, char, etc.) will be filled with the default value of that type for any non-initalizer-list array, and null references for any arrays of Object types.
However, questions like yours are more suited for classroom discussion, as you may still be learning computer science material, it seems like.
It depends on what you mean.
All arrays have fixed length, and each always contains a number of elements exactly equal to its length. In that sense, no, arrays cannot have excess capacity; they are always completely filled. You can, however, keep track externally of which elements contain valid data, and ignore the others.
Technically, no array contains objects, but many contain references (of various types; as opposed to primitive values). It is conventional to be a bit sloppy with our language by calling those arrays of objects, and that's how I interpret the question. The distinction becomes important, however, when we recognize that any element of an array of references may contain the value null, which does not refer to an object. Thus, an array of references with some elements null refers to fewer actual objects than its length. You might characterize that as the array containing fewer objects than its length.
Note that null elements are not limited to the end of an array. They may appear at any index, interspersed with non-null elements.
With all that said, however, I suspect you're looking for Lists, and specifically java.util.ArrayList. Lists are more flexible than arrays in many ways, including that they have adjustable size. And ArrayList indeed does have a distinction between its current capacity and its current size, though the capacity is expanded as needed, not fixed like an array's length. The class name reflects that it is implemented with use of arrays, and its performance characteristics reflect that.
Array is a data structure which stores a fixed-size sequential collection of elements of the same type. When you initialize an array, it can have at most the till the size of array, so for your answer it can have empty cells.
I need to create an array that can hold 10 objects. If I make this array will it be able to hold less then the 10 object. An example would be the array has 7 objects and room for 3 more. Is this possible?
In short, yes. It will be able to hold 0 to 10 objects. However it doesn't mean your array can shrink and grow from 0 to 10. It will be a fixed size of 10 (be it you are using it or not).
The long answer is, there will always be 10 elements populated with some values (be it you are actively using all elements or not). Those elements which you think you are not using at the moment will still be populated with default value according to the data type of the array (i.e. null of object, 0 for integer, false for boolean).
For example, creating an int array of size 10:
int array[] = new int[10];
By default, you will already have 10 arrays (all populated with default 0).
Index: 0 1 2 3 4 5 6 7 8 9
Array: [0][0][0][0][0][0][0][0][0][0]
You will always have these 10 elements at your disposal. It is now up to you how you want use these 10 elements.
If you are looking for something that can shrink and grow. You can look into using java.util.ArrayList which can shrink/grow as you remove/add items into the list.

Java arrays value setting

How do I add an array value to another array while increasing the array value of the other array each time. For example, if I want array1 value to be added to array2 and when I search for the array2[position] it appears as the value of array1.
array1[position]=array2[count]
I kind of want it so that it is looped but I don't know how
From your comment -> Its going to get clumsy if you insist on arrays - using ArrayList will be much easier:
ArrayList<Item> cart = new ArrayList<>();
cart.add(array1[position]);
If you insist on using arrays you must create a new array with larger size and copy the old array into the new array each time you run out of space - actually this is what ArrayList does for your (albeit in a clever way - increasing size by 50% each time it runs out of space)

Populate an array with unlimited index - Java

Is there a way to populate an array with unlimited indexes in java? I just want the program to keep appending to the array without any capacity.
I declared like this:
int arrInt[] = new int[]
But it says that it is missing dimension. So how can I do it?
Array in Java is static, ie. you have to declare its size while initializing. Hence the answer is 'no' and you are getting the correct message as you have not mentioned the dimension.
No, there isn't.
ArrayList serves that purpose (i.e. an array based list whose capacity increases over time automatically when adding elements to it).
Is there a way to populate an array with unlimited indexes in java?
NO
I just want the program to keep appending to the array without any
capacity.
Need to Use ArrayList
List<Object> arr = new ArrayList<Object>();
arr.add(obj1);
arr.add(obj2);
arr.add(obj3); .. so on
I declared like this - int arrInt[] = new int[]. But it says that it
is missing dimension.
Arrays will always complain to declare the size.
In java, arrays have a maximum number of elements equal to Integer.MAX_VALUE - 5. To get around this, try using a LinkedList which has an unimited number of elements. Since this is a list, you can modify the size whenever necessary in your code. Note that ArrayList also has a max number of elements of Integer.MAX_VALUE, so LinkedList is necessary if you truly need a list of unlimited size.
Resource: http://docs.oracle.com/javase/7/docs/api/java/util/LinkedList.html

Amount of elements in ArrayList [duplicate]

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.

Size of a list in Java [duplicate]

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.

Categories