Discuss / Java / 作业提交

作业提交

Topic source

风梦幻飞

#1 Created at ... [Delete] [Delete and Lock User]

package com.itranswarp.learnjava;

import java.util.HashMap;

import java.util.List;

import java.util.Map;

public class Students {

List<Student> list;

Map<String ,Integer> cache;//cache是方便下一次查询

public Students(List<Student> list) {

this.list = list;

cache = new HashMap<>();

}

    /**

     * 根据name查找score,找到返回score,未找到返回-1

     */

    int getScore(String name) {

        // 先在Map中查找:

        Integer score = this.cache.get(name);

        if (score == null) {

            // TODO:

        score=findInList(name);

        if(findInList(name) instanceof Integer) {

        cache.put(name, score.intValue());

        }

        }

        return score == null ? -1 : score.intValue();

    }

    Integer findInList(String name) {

        for (var ss : this.list) {

            if (ss.name.equals(name)) {

                return ss.score;

            }

        }

        return null;

    }

}


  • 1

Reply