ArrayList
一、ArrayList創建變量的步驟
1.導入包
import java.util.*;
2.創建引用類型的變量
數據類型<集合存儲的數據類型> 變量名 = new 數據類型<集合存儲的數據類型>();
集合存儲的數據類型:要將數據存儲到集合的容器中。創建集合引用變量的時候,必須要指定好存儲的類型是什么。
3.變量名.方法即可調用
注意尖括號中的類型必須要寫。
注意:ArrayList存儲的是引用類型,那么8個基本的類型對應8個引用類型。
如下表所示:
表1基本數據類型&引用數據類型參照表
| 基本數據類型 |
對應的引用數據類型 |
| byte |
Byte |
| short |
Short |
| int |
integer |
| long |
Long |
| float |
Float |
| double |
Double |
| char |
Character |
| boolean |
Boolean |
分析以下需求並實現
1.創建一個ArrayList集合,用於存儲一些字符串:"abc","def","def",
"ghi","def","hij","jkol"
2.遍歷集合,統計集合中"def"字符串一共出現了多少個
3.將集合中的所有"def"字符串刪除。打印刪除后的集合元素
1 package pers.ccsoft.lucifer; 2 3 import java.util.ArrayList; 4 5 /* 6 * 第四題:分析以下需求並實現 7 1.創建一個ArrayList集合,用於存儲一些字符串:"abc","def","def","ghi","def","hij","jkol" 8 2.遍歷集合,統計集合中"def"字符串一共出現了多少個 9 3.將集合中的所有"def"字符串刪除。打印刪除后的集合元素 10 創建時間:2018年7月22日20:42:24 11 創建者:ccsoftlucifer 12 * */ 13 public class StringArrayListText { 14 public static void main(String[] args){ 15 ArrayList<String> obj = new ArrayList<String>(); 16 obj.add("abc"); 17 obj.add("def"); 18 obj.add("def"); 19 obj.add("ghi"); 20 obj.add("def"); 21 obj.add("hij"); 22 obj.add("jkol"); 23 System.out.println("當前的集合中的元素為:"); 24 for(int i=0;i<obj.size();i++) 25 { 26 System.out.print(obj.get(i)+" "); 27 } 28 System.out.println(); 29 30 int count=0; 31 32 for(int i=0;i<obj.size();i++) 33 { 34 if(obj.get(i).equals("def")){ 35 count++; 36 } 37 } 38 System.out.println("def一共出現了"+count+"次"); 39 40 System.out.println("開始刪除集合中的def字符串,請稍候..."); 41 for(int i=0;i<obj.size();i++) 42 { 43 if(obj.get(i).equals("def")){ 44 obj.remove(i); 45 i-=1;//remove方法 會改變ArrayList的索引,需要重新掃描該位置的元素,以免漏掉 46 } 47 } 48 System.out.println("當前的集合中的元素為:"); 49 for(int i=0;i<obj.size();i++) 50 { 51 System.out.print(obj.get(i)+" "); 52 } 53 } 54 }
程序執行的結果為:

如果刪掉第45行,會出現一個問題:

會發現有一個元素並沒有刪除,這是因為在對 abc def def ……元素進行刪除的時候,第二次循環找到了def, 使用了remove方法將這個元素刪除,與此同時,
def右側的所有元素左移,第二個def移動到了第一個def的位置,當索引繼續遍歷的時候,相當於跳過了第二個def,使得結果有誤。所以要在這個地方使索引
回退一個,然后繼續遍歷,這樣結果就復合預期。
題目:
1.玩法說明:
*雙色球投注區分為紅球號碼區和藍球號碼區,紅球號碼范圍為01~33,藍球號碼范圍為01~16。
*雙色球每期從33個紅球中開出6個號碼,從16個藍球中開出1個號碼作為中獎號碼,
*雙色球玩法即是競猜開獎號碼的6個紅球號碼和1個藍球號碼,順序不限 。
1 public class Topic3 { 2 public static void main(String[] args) { 3 String stringArr[] = new String[7]; 4 //先確定藍球位置 標記為flag. 5 int flag = 0; 6 Random ra = new Random(); 7 int blueBallIndex = ra.nextInt(7); 8 stringArr[blueBallIndex]=method2(); 9 for (int i = 0; i < stringArr.length; i++) { 10 if(i==blueBallIndex) 11 continue; 12 stringArr[i]=method1(); 13 } 14 for (int i = 0; i < stringArr.length; i++) { 15 System.out.print(stringArr[i]+" "); 16 } 17 } 18 //method1 返回紅球字符串 19 public static String method1(){ 20 ArrayList<String> arrayList = new ArrayList<>(); 21 for (int i = 0; i < 33; i++) { 22 Integer temp = (i+1); 23 arrayList.add(temp.toString()); 24 } 25 Random ra = new Random(); 26 int randomValue = ra.nextInt(33); 27 return "紅球"+arrayList.get(randomValue); 28 } 29 //method2 返回藍球字符串 30 public static String method2(){ 31 ArrayList<String> arrayList = new ArrayList<>(); 32 for (int i = 0; i < 6; i++) { 33 Integer temp = (i+1); 34 arrayList.add(temp.toString()); 35 } 36 Random ra = new Random(); 37 int randomValue = ra.nextInt(6); 38 return "藍球"+arrayList.get(randomValue); 39 } 40 41 }
1.實現學生數據的錄入功能
* 2.實現學生數據的查詢功能
* 3.實現學生數據的遍歷功能
* 4.實現學生數據的刪除功能
* 5.實現學生數據的更改功能
1 import javafx.application.Platform; 2 3 import java.lang.reflect.Array; 4 import java.util.*; 5 import java.awt.*; 6 /* 7 * 程序功能:學生信息錄入程序 8 * 1.實現學生數據的錄入功能 9 * 2.實現學生數據的查詢功能 10 * 3.實現學生數據的遍歷功能 11 * 4.實現學生數據的刪除功能 12 * 5.實現學生數據的更改功能 13 * */ 14 public class ArrayTest { 15 static ArrayList<Student> stu = new ArrayList<Student>(); 16 17 public static void anyKeyContinune() 18 { 19 System.out.println("請按任意鍵繼續"); 20 Scanner input = new Scanner(System.in); 21 String str = input.next(); 22 } 23 /*menu():該靜態方法用來顯示目錄字符串信息*/ 24 public static void menu(){ 25 System.out.println("學生信息錄入程序\n\t1.添加學生信息\n\t2.查詢學生信息\n\t" + 26 "3.查詢所有學生信息\n\t4.刪除指定學生信息\n\t5.更改指定學生信息\n\t0.退出\n\n請選擇:"); 27 } 28 /*添加學生信息*/ 29 public static void addStu() 30 { 31 /* 1.學生姓名name 32 * 2.學生學號sno 33 * 3.學生成績score*/ 34 Student no1 = new Student(); 35 Scanner sc = new Scanner(System.in); 36 System.out.println("請添加學生的姓名"); 37 no1.name=sc.nextLine();//用nextLine()方法獲取字符串 38 System.out.println("請添加學生的學號"); 39 no1.sno=sc.nextLong(); 40 System.out.println("請添加學生的成績"); 41 no1.score =sc.nextInt(); 42 stu.add(no1); 43 //System.out.println(stu.get(0).name +" "+ stu.get(0).sno + " "+stu.get(0).score); 44 45 } 46 /*查詢學生信息 默認姓名查詢*/ 47 public static void searchName() 48 { 49 System.out.println("請輸入想要查詢的姓名:"); 50 Scanner sc = new Scanner(System.in); 51 int flag =0; 52 String searName = sc.nextLine(); 53 for(int i=0;i<stu.size();i++) 54 { 55 if(stu.get(i).name.equals(searName)) 56 { 57 System.out.println("學生的信息是:\nname\t sno\t\t score\n"+stu.get(i).name +" "+ stu.get(i).sno + " "+stu.get(i).score); 58 flag=1; 59 } 60 } 61 if(flag==0) 62 System.out.println("不好意思,查無此人!"); 63 anyKeyContinune(); 64 } 65 66 /*3.查詢所有學生信息*/ 67 public static void View() 68 { 69 System.out.println("所有的學生信息如下\nname\t sno\t\t score"); 70 for(int i=0;i<stu.size();i++) 71 System.out.println(stu.get(i).name +" "+ stu.get(i).sno + " "+stu.get(i).score); 72 anyKeyContinune(); 73 } 74 /*4.刪除指定學生信息*/ 75 public static void deleteStu() 76 { 77 System.out.println("請輸入想要查詢的姓名:"); 78 Scanner sc = new Scanner(System.in); 79 int flag =0; 80 String searName = sc.nextLine(); 81 int i; 82 for( i=0;i<stu.size();i++) 83 { 84 if(stu.get(i).name.equals(searName)) 85 { 86 System.out.println("學生的信息是:\nname\t sno\t\t score\n"+stu.get(i).name +" "+ stu.get(i).sno + " "+stu.get(i).score); 87 flag =1; 88 } 89 } 90 if(flag==0) 91 System.out.println("不好意思,查無此人!"); 92 else{ 93 94 System.out.println("是否想要刪除? yes or no ?"); 95 String conform = sc.nextLine(); 96 if(conform.equals("yes")) 97 { 98 System.out.println("正在刪除,請稍后!"); 99 stu.remove(i-1); 100 System.out.println("刪除成功!"); 101 } 102 else 103 { 104 System.out.println("輸入錯誤 或者 不想刪除!請想清楚后再嘗試!"); 105 } 106 } 107 108 anyKeyContinune(); 109 } 110 /*5.更改指定學生信息*/ 111 public static void change() 112 { 113 System.out.println("請輸入想要修改的學生信息,以姓名查找:"); 114 int flag =0; 115 Scanner sc = new Scanner(System.in); 116 String searName = sc.nextLine(); 117 int i; 118 for( i=0;i<stu.size();i++) 119 { 120 if(stu.get(i).name.equals(searName)) 121 { 122 System.out.println("學生的信息是:\nname\t sno\t\t score\n"+stu.get(i).name +" "+ stu.get(i).sno + " "+stu.get(i).score); 123 flag=1; 124 } 125 } 126 if(flag==0) 127 System.out.println("不好意思,查無此人!"); 128 else { 129 // Scanner sc = new Scanner(System.in); 130 Student p1 = new Student(); 131 132 System.out.println("請重新輸入學生的基本信息\n請重新輸入學生的姓名"); 133 p1.name=sc.nextLine();//用nextLine()方法獲取字符串 134 System.out.println("請重新輸入的學號"); 135 p1.sno=sc.nextLong(); 136 System.out.println("請重新輸入的成績"); 137 p1.score=sc.nextInt(); 138 stu.set(i-1,p1); 139 System.out.print("修改成功!"); 140 } 141 142 143 } 144 public static void main(String[] args) throws Exception{ 145 for (;;) 146 { 147 //顯示菜單 148 menu(); 149 //選擇選項 150 int choice; 151 Scanner sc = new Scanner(System.in); 152 //ArrayList<Student> stu = new ArrayList<Student>(); 153 choice = sc.nextInt(); 154 if (choice==1){ 155 addStu(); 156 } 157 else if(choice==2){ 158 searchName(); 159 } 160 else if(choice==3){ 161 View(); 162 } 163 else if (choice==4){ 164 deleteStu(); 165 } 166 else if (choice==5){ 167 change(); 168 } 169 else if (choice==0){ 170 System.out.print("程序正在停止,請稍等"); 171 Robot r = new Robot(); 172 for(int i=1;i<4;i++) 173 { 174 r.delay(500); 175 System.out.print("."); 176 } 177 break; 178 } 179 else 180 System.out.println("請輸入上述選項!"); 181 } 182 183 184 185 } 186 }
