返回与年份和等级相关的字符串 [英] returning the string associated with the year and rank

查看:117
本文介绍了返回与年份和等级相关的字符串的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

因此,基本上我有一张看起来像这样的地图

  HashMap< Integer,HashMap< String,Integer>> girls = 
new HashMap< Integer,HashMap< ** String **,Integer>>();'

,我想返回粗体字符串,这是我迄今的方法

  public String getGirlName(int year,int rank){// year指关键字
//排名指向值映射中的其他整数
if(girls.containsKey(year)&& girl; get(year).containsKey (等级)){
//不应该返回名称
返回girls.get(year).get(rank); //错误在这里它说我不能返回一个整数我必须返回一个字符串
else else null;
}
}

我不知道如何执行上述标题让我们试着看看 HashMap< Integer,HashMap< String,Integer>>的结构 可视化,因为它是一个相当复杂的数据结构

  // AB 
// | -------- | -------- |
// CD
// | ------ | ----------------------------- ---------------------------------------- |
{
整数:{String:Integer,String:Integer,String:Integer,String:Integer},
整数:{String:Integer,String:Integer,String:Integer,String:整数},
整数:{字符串:整数,字符串:整数,字符串:整数,字符串:整数},
整数:{字符串:整数,字符串:整数,字符串:整数,字符串:整数}

$ / code>

部分A和C是键,B和D是值。 / p>

现在让我们看看 .get(year).get(rank); 是什么。首先调用 get(year) year 是C节中的一个值,因此 get(year)会在D节中返回一个值。 c $ c> get(rank)被调用。由于它是在 get(year) rank 的返回值上调用的,因此这里是A节中的一个值。 get(rank)将在D部分返回一个值,它们都是 Integer s。因此,会发生编译器错误。



要解决这个问题,只需在内部映射中交换这两种类型。你从 HashMap< Integer,HashMap< String,Integer>>< / code>改变为 HashMap< Integer,HashMap< Integer,String>>


So basically I have a map that looks like this

HashMap<Integer, HashMap<String, Integer>> girls = 
new HashMap<Integer, HashMap<**String**, Integer>>();'

and I want to return the bolded string and this is my method so far

public String getGirlName(int year, int rank) { //year refers to Key
                                                //rank refers to other integer in value map 
  if (girls.containsKey(year) && girls.get(year).containsKey(rank)){ 
     //shouldn't this return the name 
     return girls.get(year).get(rank); //error here where it says I can't return an integer I have to return a string
     else return null; 
  }
}

I'm not sure how to do the above title

解决方案

Let's try to see the structure of HashMap<Integer, HashMap<String, Integer>> visually, since it is quite a complex data structure

//             A         B
//         |--------|--------|
//      C                              D
//  |------|---------------------------------------------------------------------|
{
    Integer: {String: Integer, String: Integer, String: Integer, String: Integer},
    Integer: {String: Integer, String: Integer, String: Integer, String: Integer},
    Integer: {String: Integer, String: Integer, String: Integer, String: Integer},
    Integer: {String: Integer, String: Integer, String: Integer, String: Integer}
}

Sections A and C are the keys, B and D are the values.

Now let's see what does .get(year).get(rank);. First, get(year) is called. year is a value in section C, so get(year) returns a value in section D. Next, get(rank) is called. Since it is called on the return value of get(year), rank here is a value in section A. get(rank) will return a value in section D, which are all Integers. Therefore, the compiler error occurs.

To fix this, simply swap the two types in the inner map. You change from HashMap<Integer, HashMap<String, Integer>> to HashMap<Integer, HashMap<Integer, String>>.

这篇关于返回与年份和等级相关的字符串的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆