Enter a number, this number represents the number of lines, each line has a color, and finally output the color with the most input.

The translation of this title is: enter a number, this number represents the number of lines, each line has a color, and finally output the color with the most input.

My idea is: input the number (Scanner), input the color (Scanner), use the Map collection to store the data, judge the number, traverse and select the color with the most number of times, and pass it by value

 

The length of Java arrays is fixed, and only the same type of data can be stored in the same array. In order to facilitate the storage and manipulation of a set of data with a variable number, the JDK class library provides Java collections. All Java collection classes are located in the java.util package. Java collections are mainly divided into the following three types: set, list, map . The set is not ordered in a specific way and has no duplicate objects. list: Sorted by index position, there can be duplicate objects. map: Each element contains a pair of key objects and value objects. The collection does not have repeated key objects, but the value objects can be repeated. Then we use the map type to store data here, because it can be judged in a key-to-value manner.

List (list): ArrayList, create object: List list<data type> = new ArrayList<data type>();

Map: Map<String,String> map = new HashMap<String,String>();

HashMap Iterator: Traverse the elements in the Hash

In Hash, you can directly use the following method to traverse (all keys) KeySet, and then you can find the required value through the key
1
2
3
4
5
6
HashMap<String,String> mp =  new  HashMap<String,String>();
for  (String i : mp.keySet()) 
     //String 是mp中的键的对应类型 i 是对应的KeySet中的每一个键值  
     System.out.println( mp.get(i)); 
}

 

package sequence;

import java.util.HashMap;
import java.util.Map;
import java.util.ArrayList;
import java.util.List;

import java.util.Scanner;

public class Main1004 {

    public static void main(String[] args) {

        Scanner in = new Scanner(System.in);
        int T = in.nextInt();
        List<String> list = new ArrayList<>();
        while (true) {
            Map<String, Integer> aMap = new HashMap<>();

            if (T != 0) {
                int m = T;
                while (T > 0) {
                    String bString = in.next();
                    if (aMap.get(bString) == null) {
                        aMap.put(bString, 1);
                    } else {
                        int count = aMap.get(bString);
                        aMap.remove(bString);
                        count++;
                        aMap.put(bString, count);
                    }
                    T--;
                }
                int i = 0;
                String result = null;
                for (String key : aMap.keySet()) {
                    int a = aMap.get(key);
                    if (a > i) {
                        i = a;
                        result = key;
                    }
                }
                list.add(result);
            }             
            else {
                for (int i = 0; i < list.size(); i++) {
                    System.out.println(list.get(i));
                }

                return;
            }


        }

    }
}

 

Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=326339163&siteId=291194637