最近中文字幕高清中文字幕无,亚洲欧美高清一区二区三区,一本色道无码道dvd在线观看 ,一个人看的www免费高清中文字幕

為了賬號(hào)安全,請(qǐng)及時(shí)綁定郵箱和手機(jī)立即綁定
已解決430363個(gè)問題,去搜搜看,總會(huì)有你想問的

kotlin中怎么給list添加元素?

kotlin中怎么給list添加元素?

慕無忌1623718 2019-03-12 09:09:45
kotlin中怎么給list添加元素?
查看完整描述

2 回答

?
慕姐8265434

TA貢獻(xiàn)1813條經(jīng)驗(yàn) 獲得超2個(gè)贊

public interface List<E> extends Collection<E>是接口,不能直接使用,必須由它的實(shí)現(xiàn)子類才能使用。
set(index, obj);方法某些時(shí)候可以實(shí)現(xiàn)添加新元素。
以下是ArrayList的set源碼部分:
/**
* Replaces the element at the specified position in this list with
* the specified element.
*使用element替代list中指定位置的element
* @param index index of the element to replace
* @param element element to be stored at the specified position
* @return the element previously at the specified position
* @throws IndexOutOfBoundsException {@inheritDoc}
*/
public E set(int index, E element) {
RangeCheck(index);//邊界查詢,判斷index是否越界
E oldValue = (E) elementData[index];//取出index位置的元素
elementData[index] = element;//替換指定位置的元素
return oldValue;//返回被替換的元素
}
set方法的含義:用指定的元素替代此列表中指定位置上的元素。如果指定位置是為空,那么就相當(dāng)于add(int index, E element)方法,添加新元素到list中,如果這個(gè)index位置上已經(jīng)存在值,那么只能是替換原有值。

 


查看完整回答
反對(duì) 回復(fù) 2019-03-19
  • 2 回答
  • 0 關(guān)注
  • 5924 瀏覽

添加回答

舉報(bào)

0/150
提交
取消
微信客服

購(gòu)課補(bǔ)貼
聯(lián)系客服咨詢優(yōu)惠詳情

幫助反饋 APP下載

慕課網(wǎng)APP
您的移動(dòng)學(xué)習(xí)伙伴

公眾號(hào)

掃描二維碼
關(guān)注慕課網(wǎng)微信公眾號(hào)