Hashmap - how can I implement the constructor of a hashtable in java

De openkb
Aller à : Navigation, rechercher

Sommaire

Questions

Suppose I need to implement a hashtable on my own,but I have some problems about implementing my constructor. For example,if I need to initialize List[] buckets,but when I write like the following codes,the computer just gave a wrong signal of"buckets[i]=new List()" , can someone tell me how to finish the constructor in this case? import java.util.List;

import java.util.LinkedList;

public class GeneralHashMap extends MyHashMap {

  public List< String>[] buckets;

  public GeneralHashMap() {
    for(int i=0;i<120;i++)
    {
        buckets[i]=new List<String>(); 
    }

    // TODO: IMPLEMENT CONSTRUCTOR

  }


  public GeneralHashMap(int newsize)
  {
    for(int i=0;i<newsize;i++)
    {
        buckets[i]=new List<String>();
    }
  }

  @Override
  protected int hash(String token) {

    // TODO: IMPLEMENT HASHING FUNCTION FOR GENERAL HASHMAP
    return -1;

  }

  @Override
  public void add(String token) {

    // TODO: IMPLEMENT ADD METHOD USING BUCKETS

  }

  @Override
  public void display() {

    // TODO: IMPLEMENT DISPLAY METHOD TO SHOW CONTENTS OF ALL BUCKETS

  }

}

Answers

In Java List is just an interface which cannot be instantiated. What you need is a class implementing such interface, for example, ArrayList. Try

buckets = new List[120];
for(int i=0;i<120;i++)
{
    buckets[i]=new ArrayList<String>(); 
}

Source

License : cc by-sa 3.0

http://stackoverflow.com/questions/30004981/how-can-i-implement-the-constructor-of-a-hashtable-in-java

Related

Outils personnels
Espaces de noms

Variantes
Actions
Navigation
Outils