1

Title pretty much says it all. I had a hard time finding information on this, so I did some trial and error, and I thought I would share my results here.


Coming from a PHP background, I expected Java's ArrayList type to support sparse indicies. For example, in PHP I can do:

$test = array(
    "Item 1",
    "Item 2",
    "Item 3"
);

unset($test[1]);

echo $test[2];

And get "Item 3" back. But, when I try something similar in Java:

ArrayList<String> test = new ArrayList<>();

test.add("Item 1");
test.add("Item 2");
test.add("Item 3");

test.remove(1);

System.out.println(test.get(2));

I get an IndexOutOfBoundsException. It would appear that when you remove the element, the array gets re-indexed. Maybe I'm missing something (I'm pretty new to Java), but it seems to me that if you depend on knowing an index won't change if elements are removed, you should using something like HashMap instead of ArrayList.

Jon Clements
  • 124,071
  • 31
  • 219
  • 256
Dominic P
  • 1,830
  • 1
  • 22
  • 41
  • [A related question](http://stackoverflow.com/questions/12626135/memory-efficient-sparse-array-in-java). – Hovercraft Full Of Eels Aug 21 '15 at 23:18
  • 3
    *"I had a hard time finding information on this ...."* - The first place you look for information on Java classes is the Javadoc. – Stephen C Aug 22 '15 at 00:00
  • The remove() method removes and shifts every trailing values 1 position further, as per documentation. Maybe you can instead just set the value to null or, in your case an empty String. – redxef Aug 22 '15 at 01:31
  • You say "Not really understanding the backlash here" on your deleted answer. The backlash is because your answer looks like a question, since you say "I expected" and show PHP code. If you only answered with the second half, it would look a lot more like a real answer. – Veedrac Aug 23 '15 at 23:21
  • @veedrac, it seemed to me that the code made the answer pretty clear. I even mentioned an alternative data type to use. I wonder what question could be perceived as being asked there. – Dominic P Aug 24 '15 at 02:40

2 Answers2

2

No. Java ArrayList (per the Javadoc)

this class provides methods to manipulate the size of the array that is used internally to store the list.

And per JLS-10.3. Array Creation

An array creation expression specifies the element type, the number of levels of nested arrays, and the length of the array for at least one of the levels of nesting. The array's length is available as a final instance variable length.

In Java, one would generally implement a sparse Collection with a Map (like HashMap or possibly LinkedHashMap).

Elliott Frisch
  • 183,598
  • 16
  • 131
  • 226
1

Does Java ArrayList support sparse indices?

No.

The javadoc says this:

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.

Always.

If it is always (at least) as big as the size, then it is not sparse ... by definition.

Stephen C
  • 632,615
  • 86
  • 730
  • 1,096