Help us understand the problem. What is going on with this article?

kotlinでHashMapの実装

More than 1 year has passed since last update.

HashMap

HashMapをkotlinで実装した。
idがkeyで名前がvalue

maptest.kt
import java.util.Scanner
import java.util.InputMismatchException

var map = kotlin.collections.HashMap<Int , String>()

fun main(arg : Array<String>) {
    val input = Scanner(System.`in`)
    var i : Int
    try {
    loop@ while(true) {
            println("何をしますか?\n" +
                    "入力 =>1" + "表示=>2" + "探す=>3" + "削除=>4"+ "終了=>5")
            i = input.nextInt()
            when (i) {
                1 -> {
                    Scan()
                }
                2 -> {
                    println(map)
                }
                3 -> {
                    search()
                }
                4 -> {
                    delete()
                }
                5->{
                    break@loop
                }
            }
    }
    }catch(e : InputMismatchException){
        println("型が違います : " +e)
    }
}

map.kt
import java.util.*
val input = Scanner(System.`in`)

fun search(){
    val key : Int
    println("idを入力してください")
    try {
        key = input.nextInt()
        if (map.containsKey(key)) {
            println("$key =" + map.get(key))
        } else {
            println("そのidは存在しません")
        }
    }catch (e : InputMismatchException){
        println("型が違います : ")
    }
}
fun Scan(){
    println("名前を入力してください")
    val name =  input.next()
    println("idを入力してください")
    val key : Int = input.nextInt()
    try {
        if (map.containsKey(key)) {
            println("上書きしますか?\n" +
                    "はい=>1いいえ=>2")
            when (input.nextInt()) {
                1 -> {
                    map.put(key, name)
                }
                2 -> {
                }
            }
        } else {
            map.put(key, name)
        }
    }
    catch (e : InputMismatchException){
        println("型が違います : ")
    }
}
fun delete() {
    println("全削除=>1 1データ削除=>2 キャンセル=>3")
    try {
        when (input.nextInt()) {
            1 -> {
                map.clear()
            }
            2 -> {
                println("削除するidを入力してください")
                val keyid: Int = input.nextInt()
                map.remove(keyid)
            }
            3 -> {
            }
        }
    }catch (e : InputMismatchException){
        println("型が違います : ")
    }
}


名前を日本語で入力やリトライなどの実装はまたこんど。

Why not register and get more from Qiita?
  1. We will deliver articles that match you
    By following users and tags, you can catch up information on technical fields that you are interested in as a whole
  2. you can read useful information later efficiently
    By "stocking" the articles you like, you can search right away
Comments
Sign up for free and join this conversation.
If you already have a Qiita account
Why do not you register as a user and use Qiita more conveniently?
You need to log in to use this function. Qiita can be used more conveniently after logging in.
You seem to be reading articles frequently this month. Qiita can be used more conveniently after logging in.
  1. We will deliver articles that match you
    By following users and tags, you can catch up information on technical fields that you are interested in as a whole
  2. you can read useful information later efficiently
    By "stocking" the articles you like, you can search right away