Java對List集合的常用操作 北京java 基礎

來源:互聯網
上載者:User

標籤:mil   list   special   public   需要   class   系統   java 基礎   break   

有時候,我們需要從一個系統裡匯出資料,並匯入另外一個系統中,而這個資料很大,而資料匯入受到限制,無法實現,這時,我們就需要對資料進行List切分,然後再一個個匯出,最終,實現資料匯入。

對於資料的分段處理,我們可以採用subList方法進行實現,具體用法可參看以下案例:

import java.util.ArrayList;

import java.util.List;

public class listTest {

  public static void main(String[] args) {

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

    list.add("one");

    list.add("two");

    list.add("three");

    list.add("four");

    list.add("five");

    list.add("six");

    list.add("seven");

    int ftest = 3;//每次取的資料

    int size = list.size();

    int temp = size / ftest + 1;

    boolean special = size % ftest == 0;

    List<String> cutList = null;

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

      if (i == temp - 1) {

        if (special) {

          break;

        }

        cutList = list.subList(ftest * i, size);

      } else {

        cutList = list.subList(ftest * i, ftest * (i + 1));

      }

      System.out.println("第" + (i + 1) + "組:" + cutList.toString());

    }

  }

}

得出的結果為:

第1組:[one, two,three]

第2組:[four, five,six]

第3組:[seven]

如果ftest = 1,得出的結果為:

第1組:[one]

第2組:[two]

第3組:[three]

第4組:[four]

第5組:[five]

第6組:[six]

第7組:[seven]

以上是其用法執行個體,可以結合執行個體進行實際應用!


Java對List集合的常用操作 北京java 基礎

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在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.