Implementuje tablice haszującą (sama tablica jest tablicą Arraylistow), ale podczas kompilacji pojawia się warning.
hash.java:46: warning: [unchecked] unchecked call to add(E) as a member of the r
aw type ArrayList
hashTable[sum].add(word);
^
where E is a type-variable:
E extends Object declared in class ArrayList
1 warning
znalazlem strone apropos problemu np. http://stackoverflow.com/questions/197986/what-causes-javac-to-issue-the-uses-unchecked-or-unsafe-operations-warning ale nie pomaga mi. Nie jestem w stanie narzutować na tablicę (albo może za mało umiem). Pomocy.
KOD:
import java.util.ArrayList;
import java.util.List;
/**
*
* @author Przemysław Papciak
*/
public class hash
{
ArrayList[] hashTable; //Tablica haszujaca, w niej beda listy typu ArrayList
int colision = 0; //licznik kolizji
int hashSize = 0; //pobierany jako parametr programu, ustawiane podczas tworzenia obiektu
/**
*
* @param hash_size
*/
public hash(int hash_size) //konstruktor wypelnia tablice haszujaca pustymi listami (metoda lancuchowa)
{
hashSize = hash_size;
hashTable = new ArrayList[hash_size];
for (int i=0;i<hash_size;i++)
{
hashTable[i]=new ArrayList<String>();
}
}
/**
*
* @param word
*/
public void insert (String word)
{
int sum;
sum = hashingFunc(word);
//ten if wyklucza dublowanie się słów, bez niego
//algorytm bedzie dopuszczac kopie tego samego slowa w tablicy
if(!find(word))
{
if (!hashTable[sum].isEmpty())
{
colision = colision + hashTable[sum].size();
}
hashTable[sum].add(word);
}
else
{
colision = colision + 1;
//Slowo juz istnieje, nie jest dodawane
//jednak licznik kolizji zwieksza sie o jeden
}
}
/**
*
* @param word
* @return
*/
private boolean find(String word)
{
int sum;
sum = hashingFunc(word);
if(hashTable[sum].contains(word))
{
return true;
}
else
{
return false;
}
}
/**
*
* @param word
*/
public void remove (String word)
{
int sum;
sum = hashingFunc(word);
hashTable[sum].remove(word);
}
//haszowanie polega na zrzuceniu stringa na tablice char
//zsumowaniu rzutowania charow na inta i podzieleniu sumy modulo <hash_size>
//zwraca indeks tablicy w ktorym jest/bedzie slowo
/**
*
* @param word
* @return
*/
private int hashingFunc (String word)
{
int lenght = word.length();
char[] str = new char[lenght+1];
word.getChars(0,lenght,str,0);
int sum = 0;
for (int i=0;i<lenght;i++)
{
sum = sum + (int)str[i];
}
sum = sum%hashSize;
return sum;
}
}