0

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

  }

}
CupawnTae
  • 14,192
  • 3
  • 29
  • 60
python3
  • 251
  • 4
  • 12
  • I want to set up a list of list so that I can store strings in every bucket,but I have some problems to implement the construct – python3 May 02 '15 at 16:57

2 Answers2

2

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>(); 
}
Tagir Valeev
  • 97,161
  • 19
  • 222
  • 334
  • would also be worth mentioning the initialization of the `new List[120]` rather than just including it. The second constructor also needs one. – CupawnTae May 02 '15 at 17:12
0

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

public List< String>[] buckets = new ArrayList< String>[120];
Manuel Jain
  • 751
  • 1
  • 6
  • 16