11th Chapter: Collection (II.)

Source: Internet
Author: User

Map Type collection:

A collection of map types has two information per element: key, value.

The keys in the map cannot be the same. A key maps a value.

Key, the value defaults to type Object.

Put (a key, value) is a method of adding elements to the Map collection.

Get (key) return value

Remove (key) deletes a value.

ContainsKey (key) judgment key.


Collections Tool Class

Conllections is primarily used for collection of list classes.

Conllections.sort (list) arranges the elements in the List collection in ascending order.

Conllections.reverse (List) reverse

Conllections.max () finds the maximum element, and the return value is type object. can also be used in set.

Conllections.min () finds the smallest element, and the return value is type object. can also be used in set.

Arrays Tool Class

Aslist () converts an array to a collection of list types.

BinarySearch () Two-point lookup.

Sort () array.

Generic type

A data type that is not fixed.

Declaration: Class name added < generics 1, generic 2, generic 3,... >

[Modifier] {Class/interface} class/Interface < generics 1, generics 2, generic 3,... >

Generics are of type object by default. Generics can only be reference data types.



Job 1

Use scanner to read a string from the console, count the number of occurrences of each character in the string, and require that you use the knowledge you have learned to complete the above requirements

Implementation of the idea according to the set, List, map set of characteristics completed

Import Java.util.HashSet;

Import Java.util.Scanner;

Import Java.util.Set;


public class ZY1 {


public static void Main (string[] args) {

Scanner sc = new Scanner (system.in);

String s = sc.nextline ();

char[] ch = s.tochararray ();

HashSet a = new HashSet ();

for (int i=0;i<ch.length;i++) {

A.add (Ch[i]);

}

for (Object o:a) {

int j = 0;

Char b= (Character) o;

for (int i = 0; i < ch.length; i++) {

if (b== Ch[i]) {

j + +;

}

}

System.out.println (b + "," + j);

}

}

}



Job 2

There are a number of students in a secondary school (the student object is placed in a list), each student has a name attribute, class name attribute (String), and test score attributes (int),

At the end of a test, each student got a test result. Please print out the total score of each class for peaceful sharing.


Import java.util.ArrayList;


public class ZY2 {

String name;

String Banji;

Double Chengji;


Public ZY2 (String name,string banji,double Chengji) {

This.name=name;

This.banji=banji;

This.chengji=chengji;

}


public static void Main (string[] args) {

Double zong1=0;

Double zong2=0;

int renshu1=0;

int renshu2=0;

ZY2 zhangs=new ZY2 ("John", "Class One", 33);

ZY2 wangkai=new ZY2 ("Wang", "Second Class", 83);

ZY2 liushaojie=new ZY2 ("Liu Shaojie", "Second Class", 88);

ZY2 lisi=new ZY2 ("Dick", "Class One", 45);

ZY2 xudesheng=new ZY2 ("Hudsen", "Class One", 85);

Arraylist<zy2> a=new arraylist<zy2> ();

A.add (zhangs);

A.add (Wangkai);

A.add (Liushaojie);

A.add (Lisi);

A.add (Xudesheng);

for (int i=0;i<a.size (); i++) {

if (A.get (i). banji== "one class") {

Zong1=zong1+a.get (i). Chengji;

renshu1++;

}else if (A.get (i) banji== "Second Class") {

Zong2=zong2+a.get (i). Chengji;

renshu2++;

}

}

System.out.println ("A class of the total score is:" +zong1+ "average score is:" +ZONG1/RENSHU1);

System.out.println ("Second class of the total score is:" +zong2+ "average score is:" +ZONG2/RENSHU2);


}

}

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.