Java Collection Exercises--topics

Source: Internet
Author: User

2. It is known that there are 16 men's football teams attending the 2008 Beijing Olympic Games. Write a program that divides the 16 teams randomly into 4 groups. Adopt List collection and random number

2008 Beijing Olympic Games men's race countries:

Côte d ' Ivoire, Argentina, Australia, Serbia, Netherlands, Nigeria, Japan, USA, China, new West Orchid, Brazil, Belgium, Korea, Cameroon, Honduras, Italy

Importjava.util.ArrayList;Importjava.util.List;ImportJava.util.Random; Public classFenzu { Public Static voidMain (string[] args) {List<String> qiudui=NewArraylist<>(); Qiudui.add (Ivorian); Qiudui.add (Argentina); Qiudui.add (Australia); Qiudui.add (Serbia); Qiudui.add (Netherlands); Qiudui.add (Nigeria); Qiudui.add (Japan); Qiudui.add (United States); Qiudui.add (China); Qiudui.add (New Zealand); Qiudui.add (Brazil); Qiudui.add (Belgium); Qiudui.add (Korea); Qiudui.add (Cameroon); Qiudui.add (Honduras); Qiudui.add (Italy); Random a=NewRandom ();  for(inti=0;i<4;i++) {System.out.print ("First" + (i+1) + "group:");  for(intj=0;j<4;j++) {String str=Qiudui.get (A.nextint (Qiudui.size ())); System.out.print (str+" ");            Qiudui.remove (str);        } System.out.println (); }    }}

3, have the following Student object,

private String name;

private int age;

private int score;

Private String Classnum;

Among them, Classnum represents the student's class number, for example "Class05". have the following List

List List = new ArrayList ();

List.add (New Student ("Tom", "Max", "Class05"));

List.add (New Student ("Jerry", "a", "Class04"));

List.add (New Student ("Owen", "Class05"));

List.add (New Student ("Jim", 30,80, "Class05"));

List.add ("Steve", "Student", "Class06"));

List.add (New Student ("Kevin", "Max", "Class04"));

On the basis of this list, complete the following requirements:

1) Calculate the average age of all students

2) calculate the average score for each class

Importjava.util.ArrayList;Importjava.util.List; Public classStudent {PrivateString name; Private intAge ; Private intscore; PrivateString Classnum; Student (String name,intAgeintscore, String classnum) {         This. name=name;  This. age=Age ;  This. score=score;  This. classnum=Classnum; }     Public Static voidMain (string[] args) {List<Student> list=NewArraylist<>(); List.add (NewStudent ("Tom", 18,100, "Class05")); List.add (NewStudent ("Jerry", 22,70, "Class04")); List.add (NewStudent ("Owen", 25,90, "Class05")); List.add (NewStudent ("Steve", 30,80, "Class05")); List.add (NewStudent ("Kevin", 28,66, "Class06")); List.add (NewStudent ("Tom", 18,100, "Class05")); intSum=0; intSumscore=0;  for(Student stu:list) {sum+=Stu.age; Sumscore+=Stu.score; } System.out.println ("Average age is:" +sum/list.size ()); System.out.println ("The average score is:" +sumscore/list.size ()); }}

5. The(Map) Design account object is as follows:

Private long ID;

private double balance;

private String password;

Requires a perfect design so that the account object can automatically assign IDs. Given a list as follows:

List List = new ArrayList ();

List.add (New account (10.00, "1234"));

List.add (New Account (15.00, "5678"));

List.add (New account (0, "1010"));

Requires the contents of the list to be placed in a map, the map key is ID, the value is the corresponding account object. Finally, traverse the map to print the ID and balance of all account objects.

Importjava.util.ArrayList;Importjava.util.List;ImportJava.util.Random; Public classAccount1 {Private LongID; Private Doublebalance; PrivateString password; List<Long> idjihe=NewArraylist<>(); Account1 (Doublebalance,string Password) {         This. balance=balance;  This. password=password; Random a=NewRandom (); Booleans=true;  while(s) {ID=A.nextlong (); if(id>0){                 This. id=ID; S=false; }            }        }     Public LonggetId () {returnID; }     Public voidSetId (LongID) { This. id=ID; }     Public DoubleGetBalance () {returnbalance; }     Public voidSetbalance (Doublebalance) {         This. Balance =balance; }     PublicString GetPassword () {returnpassword; }     Public voidSetPassword (String password) { This. Password =password; }    }
Importjava.util.ArrayList;ImportJava.util.HashMap;Importjava.util.List;ImportJava.util.Map; Public classTestaccount { Public Static voidMain (string[] args) {List<Account1> list=NewArraylist<>(); List.add (NewAccount1 (10.00, "1234")); List.add (NewAccount1 (15.00, "5678")); List.add (NewAccount1 (0, "1010")); Map<Long,Account1> map=NewHashmap<>();  for(Account1 temp:list) {map.put (Temp.getid (), temp); }         for(Long id:map.keySet ()) {System.out.println ("Id=" +id+ "balance is:" +map.get (ID). GetBalance ()); }    }}

Java Collection Exercises--topics

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.