Four Java mini-puzzles

Source: Internet
Author: User

a : analyze the following requirements and implement them with code

1. defining a List Collection, depositing multiple strings

2. Delete the string "Def" in the collection

3. then iterate through the collection elements and output

1 Importjava.util.ArrayList;2 Importjava.util.List;3 4  Public classTopic15 {6      Public Static voidMain (string[] args) {7Arraylist<string> ArrayList =NewArraylist<>();8Arraylist.add ("DSFSD");9Arraylist.add ("Def");TenArraylist.add ("GHDH"); OneArraylist.add ("FDGD"); AArraylist.add ("QEWR"); -          for(inti = 0; I < arraylist.size (); i++) { -             if(Arraylist.get (i) = = "Def"){ theArraylist.remove ("Def"); -             } -         } - System.out.println (arrayList); +     } -}

two : analyze the following requirements and implement them with code

1. generate a random integer between 1 and Cannot be duplicated ), in a List Collection

2. then use the iterator and the enhanced for Loop to iterate through the collection elements and output

3. for example, the following:

1 Importjava.util.ArrayList;2 ImportJava.util.HashSet;3 ImportJava.util.Random;4 5  Public classTopic2 {6      Public Static voidMain (string[] args) {7Arraylist<integer> ArrayList =NewArraylist<>();8Hashset<integer> set =NewHashset<>();9Random RA =NewRandom ();Ten          while(Set.size () <=10){ One             intnum = ra.nextint (100) +1; A set.add (num); -         } - Arraylist.addall (set); the System.out.println (arrayList); -     } -}

three : analyze the following requirements and implement them with code

1. defining a List Collection, depositing multiple strings

2. Delete The string containing the 0-9 number in the collection element string ( as long as the string contains 0-9 You need to delete this entire string )

3. then iterate through the collection elements and output

1 Importjava.util.ArrayList;2 ImportJava.util.Iterator;3 4  Public classTOPIC3 {5      Public Static voidMain (string[] args) {6         //1. Defining a list collection, depositing multiple strings7Arraylist<string> ArrayList =NewArraylist<>();8Arraylist.add ("Dfsd5");9Arraylist.add ("SDGD");TenArraylist.add ("FGDSG"); OneArraylist.add ("f1ds"); A          for(inti = Arraylist.size ()-1; i>=0; i--) { -             if(Methoddelete (Arraylist.get (i)) = =true){ - Arraylist.remove (i); the             } -         } -Iterator<string> it =arraylist.iterator (); -          while(It.hasnext ()) { +System.out.print (It.next () + ""); -         } +  A         //3. Then iterate through the collection elements and output at     } -  -     //2. Delete the string containing 0-9 digits in the collection element string (as long as any number in the string contains 0-9 -      Public Static BooleanMethoddelete (String string) { -         Char[] Array =String.tochararray (); -          for(inti = 0; i < Array.Length; i++) { in             if(array[i]>=48 && array[i]<=57){ -                 return true; to             } +         } -         return false; the     } *}

Four : analyze the following requirements and implement them with code

1. count The occurrences of each word

2. the following string "If you want to change your fate I Think you must come to the dark horse to learn Java" ( with space interval )

3. printing format:

To=3

Think=1

you=2

1 ImportJava.util.HashMap;2 ImportJava.util.Map;3 ImportJava.util.Set;4 5  Public classTopic4 {6      Public Static voidMain (string[] args) {7String str = "If you want to change your fate I Think you must come to the dark horse to learn Java";8String strarr[] = Str.split ("");9        /*for (int i = 0; i < strarr.length; i++) {Ten System.out.print (strarr[i]+ ""); One         }*/ Ahashmap<string,integer> map =NewHashmap<>(); -          for(String s:strarr) { -           /*if (Map.containskey (s)) { the //Presence - Integer value = Map.get (s); - value++; - //Overwrite value value continuously + Map.put (s,value); -             } + else { A //does not exist at Map.put (s,1); -             }*/ -           //Map.put (C,map.containskey (c)? Map.get (c) +1:1); -Map.put (S,map.containskey (s)? Map.get (s) +1:1); -         } -set<map.entry<string, integer>> entryset =Map.entryset (); in          for(Map.entry<string, integer>Entry:entryset) { -System.out.println (Entry.getkey () + "=" +Entry.getvalue ()); to         } +     } -}

Five: Analyze the following requirements and implement them with code

2. define a norepeat () method that requires the element to be passed over the set

public static void Norepeat (List<string> al) {

Contains

}

1 Importjava.util.ArrayList;2 Importjava.util.List;3 4  Public classTOPIC5 {5      Public Static voidMain (string[] args) {6Arraylist<string> ArrayList =NewArraylist<>();7Arraylist.add ("SDGSDG1");8Arraylist.add ("SDGSDG");9Arraylist.add ("SDGSDG");TenArraylist.add ("SDGSDG1"); One norepeat (arrayList); A     } -      Public Static voidNorepeat (list<string>al) { -          for(inti = 0; I < al.size (); i++) { the             //The first element is removed if the subsequent elements are equal. -String first =Al.get (i); -              for(intJ =i+1;j<al.size (); j + +) -             { +                 if(First.equals (Al.get (j))) { -                     //If the following element is the same, delete the following element + Al.remove (j); A                    //j--; at                 } -             } -         } - System.out.println (AL); -     } -  in}

Four Java mini-puzzles

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

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.