Answer the question
In order to leave comments, you need to log in
How to move an array element to a different index in Java?
For example, there is an array: List<RowItem> data = new ArrayList<RowItem>();
With 5 elements
data.add(new RowItem("Элемент: 0"));
data.add(new RowItem("Элемент: 1"));
data.add(new RowItem("Элемент: 2"));
data.add(new RowItem("Элемент: 3"));
data.add(new RowItem("Элемент: 4"));
RowItem item = data.get(3); // Получаю объект "Элемент: 3"
data.remove(3);
data.add(1, item);
RowItem row = data.get(indexOne);
data.set(indexOne, data.get(indexTwo));
data.set(indexTwo, row);
Collections.swap(data, 1, 3);
Answer the question
In order to leave comments, you need to log in
Didn't find what you were looking for?
Ask your questionAsk a Question
731 491 924 answers to any question