Can Java use String as an index array key? (ex: array[“a”]=1;)

后端 未结 4 1746
夕颜
夕颜 2020-12-13 12:47

Can Java use a String as an index array key? Example:

array[\"a\"] = 1;
4条回答
  •  青春惊慌失措
    2020-12-13 13:04

    No they can't. But they can use chars the ASCII value of the alphabet will be used as the key index

    Consider

        String[] a = new String['a' + 1];
        a['a'] = "Hello";
        int[] b = new int['a' + 3];
        b['c'] = 5;
    
        System.out.println(a[97]);
        System.out.print(b[99]);
    

    This will output

    Hello
    5
    

提交回复
热议问题