1

Could you please advise how to create a dynamic array..!! A primitive array can be declared as

int[] myIntArray = new int[3];

But this time I know know the size of array is of 3 elements but what if I want to create a dynamic array how I will create that, Please advise.

6 Answers 6

3

An array has always to be initialised with a given size. Use a List if you wish to have a 'dynamic' collection and then convert to an array if needed. The array cannot be resized after instantiation whereas a List can (ignoring non modifiable lists).

Sign up to request clarification or add additional context in comments.

1 Comment

That array size can be computed dynamically, but an array cannot be resized after it's been created.
1

You can instantiate an array using a variable, but once instantiated, the array will be stuck at that size.

int[] myArrayInt = new int[arraySizeVariable];

If you want something that is truly dynamic then I would suggest using an ArrayList instead.

List<Integer> myArrayList = new ArrayList<Integer>();

Comments

1

ArrayLists can grow and shrink dynamically:

ArrayList<Integer> list = new ArrayList<Integer>();

//... 
list.add(5);
list.add(1);

//...
int index = list.indexOf(5);
list.remove(index);

Comments

0

use java.lang.reflect.Array

public static Object newInstance(Class<?> componentType, int length) {}

1 Comment

The dynamic part of the question probably refers to the size, not the type, so new int[arraySizeVariable] would be fine.
0

In Java you can have all sorts of dynamic groups of elements. The basic array [] is still going to be as static as it will in any other similar language, so you are going to want to use some implementation of the Collection.java interface.

The most basic one is a Vector which simply contains some objects, not necessarily of the same class, and will grow dynamically as you add or remove items from it. If you want them to be ordered then you can use an implementations of List.java. If you want the collection to be unique then you can use instances of Set.java.

2 Comments

s/Vector/ArrayList/. Vector is more or less deprecated.
i'll just leave this here javacodegeeks.com/2010/08/…
-1
   public int[] createArray(int size){return new int[size];}

Well it's a dumb code. Creates array of size you want. It's not expandable once created. For that you need to expand manually by copying to a larger array when your old array is full. Or better, get it done by a collection called ArrayList, which doubles capacity when reaches to some threshold.

If you implement your own expandable array, it will be same as using ArrayList: see

Each ArrayList instance has a capacity. The capacity is the size of the array used to store the elements in the list. It is always at least as large as the list size. As elements are added to an ArrayList, its capacity grows automatically. The details of the growth policy are not specified beyond the fact that adding an element has constant amortized time cost.

1 Comment

could you please explain what is happening inside the code..!1

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.