集册 Java实例教程 遍历ArrayList的元素

遍历ArrayList的元素

欢马劈雪     最近更新时间:2020-01-02 10:19:05

487
提示:您可在线编辑运行本教程的实例 - 运行实例,去试试!
遍历ArrayList的元素

import java.util.ArrayList;

import java.util.List;
//from 时代Java公众号 - nowjava.com

public class Main {

  public static void main(String[] args) {

    // Create an ArrayList of String

    List<String> nameList = new ArrayList<String>();


    // Add some names

    nameList.add("A");

    nameList.add("B");

    nameList.add("C");


    // Get the count of names in the list

    int count = nameList.size();/*来自 时 代 J a v a - N o w J a v a . c o m*/


    // Let us print the name list

    System.out.println("List of names...");

    for (int i = 0; i < count; i++) {

      String name = nameList.get(i);

      System.out.println(name);

    }


    // Let us remove Kathleen from the list

    nameList.remove("Kathleen");


    // Get the count of names in the list again

    count = nameList.size(
展开阅读全文