35

I am wondering if we can use index to access List

For example:

List<Integer> list; 

list[5]     //blah....
1
  • No. You are asking for a C# feature which isn't present in java. Commented Jun 5, 2020 at 1:47

7 Answers 7

70

Since [] is an operator and java does not support operator overloading you can't use it with List. Instead you have to use the set(int index, T value) and get(int index) methods, which may be verbose but provide exact the same functionality.

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

Comments

35

List.get(int) allows you to access elements using the index.

Comments

9

There are plenty of good answers here, but I just want to point out that list.get(i) is the same as list[i] only if list is implemented with an array (i.e ArrayList). If it is a LinkedList you are not really indexing with get, but rather iterating.

So if you use get() with the interface type List, especially in a loop, you should check how it's implemented, as get() with ArrayList is O(1) whereas get() with LinkedList is O(n) (much slower).

Comments

6

You can access List elements using their index through the use of the get method:

get

public Object get(int index)

Returns the element at the specified position in this list.

Parameters: index - index of element to return.

Returns: the element at the specified position in this list.

Throws: IndexOutOfBoundsException - if the index is out of range (index < 0 || index >= size()).

Keep in mind that the index in Lists is 0 based.

Comments

4

No, you're restricted to List.get (i).

The brackets [] are defined on syntax level, not as method-name, so you can't override them. They are used just for arrays exclusively.

If you like to migrate to Scala, a more modern language for the JVM, you'll find an unified access to arrays and lists, but both with () parentheses.

Comments

3

an alternative to using get(int) is to create an Array using toArray()

List<T> list = ...

Object[] array = list.toArray();

if T is known, toArray(T[]) can be used to return T[] instead of Object[].
The use of toArray is only meaningful, instead of get, if an array is really needed (lots of accesses).

Comments

2

No, you can't do this in Java.

5 Comments

@Downvoter Patrick was correct in saying that list[5] is not possible in Java, although he didn't mention any alternatives.
@asgs - I'm not the "Downvoter", but the answer is not useful, it's actually wrong. Yes, "we can use index to access List" using list.get(int) or, if really needed, list.toArray()[5] [:-)
But don't do that, it would be very inefficient to allocate an array with toArray just to access an element.
I agree, but it can be done in Java... and not that inefficient if doing lots of accesses.
It is however possible to do in C#

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.