简体   繁体   中英

how can I implement the constructor of a hashtable in java

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

  }

}

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>(); 
}

initialize your public List< String>[] buckets; properly before filling it:

public List< String>[] buckets = new ArrayList< String>[120];

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM