四道java小題

來源:互聯網
上載者:User

標籤:集合   false   bool   遍曆   div   產生   list   次數   for   

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

    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小題

相關文章

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在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.