集册 Java实例教程 显示带有for语句增强功能的列表内容

显示带有for语句增强功能的列表内容

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

620
提示:您可在线编辑运行本教程的实例 - 运行实例,去试试!
显示带有for语句增强功能的列表内容

import java.util.ArrayList;


public class Main

{/**NowJava.com**/

   public static void main(String[] args)

   {

      // create a new ArrayList of Strings with an initial capacity of 10

      ArrayList<String> items = new ArrayList<String>(); 


      items.add("red"); // append an item to the list          

      items.add(0, "yellow"); // insert "yellow" at index 0


      // display colors using enhanced for in the display method

      display(items,

         "%nDisplay list contents with enhanced for statement:");
         /*
         来 自*
          nowjava
         */


      items.add("green"); // add "green" to the end of the list

      items.add("yellow"); // add "yellow" to the end of the list      

      display(items, "List with two new elements:"); 


   } 


   // display the ArrayList's elements on the console

   public static void display(ArrayList<String> items, String header)

   {

      System.out.print(header); 
展开阅读全文