如何在特定位置中插入一个ArrayList对象 [英] How to insert an object in an ArrayList at a specific position

查看:1541
本文介绍了如何在特定位置中插入一个ArrayList对象的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

假设我有一个大小为对象的ArrayList。现在,我想在特定位置插入一个另一个对象,让我们在指数位置k说(大于0且小于n)和我在和索引位置k后想其他对象提前转移一个索引位置。那么,有没有办法直接在Java中做到这一点。其实我想保留,同时增加新的对象来分类的列表中。

Suppose I have an ArrayList of objects of size n. Now I want to insert an another object at specific position, let's say at index position k (is greater than 0 and less than n) and I want other objects at and after index position k to shift one index position ahead. So is there any way to do this directly in Java. Actually i want to keep the list sorted while adding new object.

推荐答案

插入值成特定索引的ArrayList,使用:

To insert value into ArrayList at particular index, use:

public void add(int index, E element)

此方法将转向列表的后续元素。但你不能保证名单将继续作为新对象插入可以按照排序顺序坐错了位置来分类的。

This method will shift the subsequent elements of the list. but you can not guarantee the List will remain sorted as the new Object you insert may sit on the wrong position according to the sorting order.

替换在指定位置的元素,使用:

To replace the element at the specified position, use:

public E set(int index, E element)

这个方法在在指定位置替换元素
列表与指定的元素,并返回该元素previously
在指定的位置。

This method replaces the element at the specified position in the list with the specified element, and returns the element previously at the specified position.

这篇关于如何在特定位置中插入一个ArrayList对象的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆