遍歷容器方法彙總

2021-09-24 15:23:21 字數 1094 閱讀 6859

最近學了 list,set,map的遍歷,話不多說寫一手

首先是list遍歷

listlist = new arraylist();

list.add("jia");

list.add("jin");

list.add("jie");

//根據集合長度來遍歷

for (int i = 0; i < list.size(); i++)

//增強for迴圈,又稱foreach遍歷

for (string temp : list)

//迭代器 iterator遍歷

for (iterator iterator = list.iterator(); iterator.hasnext();)

//如果遍歷時,刪除集合中的元素,建議使用這種方式!

iterator iter = list.iterator();

while (iter.hasnext())

set 遍歷

//		遍歷set

setset = new hashset();

set.add("a");

set.add("b");

set.add("c");

//foreach遍歷

for (string temp : set)

// 迭代器遍歷

for(iterator itr = set.iterator(); itr.hasnext();)

遍歷map

mapmap = new hashmap();

map.put(1, "li");

map.put(2, "jen");

map.put(3, "vho");

for (integer key : map.keyset())

// 迭代器遍歷

set> ss = map.entryset();

for(iterator iter1 = ss.iterator();iter1.hasnext();)

最後說一下,容器的遍歷無非就是將使用容器內的元素,多敲多體會

Python容器型別的遍歷彙總

遍歷列表 l a b 1 2 print l for i in l print i print type i 結果 a b 1 2 ab 12遍歷字串 s abcde print s for si in s print si print type si 結果 abcdeab cde遍歷字典 dic ...

Python容器型別公共方法彙總

以下公共方法支援列表,元組,字典,字串。python 包含了以下內建函式 函式描述 備註len item 計算容器中元素個數 del item 刪除變數 del 有兩種方式 max item 返回容器中元素最大值 如果是字典,只針對 key 比較 min item 返回容器中元素最小值 如果是字典,...

PHP 遍歷陣列的方法彙總

foreach 是乙個用來遍歷陣列中資料的最簡單有效的方法。example1 colors array red blue green yellow foreach colorsas color 顯示結果 do you like red?do you like blue?do you like gre...