Java之List使用方法

來源:互聯網
上載者:User

標籤:rem   next   查詢   ntb   asn   ack   div   object   package   

 

 1 package basic; 2  3 import java.util.ArrayList; 4 import java.util.Iterator; 5 import java.util.List; 6  7 //List使用方法 8 public class ListDemo { 9 10     public static void main(String[] args) {11         // 執行個體化List12         List<String> lists = new ArrayList<String>();13 14         // 添加元素15         lists.add("a");16         lists.add("b");17         lists.add("c");18         lists.add("d");19         dataPrintByIt(lists);20         lists.add(3, "e");21         dataPrintByIt(lists);22 23         // 修改元素24         lists.set(4, "f");25         dataPrintByIt(lists);26 27         // 刪除元素(index或object)28         lists.remove(0);29         lists.remove("b");30         dataPrintByForEach(lists);31 32         // 查詢33         String item = lists.get(1);34         int n = lists.indexOf("f");35         System.out.print("get:" + item + "  indexOf:" + n);36 37         // 遍歷(原始方法和新特性)38         dataPrintByIt(lists);39         dataPrintByForEach(lists);40 41         // 清空42         // lists.clear();43 44     }45 46     // 遍曆list-原始方法47     public static void dataPrintByIt(List<String> lists) {48         Iterator<String> it = lists.iterator();49         while (it.hasNext()) {50             System.out.print(it.next() + " ");51         }52         System.out.println();53     }54 55     // 遍曆list-forEach方法,Java8新特性56     public static void dataPrintByForEach(List<String> lists) {57         lists.forEach(58                 (x) -> System.out.print(x + " ")59                 );60         System.out.println();61     }62     63     // 遍曆list-forEach方法64     public static void dataPrintByFE(List<String> lists) {65         for (String item:lists){66             System.out.println(item);67         }68         System.out.println();69     }70 71 }

 

Java之List使用方法

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.