1, pg235--2 Add "a", "a", "C", "C", "a" 5 elements to the set set and the list collection respectively, observe whether the duplicate value "a" can be successfully added in the list collection and set set.
PackageOrg.hanqi.array;ImportJava.util.*; Public classZuoye { Public Static voidMain (string[] args) {//Set SetSet<string> s=NewHashset<string>(); S.add (A); S.add (A); S.add (C); S.add (C); S.add (A); if(S.size () ==5) {System.out.println ("Duplicate value" a "can be successfully added in the Set collection"); } Else{System.out.println ("Duplicate value" a "cannot be added successfully in the Set collection"); } //List CollectionList<string> l=NewArraylist<string>(); L.add (A); L.add (A); L.add (C); L.add (C); L.add (A); if(L.size () ==5) {System.out.println ("Duplicate value" a "can be successfully added in the list collection"); } Else{System.out.println ("Duplicate value" a "cannot be added successfully in the list collection"); } }}
View Code
2. Pg235--3 creates the map collection, creates an EMP object, adds the created EMP object to the collection, and removes the object with ID 005 from the collection
PackageOrg.hanqi.array;ImportJava.util.*; Public classEMP {PrivateString ID; PrivateString value; PublicEmp (String id,string value) { This. id=ID; This. value=value; } Public Static voidMain (string[] args) {EMP e1=NewEMP ("001", "value1"); EMP E2=NewEMP ("002", "value2"); EMP E3=NewEMP ("003", "Value3"); EMP e4=NewEMP ("004", "Value4"); EMP e5=NewEMP ("005", "Value5"); EMP e6=NewEMP ("006", "Value6"); EMP E7=NewEMP ("007", "Value7"); //Create a mapMap<string,string> m=NewHashmap<string,string>(); //The EMP object is added to the collectionm.put (e1.id, E1.value); M.put (E2.id, E2.value); M.put (E3.id, E3.value); M.put (E4.id, E4.value); M.put (E5.id, E5.value); M.put (E6.id, E6.value); M.put (E7.id, E7.value); System.out.println ("Length =" +m.size ()); //remove an object with ID 005M.remove ("005"); System.out.println ("Length after removal =" +m.size ()); //Traverse for(String t:m.keyset ()) {System.out.println (M.get (t)); } }}
View Code
3, randomly generated four-bit verification code 0--9,a--z,a--z randomly take four numbers as a verification code
PackageOrg.hanqi.array;ImportJava.util.*; Public classZuoYe1 { Public Static voidMain (string[] args) {//Generate verification Code 0--9,a--z,a--z randomly take four number as index value to generate verification codeList<Object> list=NewArraylist<object>(); for(inti=0;i<10;i++) {list.add (i); } for(inti=65;i<=90;i++) {List.add (Char) (i); } for(inti=97;i<=122;i++) {List.add (Char) (i); } for(Object t:list) {System.out.print (t); } System.out.println (); Random R=NewRandom (); //generate a four-digit verification codeSystem.out.print ("Four-digit verification code:"); for(inti=1;i<=4;i++) {System.out.print (List.get (R.nextint (List.size ()))); } }}
View Code
4. Randomly extract 9 non-repeating numbers from 1-28
PackageOrg.hanqi.array;ImportJava.util.*; Public classYaojiang { Public Static voidMain (string[] args) {//TODO Auto-generated method stubsList<Object> list=NewArraylist<object>(); for(inti=1;i<=28;i++) {list.add (i); } Random R=NewRandom (); for(inti=0;i<9;i++) { intt=R.nextint (List.size ()); System.out.print (" " +List.get (t)); List.remove ((List.get (t))); } }}
View Code
Java collection classes after class exercises