新聞中心
這里有您想知道的互聯(lián)網(wǎng)營銷解決方案
【java】array和list常用小技巧和知識-創(chuàng)新互聯(lián)
一些常用小技巧,持續(xù)更新
1)初始化非0值,fill
int[] ans = new int[5];
//快速填充數(shù)組初始值
Arrays.fill(ans, -1);
2) array轉(zhuǎn)list
a. 首先可以直接用asList
eg:
Arrays.asList(strArray)
PS:這種有個弊端的到的list并不能調(diào)用add
b. so,再包一層
通過asList轉(zhuǎn)化后的list,再生成list
ArrayListlist = new ArrayList(Arrays.asList(strArray)) ;
c. array通過stream轉(zhuǎn)list , 一步到位,需要boxed
ListnumList = Arrays.stream(nums).boxed().collect(Collectors.toList());
numList.add(6); //可以add
numList.stream().forEach(System.out::println);
3)list轉(zhuǎn)array
a. 直接調(diào)用toArray返回的是Object數(shù)組
todo...
4)一些長度
a. 對于字符串獲取長隊(duì)是個方法length()
b. 對于數(shù)組獲取長度是個屬性length
c. Map、List和Set這些集合類使用size()
5)沒有(item,index)=>{}
替換方案:
for循環(huán)獲取index,然后再通過中括號或者get取
或者forin的到item,再通過indexOf取下標(biāo)
6)類型判斷
System.out.println(numList instanceof List);
//等效于
System.out.println(List.class.isInstance(numList));
7)System.arraycopy
public static void arraycopy(Object src, int srcPos, Object dest, int destPos, int length)
src:源數(shù)組;
srcPos:源數(shù)組要復(fù)制的起始位置;
dest:目的數(shù)組;
destPos:目的數(shù)組放置的起始位置;
length:復(fù)制的長度.
你是否還在尋找穩(wěn)定的海外服務(wù)器提供商?創(chuàng)新互聯(lián)www.cdcxhl.cn海外機(jī)房具備T級流量清洗系統(tǒng)配攻擊溯源,準(zhǔn)確流量調(diào)度確保服務(wù)器高可用性,企業(yè)級服務(wù)器適合批量采購,新人活動首月15元起,快前往官網(wǎng)查看詳情吧
網(wǎng)站題目:【java】array和list常用小技巧和知識-創(chuàng)新互聯(lián)
轉(zhuǎn)載注明:http://fisionsoft.com.cn/article/dshdeg.html