如何在java中实现List集合的遍历 java如何遍历List里面的每个元素
\u5728java\u4e2d\u96c6\u5408\u7684\u904d\u5386\u662f\u600e\u6837\u904d\u5386\u7684List list = new ArrayList();
list.add("aaa");
list.add("bbb");
list.add("ccc");
\u65b9\u6cd5\u4e00\uff1a
for(String attribute : list) {
System.out.println(attribute);
}
\u65b9\u6cd5\u4e8c\uff1a
for(int i = 0 ; i < list.size() ; i++) {
system.out.println(list.get(i));
}
\u65b9\u6cd5\u4e09\uff1a
Iterator it = list.iterator();
while(it.hasNext()) {
System.ou.println(it.next);
}
List list = new ArrayList();
list.add("a");
list.add("b");
list.add("c");
list.add("d");
list.add("e");
for (int i = 0; i < list.size(); i++) {
System.out.println(list.get(i));
}
\u6216\u8005\uff1a
for (Object object : list) {
System.out.println(object);
}
java中实现List集合的遍历有三种方法:
方法一:for-each循环
for(String attribute : list) {
System.out.println(attribute);
}
方法二:for循环
for(int i = 0 ; i < list.size() ; i++) {
system.out.println(list.get(i));
}
方法三:迭代器迭代
Iterator it = list.iterator();
while(it.hasNext()) {
System.ou.println(it.next);
}
List<String> list = new ArrayList<String>();
list.add("a");
list.add("b");
list.add("c");
list.add("d");
for(String str : list){
System.out.print(str);
}
for(int i = 0 ; i < list.size() ; i++) {
System.out.print(list.get(i));
}
输出结果 abcd
常用的List集合, 可以使用下面几种方式进行遍历
参考代码import java.util.ArrayList;import java.util.Iterator; public class Test { public static void main(String[] args) { ArrayList<Integer> list = new ArrayList<Integer>(); list.add(5); list.add(8); list.add(9); //方案一 推荐 .简洁 for (Integer e : list) { System.out.print(e + ","); } //方案二 for (int i = 0; i < list.size(); i++) { System.out.println(list.get(i)); } //方案三 Iterator<Integer> it = list.iterator(); while (it.hasNext()) { System.out.println(it.next()); } }}
List<String> list = new ArrayList<String>();
list.add("aaa");
list.add("bbb");
list.add("ccc");
方法一:
for(String attribute : list) {
System.out.println(attribute);
}
方法二:
for(int i = 0 ; i < list.size() ; i++) {
system.out.println(list.get(i));
}
方法三:
Iterator it = list.iterator();
while(it.hasNext()) {
System.ou.println(it.next);
}
绛旓細鍦↗ava涓垱寤轰竴涓笉鍙彉鐨凩ist锛鍙互浣跨敤Collections.unmodifiableList()鏂规硶銆傝繖涓柟娉曚細杩斿洖涓涓笉鍙彉鐨勮鍥撅紝杩欐剰鍛崇潃浣犱笉鑳藉湪杩斿洖鐨勫垪琛ㄤ笂杩涜娣诲姞銆佸垹闄ゆ垨淇敼鎿嶄綔銆傞鍏堬紝java List originalList = Arrays.asList("Apple", "Banana", "Cherry");List immutableList = Collections.unmodifiableList(orig...
绛旓細1. ArrayList锛欰rrayList鏄疞ist鎺ュ彛鐨勪竴涓彲璋冩暣澶у皬鐨勬暟缁勫疄鐜銆傚畠鏄墍鏈夊熀浜庢暟缁勭殑鏁版嵁缁撴瀯鐨勬渶浣抽夋嫨锛屽洜涓哄畠鎻愪緵浜嗗揩閫熺殑闅忔満璁块棶鍒楄〃涓殑鍏冪礌銆傜劧鑰岋紝鎻掑叆鍜屽垹闄ゅ厓绱犵殑鎿嶄綔鍙兘浼氭瘮杈冩槀璐碉紝鍥犱负闇瑕佺Щ鍔ㄥ厓绱犮備緥濡傦細java List list = new ArrayList>();list.add("Apple");list.add("Banana");2. L...
绛旓細1銆List<String> list = new ArrayList<String>();//鍒涘缓闆嗗悎瀵硅薄锛2銆乴ist.Add("1");//鍦ㄩ泦鍚堥噷瀛樺叆鏁版嵁銆3銆乴ist.Add("2");4銆丩ist鏄竴涓帴鍙o紝涓嶈兘瀹炰緥鍖栵紝鍒涘缓瀵硅薄鏃惰浣跨敤浠鐨勫疄鐜绫籄rrayList锛堝叾浠栵級銆
绛旓細java涓疄鐜癓ist闆嗗悎鐨勯亶鍘嗘湁涓夌鏂规硶锛氭柟娉曚竴锛for-each寰幆for(String attribute : list) { System.out.println(attribute);}鏂规硶浜岋細for寰幆for(int i = 0 ; i < list.size() ; i++) { system.out.println(list.get(i));}鏂规硶涓夛細杩唬鍣ㄨ凯浠terator it = list.iterator();while(it...
绛旓細for (String str : arrayList) { System.out.println(str);} } } 鍦ㄦ绀轰緥涓紝鎴戜滑棣栧厛閫氳繃new ArrayList<>()璇彞鍒涘缓浜嗕竴涓悕涓篴rrayList鐨ArrayList闆嗗悎瀵硅薄銆傜劧鍚庝娇鐢╝dd()鏂规硶灏嗕笁涓瓧绗︿覆渚濇娣诲姞鍒伴泦鍚堜腑銆傛渶鍚庯紝浣跨敤澧炲己鐨刦or寰幆閬嶅巻闆嗗悎锛屽苟浣跨敤System.out.println()璇彞鎵撳嵃姣忎釜瀛楃涓茬殑鍐呭...
绛旓細鍦↗AVA涓锛屾兂瑕佹妸涓涓list涓鐨勫唴瀹规坊鍔犺嚦鍙︿竴涓猯ist涓紝鏈変袱绉嶆柟娉曪細閲囩敤寰幆鐨勬柟娉曘傞氳繃寰幆绗竴涓狝rrayList锛岀劧鍚庨氳繃姣忓惊鐜竴娆$殑鏂瑰紡锛屽皢鍊艰祴鍊肩粰鍙︿竴涓狝rrayList銆傚叿浣撲唬鐮瀹炵幇濡備笅锛2.閲囩敤ArrayList鑷甫鐨刟ddAll()鏂规硶銆俛ddAll()鏂规硶鏄氳繃浼犲叆鏁翠竴涓狶ist锛屽皢姝List涓鐨勬墍鏈夊厓绱犲姞鍏ュ埌鏂扮殑List涓紝...
绛旓細涓 list鎺ュ彛 1.java.util.ArrayList绫诲疄鐜颁竴涓彲澧為暱鐨勫姩鎬佹暟缁勶紝骞虫椂鐢ㄧ殑姣旇緝澶氱殑灏辨槸add()鏂规硶锛屽叾瀹冩柟娉 璇峰弬鑰傾PI甯姪鏂囨。銆備娇鐢ㄨ鏂规硶鍚戦泦鍚堝唴娣诲姞瀵硅薄锛屽苟涓旇繖浜涘璞℃槸鎸夌収涓瀹氱殑椤哄簭鎺掑垪 鐨勶紝瀹冨唴閮ㄥ師鐞嗘槸鏁扮粍瀹炵幇鐨锛屽洜姝ゅ鐞嗘暟鎹噺杈冨ぇ鏃讹紝涓嶅缓璁娇鐢ㄣ俻ublic class TestArrayList { public ...
绛旓細浠g爜濡備笅锛歱ublic class List { public static void main(String[] args) { // 鍒涘缓闆嗗悎list2 ArrayList<String> list2 = new ArrayList<String>(); // 鍚戦泦鍚堜腑娣诲姞鍏冪礌 list2.add("abc"); list2.add("xyz"); // 閬嶅巻闆嗗悎 for (int i = 0; i < list2.siz...
绛旓細List鏈韩鏄竴涓帴鍙o紝鎵浠ワ紝鎯宠瀹炰緥鍖栦竴涓狶ist闇瑕佺户鎵夸簡List鐨绫伙紝姣斿ArrayList绛夈傛墍浠ュ彲浠ヨ繖鏍峰仛锛歀ist<...> myList = new ArrayList<...>();
绛旓細java.util.LinkedList 鏄Java闆嗗悎妗嗘灦涓殑鎴愬憳涔嬩竴锛屽簳灞傚熀浜庡弻鍚戦摼琛ㄥ疄鐜帮紝闆嗗悎瀹归噺鍙姩鎬佸彉鍖栥傚畠缁ф壙鑷狝bstractSequentialList鎶借薄绫伙紝瀹炵幇List鎺ュ彛锛岃繕瀹炵幇浜咰loneable鍜孲erializable涓変釜鏍囪鎺ュ彛锛岃〃鏄嶭inkedList鏄彲鍏嬮殕澶嶅埗鍜屽簭鍒楀寲鐨銆傜浉姣擩ava鏁扮粍鍒楄〃锛圓rrayList锛夊熀浜庡姩鎬佹暟缁勫疄鐜帮紝鏀寔蹇熼殢鏈鸿闂絾澧炲垹鎿嶄綔...