Try to create a hash table in java with string as the key and double as the value

In the following procedure:

import java.util.*;

public class HashTableStringdouble
{
//  private Hashtable<String,double[]> model  = new Hashtable<String,double[]>();;
    private Hashtable<String,double> model  = new Hashtable<String,double>();;                // this does not work

    public static void main(String args[])
    {
    }
}

There are double [] but not double It gives the following error:

HashTableStringdouble. Java: 7: error: unexpected type private hashtable model = new hashtable()// This doesn't work ^ requirement: reference discovery: double hashtablestringdouble Java: 7: error: unexpected type private hashtable model = new hashtable()// This does not work ^ requirement: reference found: Double 2 errors

I'm not sure what I did wrong here Please explain how hashtable works

Solution

You cannot use primitives as keys or values in a hashtable. You need to use objects For example, it can use double instead of double It is used with double [] because arrays are objects in Java

In addition, hashtable is a little outdated, and HashMap is preferred in most cases:

private Map<String,Double> model  = new HashMap<String,Double>();
//or if you use Java 7+
private Map<String,Double> model  = new HashMap<>();
The content of this article comes from the network collection of netizens. It is used as a learning reference. The copyright belongs to the original author.
THE END
分享
二维码
< <上一篇
下一篇>>