views:

315

answers:

8

What is the best way to do a resizable array in Java? I tried using Vector, but that shifts all elements over by when when you do an insert, and I need an array that can grow but the elements stay in place. I'm sure there's a simple answer for this, but I still not quite sure.

+2  A: 

A LinkedList?

Alex
+11  A: 

As an alternative, you could use an ArrayList. It is a resizable-array implementation of the List interface.

Usage (using String):

List<String> myList = new ArrayList<String>();
myList.add("a");
myList.add("c");
myList.add("b");

The order will be just like you put them in: a, c, b.

You can also get an individual item like this:

int myInt = myList.get(0);

Which will give you the 0th element: "a".

Kevin Crowell
But... That doesn't compile!
BalusC
You'd have to `import java.util.List` and `java.util.ArrayList`
isbadawi
@isbadawi No, I wrote my original answer while writing C# and did some weird mix of the 2. I edited it.
Kevin Crowell
Note that auto-boxing is happening when you add the ints to the List (since java doesn't support primitives in generics)
Alex
+1  A: 

Check out ArrayList

carillonator
A: 

I tried using Vector, but that shifts all elements over by when when you do an insert, and I need an array that can grow but the elements stay in place.

You probably want to use ArrayList instead of Vector.

They both provide about the same interface, and you can replace elements with both of them by calling set(idx, element). That does not do any shifting around. It also does not allow you to grow the array, though: You can only insert at already occupied positions (not beyond the current size of the array), to add new elements at the end you have to use add(element).

The difference between ArrayList and Vector is that Vector has synchronization code which you most likely do not need, which makes ArrayList a little faster.

Thilo
A: 

Using wonderful classes in Collections framework is the better than using arrays. But in case your question is from a "quizzing" perspective, here is what you should do. Create your own resize method such as:

  int[] oldArray = {1,2,3};

  int oldSize = java.lang.reflect.Array.getLength(oldArray);
  Class elementType = oldArray.getClass().getComponentType();
  Object newArray = java.lang.reflect.Array.newInstance(
         elementType,newSize);
  int preserveLength = Math.min(oldSize,newSize);
  if (preserveLength > 0)
      System.arraycopy (oldArray,0,newArray,0,preserveLength);

  oldArray = newArray;
ring bearer
This does not resize the array. It creates a new array. Which means none of the objects that hold a reference to the old array will be see the changes.
Thilo
Guilty as charged; please disregard my answer above.
ring bearer
If we should disregard your answer maybe you could just delete it all together? Then you will also not risk getting downvoted again?
Alfred
Jeez! tough crowd here, @Alfred, what sadistic pleasure do you derive from down voting something that was already down voted? Anyway, go ahead and down-vote as much as you like... and yes, I will leave this answer there to show how can we assume things and give wrong answers
ring bearer
@ring bearer: I did not downvote you ;). I am against down-voting myself unless a answer/question is totally useless.
Alfred
+1  A: 

Like Sanjo pointed out: "An array is a static datastructure, so they can't grow". The list interface can by backed by an array(for example ArrayList like Kevin pointed out in his post). When the list structure is full and a new item has to be added to the list. Then the structure first creates a new array which can contain the old elements plus the new element which has to be added to the list.

The list interface has a different implementations which all have there pros/cons and you should pick the one best solving your problem-set. Below I will try to give a short summary when to use which implementation:

Not thread-safe implementations:

  • ArrayList: Resizable-array implementation of the List interface. You should use this implementation when you are doing a lot of size, isEmpty, get, set, iterator, and listIterator operations run in constant time. The add operation runs in amortized constant time, that is, adding n elements requires O(n) time. I think you should use this implementation when doing more lookups(get()) then adding items to list(add()).
  • LinkedList: This implementation is not backup by an array but "links" the nodes together. In my opinion you should use this implementation when you are doing more add() then get().

Thread-safe implementations:

Be aware that these list implementations aren't thread-safe which means it is possible to get race conditions when accesing them from multiple threads. If you want to use List implementations from multiple threads I would advise you to study the java.util.concurrent package and use implementation from that class.

Alfred
A: 

If you want to operate array data after all element had already inserted or deleted, there is a way that try to create a LinkedList or ArrayList, its simply resize, after the data input is finished, you can transfer the ArrayList to an Array, then do all the things you normally to Array.

jasonfungsing
+2  A: 

You probably should use ArrayList instead of Vector for reasons explained in other answers.

However ...

I tried using Vector, but that shifts all elements over by when when you do an insert, and I need an array that can grow but the elements stay in place.

When you do an insertElementAt(pos, elem), you have specifically asked for the element shifting. If you don't want the elements to be shifted, you should use set(pos, elem) instead. Or if you want to add the element at the end of the vector, you can also use add(elem).

Incidentally, the previous paragraph applies to all implementations of List, not just Vector, though the implementation details and performance vary across the different kinds of List.

Stephen C