集合嵌套和遍歷元素
package Day16; import java.util.ArrayList; public class LX15 { public static void main(String[] args) { //創建集合1-----規定其型別為學生型別 ArrayList<Student> A = new ArrayList<Student>(); //向集合1內添加元素 //創建學生物件并添加元素 Student AA = new Student("劉備",30); Student BB = new Student("關羽",29); Student CC = new Student("張飛",28); //將學生類的元素添加到集合1中 A.add(AA); A.add(BB); A.add(CC); //創建集合2--規定其型別為學生型別 ArrayList<Student> B = new ArrayList<Student>(); //向集合2中添加元素 //創建學生物件并添加元素 Student AAA = new Student("唐僧",30); Student BBB = new Student("孫悟空",29); Student CCC = new Student("豬八戒",28); Student DDD = new Student("沙僧",27); //將學生物件的資訊添加到集合2中 B.add(AAA); B.add(BBB); B.add(CCC); B.add(DDD); //創建集合3-規定其型別為學生型別 ArrayList<Student> C = new ArrayList<Student>(); //向集合3中添加學生物件元素 //創建學生物件 Student AAAA = new Student("宋江",43); Student BBBB = new Student("武松",42); Student CCCC = new Student("魯智深",41); Student DDDD = new Student("吳用",40); //向集合3中添加學生類物件 C.add(AAAA); C.add(BBBB); C.add(CCCC); C.add(DDDD); //創建一個D集合---集合中包含著三個集合 //前三個集合的型別為ArrayList<Student>型別 ArrayList<ArrayList<Student>> D = new ArrayList<ArrayList<Student>>(); //向集合D中添加集合A,B,C元素 D.add(A); D.add(B); D.add(C); //對所有集合元素進行遍歷 //首先對大集合進行遍歷---增強for //for(資料型別 變數 :陣列或者集合名) for(ArrayList<Student> x: D){ //此時遍歷獲取小集合 //對小集合進行增強for的遍歷 //確定遍歷的資料型別 for(Student y : x){ System.out.println(y.getName()+"---"+y.getAge()); } } } }
獲取10個1-20之間的亂數,要求不能重復
1 package Day16; 2 //獲取10個1-20之間的亂數,要求不能重復 3 /* 4 * 分析: 5 * A:創建產生亂數的物件 6 * B:創建一個存盤亂數的集合, 7 * C:定義一個統計變數,從0開始, 8 * D:判斷統計遍歷是否小于10 9 * 是:先產生一個亂數,判斷該亂數在集合中是否存在, 10 * 如果不存在:就添加,統計變數++, 11 * 如果存在:就不搭理它, 12 * 否:不搭理它 13 * E:遍歷集合 14 * */ 15 16 import com.sun.scenario.effect.impl.sw.sse.SSEBlend_SRC_OUTPeer; 17 18 import java.util.ArrayList; 19 import java.util.List; 20 import java.util.Random; 21 22 23 public class Lx16 { 24 public static void main(String[] args) { 25 //首先創建一個亂數生成器物件 26 Random AA = new Random(); 27 // int KK=AA.nextInt(20); 28 // System.out.println(KK); 29 30 //創建一個存盤亂數的集合 31 //其中存盤的型別應該是int型別--由于集合中不能使用基本型別 32 //所以使用int型別的包裝類Integer 33 List<Integer> A = new ArrayList<Integer>(); 34 35 //定義一個統計變數,從0開始 36 int count = 0; 37 //D:判斷統計遍歷是否小于10 38 // * 是:先產生一個亂數,判斷該亂數在集合中是否存在, 39 // * 如果不存在:就添加,統計變數++, 40 // * 如果存在:就不搭理它, 41 // * 否:不搭理它 42 // * E:遍歷集合 43 while(count<10){ 44 //public int nextInt(int n)回傳一個偽亂數,它是取自此亂數生成器序列的、 45 // 在 0(包括)和指定值(不包括)之間均勻分布的 int 值, 46 //以此獲取0-20中的亂數 47 int number = AA.nextInt(20)+1; 48 49 //對獲取的數進行判斷 50 //判斷亂數在集合中是否存在contains判斷集合中是否包含指定元素 51 if(!A.contains(number)){ 52 //如果不存在則進行添加 53 A.add(number); 54 //添加一個count就加1---直到count為10結束while回圈 55 count++; 56 } 57 58 } 59 //遍歷集合 60 //增強for遍歷 61 for(Integer X: A){ 62 System.out.println(X); 63 } 64 65 66 } 67 }
鍵盤錄入多個資料,在控制臺輸出最大值
package Day16; import java.util.ArrayList; import java.util.Arrays; import java.util.Scanner; /* * 鍵盤錄入多個資料,以0結束,要求在控制臺輸出這多個資料中的最大值 * * 分析: * A:創建鍵盤錄入資料物件 * B:鍵盤錄入多個資料,我們不知道多少個,所以用集合存盤 * C:以0結束,這個簡單,只要鍵盤錄入的資料是0,我就不繼續錄入資料了 * D:把集合轉成陣列 * E:對陣列排序 * F:獲取該陣列中的最大索引的值 * */ public class Lx17 { public static void main(String[] args) { //創建鍵盤輸入物件 Scanner sc =new Scanner(System.in); //創建一個存盤資料的集合 //并規定其型別為int型別的包裝類---此處規定的型別為參考型別 //用來接受鍵盤輸入的多個資料 ArrayList<Integer> A = new ArrayList<Integer>(); //對鍵盤錄入的資料進行判斷 //其中不知道鍵盤輸入多少次--故采用while回圈進行 while(true){ System.out.println("請你輸入資料"); //定義鍵盤輸入資料的型別 Integer number = sc.nextInt(); //對鍵盤輸入的資料進行是否為0的判斷 if(number !=0){ //如果鍵盤輸入的資料不等于0,則將該資料添加到集合中 A.add(number); }else{ break; } } //回圈判斷結束---進行集合向陣列轉變 // 把集合轉成陣列 // public <T> T[] toArray(T[] a) // 回傳值型別是Integer[] 首先創建一個Integer的陣列--長度和集合長度一致 Integer[] i = new Integer[A.size()]; //呼叫集合轉陣列的方法 //將集合A轉化為Integer陣列i A.toArray(i); // 對陣列排序 // public static void sort(Object[] a) Arrays.sort(i); //獲取該處最大索引值 //System.out.println(i[i.length-1]); //對獲取資料的形式進行改進 System.out.println("陣列"+show(i)+"中最大值是"+i[i.length-1]); //show(i); } //使用拼接字串功能 public static String show(Integer[] i){ //public StringBuilder()構造一個不帶任何字符的字串生成器, // 其初始容量為 16 個字符, StringBuilder AA = new StringBuilder(); //向字串容器添加物件 //public StringBuilder append(String str)將指定的字串追加到此字符序列, AA.append("["); for (int x=0;x<i.length;x++){ if(x==i.length-1){ AA.append(i[x]); AA.append("]"); break; }else{ AA.append(i[x]); AA.append(","); } } AA.append("]"); //public String toString()回傳此序列中資料的字串表示形式, return AA.toString(); } }
轉載請註明出處,本文鏈接:https://www.uj5u.com/houduan/449040.html
標籤:Java
