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.

5
  • A related question. Commented Aug 21, 2015 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. Commented Aug 22, 2015 at 0: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. Commented Aug 22, 2015 at 1: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. Commented Aug 23, 2015 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. Commented Aug 24, 2015 at 2:40

2 Answers 2

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).

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

Comments

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.

Comments

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.