java集合練習

來源:互聯網
上載者:User

標籤:[1]   com   log   key   main   sys   list   data   http   

分組練習:

List<List<Student>>  list=new ArrayList<List<Student>>();這個是二維集合,分組的時候可以在第一層迴圈中建立一個list,用來接收迴圈的時候返回的對象,然後把list添加到二維集合中

package com.hanqi;import java.util.ArrayList;import java.util.List;import java.util.Random;public class fenzu {public static void main(String[] args){List<Student> l=DataSourceModel.getStuList();List<List<Student>>  list=new ArrayList<List<Student>>();/*int[][] i=new int[5][];int[] a=new int[]{1,2,3};i[3]=a;System.out.println(i[3][1]);*/  Random r=new Random();  int fenzur=4;  int zsize=13;  for(int i=0;i<(13/fenzur);i++){  List<Student> li= new ArrayList<Student> ();  for(int j=0;j<fenzur;j++){  int a=r.nextInt(4);  li.add(l.get(a));  l.remove(a);  if(l.size()<2){  li.add(l.get(0));  }  }list.add(li);  System.out.println(li);  System.out.println(li.size());  }}}

  

重複資料刪除的對象就也可以用雙層for迴圈來尋找出重複的,也可以將list集合轉化成set集合,因為set不可以重複,所以就將重複的自動刪除了

public static void chachong(List l){for(int a=0;a<l.size();a++){int size=0;for(int b=0;b<l.size();b++){if(l.get(a).equals(l.get(b))){size++;if(size>1){l.remove(b);}}}}}public static void kaitou( List l){//j開頭的名字for(int i=0;i<l.size();i++){if(‘J‘==(l.get(i).toString().charAt(0))){//判斷兩個字元相等可以用==System.out.println(l.get(i));}}}public static void bnianling(List l){//比年齡for(int a=0;a<l.size();a++){if(((Student) l.get(a)).getAge()>25){//括弧的student是強轉成student型??System.out.println(l.get(a));}}}}

  因為map沒有直接的iterator介面,所以可以動過keyset方法獲得索引值,將索引值賦給set集合(因為,list可以重複,而set不可以重複)或者collection集合,就可以實現iterator介面了

Collection<Integer> c=m.keySet();Iterator<Integer> i=c.iterator();    while (i.hasNext()){    int a=i.next();    if(a>=1004&&a<=1009){    System.out.println(a);    if(a==1005){    i.remove();    continue;    }}

  

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.