集册 Java实例教程 从索引列表中删除值

从索引列表中删除值

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

531
提示:您可在线编辑运行本教程的实例 - 运行实例,去试试!
从索引列表中删除值

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



      items.remove(1); // remove item at index 1

      display(items, "Remove second list element (green):"); 


   } 
   /** from 
   NowJava.com**/


   // display the ArrayList's elements on the console

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

   {

      System.out.print(header); // display header


      
展开阅读全文