1. 程式人生 > >四道java小題

四道java小題

iterator get println length ger 代碼 相同 method tor

:分析以下需求,並用代碼實現

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.生成101100之間的隨機整數(不能重復),存入一個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 }

四道java小題