[Blue Bridge Cup] the "sixth move" of the 14 move sprint "Java API"

Posted by Hisakata on Wed, 09 Mar 2022 11:58:46 +0100

preface

- 🏀 Hello, I'm BXuan, a sophomore of software engineering who loves programming and basketball
- 📚 Recently, in preparation for the blue bridge provincial competition in April, this chapter will talk with you about common java API s!
- 🏃 It's not hard to give up, but it must be cool to insist.

Knowledge points

  • Explanation of vessel
  • Use of linear List
  • Use of queues
  • Use of set s
  • Use of map s

🙋‍♂️ container

Vector container (class)

There are Vector and list in the linear table, which have similar functions.

The main function of Vector is variable length array. Just use it as an array.

Vector can be used as an array, which is very simple and convenient to use.

//The first construction method creates a default vector with a default size of 10:
Vector()
//The second construction method creates a vector of a specified size.
Vector(int size)
//The third construction method creates a vector of the specified size, and the increment is specified by incr.
//Increment represents the number of elements that the vector increases each time.
Vector(int size,int incr)
//The fourth construction method creates a vector containing set c elements:
Vector(Collection c)

See the API of java Vector for details.


Real problem analysis

1. Title Description

The courier needs to sort the express. Now Xiao Li is a courier. He wants you to help him design a program for sorting the express, which is separated by city.

There are now the following inputs:

No. Province

Please separate the order number by city and output it.

Cities are sorted according to the input order

The order number is sorted according to the input order

Example:

input

10 

10124214 Beijing
12421565  Shanghai
sdafasdg213 Tianjin
fasdfga124 Beijing
145252  Shanghai
235wtdfsg Jinan
3242356fgdfsg Chengdu
23423 Wuhan  
23423565f Shenyang
1245dfwfs Chengdu

output

Beijing 2
10124214
fasdfga124
 Shanghai 2
12421565
145252
 Tianjin 1
sdafasdg213
 Jinan 1
235wtdfsg
 Chengdu 2
3242356fgdfsg 
1245dfwfs 
Wuhan 1
23423
 Shenyang 1
23423565f 

understand:

First of all, there must be less than 1000 Chinese cities, but the order number is uncertain. We can't open 10000 in each array, so there's not enough memory, so we use vector at this time. Its capacity is dynamically applied, which can be understood as unlimited.

2. Code example

package E_lanqiao;
import java.util.List;
import java.util.Scanner;
import java.util.Vector;

public class Main {

  static Vector city=new Vector<String>();

  static Vector <Vector<String>> dig= new Vector <Vector<String>>();

  static int Myfind(String s)
  {
      for(int i=0;i<city.size();i++)
      {
          if(city.get(i) ==s) {
              return i;
          }
      }
      return -1;
  }

  public static void main(String[] args) {


      int n;
      Scanner in=new Scanner(System.in);
      n=in.nextInt();
      for(int i=0;i<n;i++)
      {
          String d,c;
          d=in.next();
          c=in.next();
          int flag=Myfind(c);
          if(flag==-1){
              city.addElement(c);
              dig.addElement(new Vector<String>());
              dig.get(city.size()-1).addElement(d);
          }else   
              dig.get(flag).addElement(d);
      }
      for(int i=0;i<city.size();i++)
      {
          System.out.println(city.get(i)+" "+dig.get(i).size());

          for(int j = 0; j< dig.get(i).size(); j++)
              System.out.println(dig.get(i).get(j));
      }
  }
}

🙋‍♀️ Linear table List



🙋‍♂️ Queue queue

True topic review (CLZ's Bank)

This is explained in detail in the second part of this column.

🙋‍♀️ Set set

🙋‍♂️ Map mapping

I came into contact with hash mapping when learning hash lists. Here we want to talk about a similar data structure.

A map is an association container that provides a one-to-one hash.

  • The first one can be called a key. Each keyword can only appear once in the map
  • The second may be called the value of the keyword

Map is implemented as a template (generic) and can store any type of data, including user-defined data types. Map is mainly used for one-to-one mapping of data.

For example, for managing students in the class, the Key Value is the student number, and the Value is the structure or class of other information.

Real topic review (Frisch's language)

It is explained in detail in the fourth part of this column

👏 Summary

There are many APIs that java can call. In the process of learning, we should not only carefully study the principle of each data structure and algorithm implementation, but also know how to call the implemented API interface methods on the basis of understanding the principle to solve the problem more conveniently!

Topics: Java Algorithm