一:分析以下需求,並用代碼實現
1.定義List集合,存入多個字符串
2.刪除集合中字符串"def"
3.然后利用迭代器遍歷集合元素並輸出
1 import java.util.ArrayList; 2 import java.util.List; 3 4 public class Topic1 5 { 6 public static void main(String[] args) { 7 ArrayList<String> arrayList = new ArrayList<>(); 8 arrayList.add("dsfsd"); 9 arrayList.add("def"); 10 arrayList.add("ghdh"); 11 arrayList.add("fdgd"); 12 arrayList.add("qewr"); 13 for (int i = 0; i < arrayList.size(); i++) { 14 if (arrayList.get(i)=="def"){ 15 arrayList.remove("def"); 16 } 17 } 18 System.out.println(arrayList); 19 } 20 }
二:分析以下需求,並用代碼實現
1.生成10個1至100之間的隨機整數(不能重復),存入一個List集合
2.然后利用迭代器和增強for循環分別遍歷集合元素並輸出
3.如:15 18 20 40 46 60 65 70 75 91
1 import java.util.ArrayList; 2 import java.util.HashSet; 3 import java.util.Random; 4 5 public class Topic2 { 6 public static void main(String[] args) { 7 ArrayList<Integer> arrayList = new ArrayList<>(); 8 HashSet<Integer> set = new HashSet<>(); 9 Random ra = new Random(); 10 while(set.size()<=10){ 11 int num = ra.nextInt(100)+1; 12 set.add(num); 13 } 14 arrayList.addAll(set); 15 System.out.println(arrayList); 16 } 17 }
三:分析以下需求,並用代碼實現
1.定義List集合,存入多個字符串
2.刪除集合元素字符串中包含0-9數字的字符串(只要字符串中包含0-9中的任意一個數字就需要刪除此整個字符串)
3.然后利用迭代器遍歷集合元素並輸出
1 import java.util.ArrayList; 2 import java.util.Iterator; 3 4 public class Topic3 { 5 public static void main(String[] args) { 6 //1.定義List集合,存入多個字符串 7 ArrayList<String> arrayList = new ArrayList<>(); 8 arrayList.add("dfsd5"); 9 arrayList.add("sdgd"); 10 arrayList.add("fgdsg"); 11 arrayList.add("f1ds"); 12 for (int i = arrayList.size()-1; i>=0; i--) { 13 if(methodDelete(arrayList.get(i))==true){ 14 arrayList.remove(i); 15 } 16 } 17 Iterator<String> it = arrayList.iterator(); 18 while (it.hasNext()){ 19 System.out.print(it.next()+" "); 20 } 21 22 //3.然后利用迭代器遍歷集合元素並輸出 23 } 24 25 //2.刪除集合元素字符串中包含0-9數字的字符串(只要字符串中包含0-9中的任意一個數字就 26 public static boolean methodDelete(String string){ 27 char[] array = string.toCharArray(); 28 for (int i = 0; i < array.length; i++) { 29 if (array[i]>=48 && array[i]<=57){ 30 return true; 31 } 32 } 33 return false; 34 } 35 }
四:分析以下需求,並用代碼實現
1.統計每個單詞出現的次數
2.有如下字符串"If you want to change your fate I think you must come to the dark horse to learn java"(用空格間隔)
3.打印格式:
to=3
think=1
you=2
1 import java.util.HashMap; 2 import java.util.Map; 3 import java.util.Set; 4 5 public class Topic4 { 6 public static void main(String[] args) { 7 String str = "If you want to change your fate I think you must come to the dark horse to learn java"; 8 String strArr[] = str.split(" "); 9 /* for (int i = 0; i < strArr.length; i++) { 10 System.out.print(strArr[i]+" "); 11 }*/ 12 HashMap<String,Integer> map = new HashMap<>(); 13 for (String s : strArr) { 14 /* if (map.containsKey(s)){ 15 //存在 16 Integer value = map.get(s); 17 value++; 18 //不停的覆蓋value值 19 map.put(s,value); 20 } 21 else { 22 //不存在 23 map.put(s,1); 24 }*/ 25 // map.put(c,map.containsKey(c)?map.get(c)+1:1); 26 map.put(s,map.containsKey(s)?map.get(s)+1:1); 27 } 28 Set<Map.Entry<String, Integer>> entrySet = map.entrySet(); 29 for (Map.Entry<String, Integer> entry : entrySet) { 30 System.out.println(entry.getKey()+"="+entry.getValue()); 31 } 32 } 33 }
五:分析以下需求,並用代碼實現
2.定義一個noRepeat()方法,要求對傳遞過來集合中進行元素去重
public static void noRepeat(List<String> al){
contains
}
1 import java.util.ArrayList; 2 import java.util.List; 3 4 public class Topic5 { 5 public static void main(String[] args) { 6 ArrayList<String> arrayList = new ArrayList<>(); 7 arrayList.add("sdgsdg1"); 8 arrayList.add("sdgsdg"); 9 arrayList.add("sdgsdg"); 10 arrayList.add("sdgsdg1"); 11 noRepeat(arrayList); 12 } 13 public static void noRepeat(List<String> al){ 14 for (int i = 0; i < al.size(); i++) { 15 //第一個元素拿出來 如果 后續有元素與之相等,就刪除. 16 String first = al.get(i); 17 for (int j =i+1;j<al.size();j++) 18 { 19 if (first.equals(al.get(j))){ 20 //如果 后面與這個元素相同,就刪除后面的元素 21 al.remove(j); 22 // j--; 23 } 24 } 25 } 26 System.out.println(al); 27 } 28 29 }
補充部分:
一:簡答題:
1. &和&&的區別?
前者是不短路與 ,后者是短路與.
在編寫代碼的時候,使用a&b,即是a為假,也會對b進行判斷.
如果使用的是 a&&b,如果a為假,直接短路得出結果,不會對b進行判斷
2.數據類型有哪些?
數據類型分為:基本數據類型、引用數據類型。
基本數據類型:整數型 byte short int long 、浮點型 float double 、字符型 char、布爾型 boolean(四個類別)。
引用數據類型:字符串、數組、類、接口、枚舉、Lambda
3.成員變量和局部變量的區別(從作用域,存儲位置,初始值,三個方面進行概述)
(1)定義的位置
局部變量:在方法的小括號或者大括號
成員變量:在類中方法外
(2)作用域(范圍)
局部變量:只能在所在的方法中
成員變量:在整個類中
(3)默認值
局部變量:沒有默認值,使用之前必須先賦值,否則就報錯
成員變量:有默認值
(4)內存中的位置
局部變量:跟着方法進入棧內存
成員變量:跟着對象進入堆內存
(5)生命周期
局部變量:隨着方法的進棧而存在,隨着方法的出棧而消失,立刻回收
成員變量:隨着對象的創建而存儲,隨着對象被垃圾回收機制回收而消失,在合適的時候回收
4. 線程和進程有什么區別和聯系?
進程:是指一個內存中運行的應用程序,每個進程都有一個獨立的內存空間,一個應用程序可以同時運行多
個進程;進程也是程序的一次執行過程,是系統運行程序的基本單位;系統運行一個程序即是一個進程從創
建、運行到消亡的過程。
線程:線程是進程中的一個執行單元,負責當前進程中程序的執行,一個進程中至少有一個線程。一個進程
中是可以有多個線程的,這個應用程序也可以稱之為多線程程序。
5. 實現多線程的方式以及區別:
方式一:繼承Thread類
方式二:實現Runnable接口
如果一個類繼承Thread,則不適合資源共享。但是如果實現了Runable接口的話,則很容易的實現資源共享。
總結:
實現Runnable接口比繼承Thread類所具有的優勢:
1. 適合多個相同的程序代碼的線程去共享同一個資源。
2. 可以避免java中的單繼承的局限性。
3. 增加程序的健壯性,實現解耦操作,代碼可以被多個線程共享,代碼和線程獨立。
4. 線程池只能放入實現Runable或Callable類線程,不能直接放入繼承Thread的類。
6.start()和run方法有什么區別?
start():該方法是在當前線程中啟動一個新的線程,而新啟動的線程會調用run()方法,同時該方法不能重復調用;
run() :該方法和普通的方法一樣,可以重復執行,不會創建新的線程。
7.Map集合的兩種遍歷方式:
(1)第一種方式通過鍵找值,因為map中鍵是唯一的.使用keySet()方法返回Map中的所有鍵.
食用方式: Set<數據類型> keys = map.keySet();
(2)通過Entry鍵值對對象進行遍歷
食用方式:Set<Entry<類型1,類型2> entrySet = map.entrySet();
然后遍歷entrySet集合就行
for(Entry<類型1,類型2> entry: entrySet)
1.:根據需求完成代碼:
1.定義動物類
屬性:
年齡,顏色
行為:
eat(String something)方法(無具體行為,不同動物吃的方式和東西不一樣,something表示吃的東西)
生成空參有參構造,set和get方法
2.定義狗類繼承動物類
行為:
eat(String something)方法,看家lookHome方法(無參數)
3.定義貓類繼承動物類
行為:eat(String something)方法,逮老鼠catchMouse方法(無參數)
4.定義Person類
屬性:
姓名,年齡
行為:
keepPet(Dog dog,String something)方法
功能:喂養寵物狗,something表示喂養的東西
行為:
keepPet(Cat cat,String something)方法
功能:喂養寵物貓,something表示喂養的東西
生成空參有參構造,set和get方法
5.定義測試類(完成以下打印效果):
keepPet(Dog dog,String somethind)方法打印內容如下:
年齡為30歲的老王養了一只黑顏色的2歲的寵物
2歲的黑顏色的狗兩只前腿死死的抱住骨頭猛吃
keepPet(Cat cat,String somethind)方法打印內容如下:
年齡為25歲的老李養了一只灰顏色的3歲的寵物
3歲的灰顏色的貓眯着眼睛側着頭吃魚
6.思考:
1.Dog和Cat都是Animal的子類,以上案例中針對不同的動物,定義了不同的keepPet方法,過於繁瑣,能否簡化,並體會簡化后的好處?
2.Dog和Cat雖然都是Animal的子類,但是都有其特有方法,能否想辦法在keepPet中調用特有方法?
定義Animal類:
1 package topic1; 2 3 public abstract class Animal { 4 /*年齡,顏色*/ 5 private int age; 6 private String color; 7 /*eat(String something)方法(無具體行為,不同動物吃的方式和東西不一樣,something表示吃的東西)*/ 8 public abstract void eat(String something); 9 /* 生成空參有參構造,set和get方法*/ 10 11 public Animal() { 12 } 13 14 public Animal(int age, String color) { 15 this.age = age; 16 this.color = color; 17 } 18 19 public int getAge() { 20 return age; 21 } 22 23 public void setAge(int age) { 24 this.age = age; 25 } 26 27 public String getColor() { 28 return color; 29 } 30 31 public void setColor(String color) { 32 this.color = color; 33 } 34 }
Cat類
1 package topic1; 2 3 public class Cat extends Animal { 4 @Override 5 public void eat(String something) { 6 System.out.println("眯着眼睛側着頭"+something); 7 } 8 public void CatchMouse(){ 9 System.out.println("貓正在抓老鼠"); 10 } 11 12 public Cat() { 13 } 14 15 public Cat(int age, String color) { 16 super(age, color); 17 } 18 }
Dog類
1 package topic1; 2 3 public class Dog extends Animal { 4 @Override 5 public void eat(String something) { 6 System.out.println("兩只前腿死死的抱住"+something+"猛吃"); 7 } 8 public void lookHome(){ 9 System.out.println("狗正在看家"); 10 } 11 12 public Dog() { 13 } 14 15 public Dog(int age, String color) { 16 super(age, color); 17 } 18 }
Person類
1 package topic1; 2 3 import java.text.DateFormat; 4 5 public class Person { 6 /* 姓名,年齡*/ 7 private String name; 8 private int age; 9 /*行為: 10 keepPet(Dog dog,String something)方法 11 功能:喂養寵物狗,something表示喂養的東西 12 */ 13 /* public void keepPet(Dog dog,String something){ 14 System.out.printf("年齡為%d的%s養了一只%s顏色的%d歲的寵物",this.age,this.name,dog.getColor(),dog.getAge()); 15 System.out.printf("%d歲的%s的狗兩只前腿死死的包住骨頭猛吃",dog.getAge(),dog.getColor()); 16 } 17 public void keepPet(Cat cat,String something){ 18 System.out.printf("年齡為%d的%s養了一只%s顏色的%d歲的寵物",this.age,this.name,cat.getColor(),cat.getAge()); 19 System.out.printf("%d歲的%s的貓眯着眼睛側着頭吃魚",cat.getAge(),cat.getColor()); 20 }*/ 21 public void keepPet(Object obj,String something){ 22 if(obj instanceof Dog){ 23 System.out.printf("年齡為%d的%s養了一只%s顏色的%d歲的寵物\n",this.age,this.name,((Dog) obj).getColor(),((Dog) obj).getAge()); 24 System.out.printf("%d歲的%s顏色的狗",((Dog) obj).getAge(),((Dog) obj).getColor()); 25 ((Dog) obj).eat(something); 26 } 27 else if ((obj instanceof Cat)){ 28 System.out.printf("年齡為%d的%s養了一只%s顏色的%d歲的寵物\n",this.age,this.name,((Cat) obj).getColor(), ((Cat) obj).getAge()); 29 System.out.printf("%d歲的%s顏色的貓",((Cat) obj).getAge(),((Cat) obj).getColor()); 30 ((Cat) obj).eat(something); 31 } 32 } 33 34 /*生成空參有參構造,set和get方法 */ 35 36 public String getName() { 37 return name; 38 } 39 40 public void setName(String name) { 41 this.name = name; 42 } 43 44 public int getAge() { 45 return age; 46 } 47 48 public void setAge(int age) { 49 this.age = age; 50 } 51 52 public Person(String name, int age) { 53 54 this.name = name; 55 this.age = age; 56 } 57 58 public Person() { 59 60 } 61 }
測試類:
1 package topic1; 2 3 public class TestClass { 4 public static void main(String[] args) { 5 Person p = new Person("老王",30); 6 Person p2 = new Person("老李",25); 7 Dog dog = new Dog(2,"黑"); 8 Cat cat = new Cat(3,"灰"); 9 p.keepPet(dog,"骨頭"); 10 p2.keepPet(cat,"吃魚"); 11 } 12 }
2.產生10個長度為10的不能重復的字符串(里面只能出現大寫字母、小寫字母、0-9的數字),並遍歷打印輸出
1 package topic2; 2 3 import java.util.LinkedHashSet; 4 import java.util.Random; 5 6 public class Topic2 { 7 public static void main(String[] args) { 8 method3(); 9 } 10 private static void method3() { 11 LinkedHashSet<String> set = new LinkedHashSet<>(); 12 while(set.size()<10) 13 { 14 set.add(makeArray()); 15 16 } 17 System.out.println(set); 18 19 } 20 /*產生長度為10的不能重復的字符串(里面只能出現大寫字母、小寫字母、0-9的數字)*/ 21 private static String makeArray(){ 22 String strValue = ""; 23 //1.先創建一個字符數組,題目要求長度為10,即是字符數組的上限為10 24 char array[] = new char[10]; 25 //2.隨機生成 一個flag標記值,用來隨機生成 小寫字母 大寫字母 數字 26 Random ra = new Random(); 27 for (int i = 0; i < array.length; i++) { 28 //獲得flag標記值 29 //flag為 0 標記 整數 30 //flag為 1 標記 小寫字母 31 //flag為 2 標記 大寫字母 32 33 int flag = ra.nextInt(3); 34 if (flag == 0) 35 { 36 int zhengshu = new Random().nextInt(10); 37 array[i]=(char)(48+zhengshu); 38 39 } 40 else if (flag ==1){ 41 int xiaoxie = new Random().nextInt(26); 42 array[i]=(char)('a'+xiaoxie); 43 } 44 else 45 { 46 int daxie = new Random().nextInt(26); 47 array[i]=(char)('A'+daxie); 48 } 49 } 50 for (int i = 0; i < array.length; i++) { 51 strValue+=array[i]; 52 } 53 return strValue; 54 } 55 56 }
3.鍵盤錄入一個字符串,統計每個字母出現的次數(不用按照字母的順序排列),按照如下格式打印:
* a(10)b(2000)c(30)d(400)...
1 package topic3; 2 3 import java.util.*; 4 5 public class Topic3 { 6 public static void main(String[] args) { 7 System.out.println("請輸入隨便一個字符串"); 8 Scanner sc = new Scanner(System.in); 9 String str = sc.nextLine(); 10 method1(str); 11 12 13 14 } 15 private static void method1(String str) { 16 17 //1. 創建Map集合,key是字符串中的字符,value是字符的個數 18 //由於HashMap具有篩選功能,可以幫助我們對字符進行統計. 19 HashMap<Character,Integer> map = new HashMap<>(); 20 //2.將形式參數傳遞過來的字符串使用toCharArray()的方法轉換成Char類型的字符數組.c用來遍歷獲取字符數組中的每一個值. 21 for(char c :str.toCharArray()){ 22 //對於字符串"abcbcd"為例,char c =a; map.containKey(a)為false,執行:號后的1 23 //map.put(a,1); 這樣就將a字符與對應的數量添加到了map集合中. 24 map.put(c,map.containsKey(c)?map.get(c)+1:1); 25 } 26 /* //獲取最后一個key 27 Set<Character> chrs = map.keySet(); 28 List list = new ArrayList(chrs); 29 char lastKey = (char)(list.get(list.size()-1));*/ 30 //char lastKey = (char)list.lastIndexOf("d"); 31 // System.out.println(lastKey); 32 for(Map.Entry<Character,Integer> entry : map.entrySet()){ 33 char key = entry.getKey(); 34 int value = entry.getValue(); 35 //如果是最后一個key直接打印key與value結束. 36 /* if (key == lastKey) 37 { 38 System.out.println(key+"="+value); 39 break; 40 }*/ 41 //如果不是最后一個,打印 key與value和一個逗號分隔 42 System.out.print(key+"("+value+")"); 43 44 } 45 46 } 47 48 }
4.分析以下需求,並用代碼實現 定義Student類包含姓名:String name、年齡:int age、成績:int
* score,生成空參、有參構造、set和get方法、toString方法
1.創建10個學生對象(其中有兩個學員信息完全相同)存入集合中
2.去除重復的學員並遍歷打印剩余學員信息
3.打印最高分的學員信息
4.打印平均分
5.打印不及格的學員信息及數量
創建一個Student類
1 package topic4; 2 3 public class Student { 4 /*姓名:String name、年齡:int age、成績:int*/ 5 private String name; 6 private int age; 7 private int score; 8 /*生成空參、有參構造、set和get方法、toString方法*/ 9 10 @Override 11 public String toString() { 12 return "Student{" + 13 "name='" + name + '\'' + 14 ", age=" + age + 15 ", score=" + score + 16 '}'; 17 } 18 19 public String getName() { 20 return name; 21 } 22 23 public void setName(String name) { 24 this.name = name; 25 } 26 27 public int getAge() { 28 return age; 29 } 30 31 public void setAge(int age) { 32 this.age = age; 33 } 34 35 public int getScore() { 36 return score; 37 } 38 39 public void setScore(int score) { 40 this.score = score; 41 } 42 43 public Student(String name, int age, int score) { 44 45 this.name = name; 46 this.age = age; 47 this.score = score; 48 } 49 50 public Student() { 51 52 } 53 }
測試類:
1 package topic4; 2 3 import java.util.ArrayList; 4 5 public class Topic4 { 6 public static void main(String[] args) { 7 /* 1.創建10個學生對象(其中有兩個學員信息完全相同)存入集合中*/ 8 ArrayList<Student> arrayList = new ArrayList<>(); 9 arrayList.add(new Student("學生1",18,90)); 10 arrayList.add(new Student("學生2",18,80)); 11 arrayList.add(new Student("學生3",18,70)); 12 arrayList.add(new Student("學生4",18,60)); 13 arrayList.add(new Student("學生5",18,50)); 14 arrayList.add(new Student("學生6",18,40)); 15 arrayList.add(new Student("學生7",18,30)); 16 arrayList.add(new Student("學生8",18,20)); 17 arrayList.add(new Student("學生9",18,10)); 18 arrayList.add(new Student("學生1",18,90)); 19 /*2.去除重復的學員並遍歷打印剩余學員信息*/ 20 int delFlag =0;//重復刪除元素標記 21 int highScoreFlag=0;//最高分標記 22 int count=0;//總成績 23 for (int i = 0; i < arrayList.size(); i++) { 24 for(int j=1;j<arrayList.size();j++){ 25 if (arrayList.get(i)==arrayList.get(j)) 26 { 27 delFlag=j; 28 } 29 } 30 31 } 32 //刪除重復學員 33 arrayList.remove(delFlag); 34 for (Student student : arrayList) { 35 System.out.println(student); 36 } 37 // 3.打印最高分的學員信息 38 int score = arrayList.get(1).getScore(); 39 for (int i = 1; i < arrayList.size(); i++) { 40 if (arrayList.get(i).getScore()>score) 41 { 42 // score=arrayList.get(i).getScore(); 43 highScoreFlag=i; 44 } 45 } 46 System.out.println("最高分的學員為"+arrayList.get(highScoreFlag)); 47 // 4.打印平均分 48 for (int i = 0; i < arrayList.size(); i++) { 49 count+=arrayList.get(i).getScore(); 50 } 51 System.out.println("平均分為"+(double)count/(double)arrayList.size()); 52 // 5.打印不及格的學員信息及數量 53 int bujigeCount=0; 54 System.out.println("不及格的人為:"); 55 for (int i = 0; i < arrayList.size(); i++) { 56 if (arrayList.get(i).getScore()<60) 57 { 58 System.out.print(arrayList.get(i).getName()+" "); 59 bujigeCount++; 60 } 61 } 62 System.out.println("不及格的總人數為"+bujigeCount); 63 } 64 }