Home >Java >javaTutorial >Three ways to traverse collections in Java

Three ways to traverse collections in Java

高洛峰
高洛峰Original
2017-01-23 16:50:481691browse

For traversing a collection to obtain its objects, here are three simple ways to summarize it

Method 1: Turn the collection into an array, and then traverse the array

Object[] obj = list.toArray();
for(Object s : obj){
  System.out.println((String) s);
}

Method 2: Get() method. But it can only be used in list collections. Only List collections have index values.

for(int i = 0;i<list.size();i++){
  System.out.println(list.get(i));
}

Method 3: Through iterator

ListIterator it = list.listIterator();
  while(it.hasNext()){
    System.out.println(it.next());
}

Here are two cases for comparison

Collection case:

import java.util.ArrayList;
import java.util.Collection;
import java.util.Iterator;
import java.util.List;
public class paseWork {
  public static void main(String[] args) {
    CollectionTest();
  }
  public static void CollectionTest(){
    Collection <String>collection = new ArrayList<String>();
    collection.add("初一");
    collection.add("小二");
    collection.add("张三");
    collection.add("李四");
    collection.add("王五");
    //1.集合转为数组 Object[]
    Object[] objectsArrC = collection.toArray();
    for (Object object : objectsArrC) {
      String string = (String) object; // 将对象强制转换为字符串输出
      System.out.println(string);
    }   
    //2.get()方法获取元素
    for (int i = 0;i < collection.size();i++){
      //get()只能用在list集合中,所以这里需要转换形式
      System.out.println(((ArrayList<String>) collection).get(i));
    }
    //3.迭代器
    Iterator<String> it = collection.iterator();
    while(it.hasNext()){
      System.out.println(it.next());
    }
  } 
}

List case:

import java.util.ArrayList;
import java.util.Collection;
import java.util.List;
import java.util.ListIterator;
public class paseWork {
  public static void main(String[] args) {
    ListTest();
  }
  public static void ListTest(){
    List<String> list = new ArrayList<String>();
    list.add("初一");
    list.add("小二");
    list.add("张三");
    list.add("李四");
    list.add("王五");
    //1.集合转为数组 Object[]
    Object[] objectsArrL = list.toArray();
    for (Object object : objectsArrL) {
      String string = (String) object; //将对象强制转换为字符串输出
      System.out.println(string);
    }
    //2.通过 get() 方法
    for (int i = 0; i < list.size(); i++) {
      System.out.println(list.get(i));
    } 
    //3.迭代器
    ListIterator<String> iterator = list.listIterator();
    while(iterator.hasNext()){
      System.out.println(iterator.next());
    }
  }
}

The above is the entire content of this article. I hope that the content of this article can bring some help to everyone's study or work. I also hope to support the PHP Chinese website!

For more articles related to the three ways to traverse collections in Java, please pay attention to the PHP Chinese website!

Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn