关于Kotlin中的hashMapOf的关键是哪一个?

时间:2018-07-23 00:36:48

标签: android kotlin

我知道我可以使用HashMap创建地图对象。

以下代码来自https://kotlinlang.org/api/latest/jvm/stdlib/kotlin.collections/hash-map-of.html

哪个是关键?如何通过钥匙访问地图? mvnunupper_boundmap [1]吗?

map["1"]

2 个答案:

答案 0 :(得分:2)

这三个都是可能的。

在这种情况下,您提到的密钥是

program test
  use iso_c_binding
  implicit none
! A C function that returns a string need a pointer to the array of single char 
  type (c_ptr) :: C_String_ptr
! This is the Fortran equivalent to a string of single char
  character (len=1, kind=c_char), dimension(:), pointer :: filchar=>null()
! Interface to a C routine which opens a window to browse for a file to open
  interface
    function tinyopen(typ) bind(c, name="tinyopen")
       use iso_c_binding
       implicit none
       integer(c_int), value :: typ
       type (C_Ptr) :: tinyopen
    end function tinyopen
  end interface
  character (len=256) :: filename
  integer typ,jj
  typ=1
C_String_ptr = tinyopen(typ)
! convert C pointer to Fortran pointer
  call c_f_pointer(C_String_ptr,filchar,[256])
  filename=' '
  if(.not.associated(filchar)) then
! if no characters give error message
    write(*,*)'No file name'
  else
! convert the array of single characters to a Fortran character
    jj=1
    do while(filchar(jj).ne.c_null_char)
      filename(jj:jj)=filchar(jj)
      jj=jj+1
    enddo
  endif
  write(*,*)'Text is: ',trim(filename)
end program test

但是,如果您按以下方式创建地图:

map[1]

关键是

hashMapOf("1" to "x", "2" to "y", "-1" to "zz")

要添加进一步的说明,在 HashMap 或任何 Map 中,键位于值类型之前。正如您所注意到的, HashMap 标识Key(Int)类型以及Value(String)类型。

答案 1 :(得分:2)

您可以使用TextField进行访问。您可以参考this blog了解更多变体。

在表达式map[1]中,hashMapOf(1 to "x", 2 to "y", -1 to "zz")指的是键,其中1是值。

使用以下命令打印值时:

x

输出将是:

println(hasmap[1])