S
S
Stavr Strelov2020-08-12 10:33:21
Java
Stavr Strelov, 2020-08-12 10:33:21

Java. How to resize an ArrayList?

Hello! I'm building an application where the location of objects depends on their index in an ArrayList and I'm having a problem.
ArrayList expands when any objects are added to it:

list.add("Новая строка"); //Размер массива стал на единицу больше

You can also add objects to it by index:
list.add(0, "Новая строка"); //Данный объект был добавлен в массив под индексом 0

However, adding objects to the array at indices that do not correspond to the current size of the array does not work:
list.add(0, "Новая строка");
list.add(12, "Ещё одна новая строка"); //Не работает

I don't want to add a bunch of empty objects before index 12 to expand the list's dimensions. Is there any function to increase the size of an ArrayList without adding objects to it? Or is there any standard solution to this problem? Thanks in advance!

Answer the question

In order to leave comments, you need to log in

1 answer(s)
D
Dmitry Roo, 2020-08-12
@IAmNoob

If you need rows with indexes doMap<Integer, String>

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question