Iniziamo a sistemare un po' di cose:
Codice PHP:
public class CodificatoreTester
{
public static void main(String[] args)
{ }
}
class Codificatore implements InvertibleDictionary
{
private StringPair [] v;
private int vSize;
public Codificatore()
{
v = new StringPair[10];
vSize = 0;
}
public String toString()
{
for(int i = 0; i < vSize; i++)
System.out.println(v[i]);
// MANCA IL RETURN
}
public boolean isEmpty()
{
return vSize == 0;
}
public void makeEmpty()
{
vSize = 0;
}
public void insert(Comparable key, Comparable value)
{
if(key == null)
throw new DictionaryItemNotFoundException();
try
{
remove(key);
}
catch (DictionaryItemNotFoundException e)
{
// ASSASSINO!!! NON SI AMMAZZANO COSi' LE ECCEZIONI!!!
// e.printStackTrace();
}
for(int i = 0; i < vSize; i++)
{
if(v[i].word.equals(key))
{
v[i] = new StringPair((String) key, (String ) value);
return;
}
}
v[vSize++] = new StringPair((String) key, (String) value);
}
public void remove(Comparable key)
{
for(int i = 0; i < vSize; i++)
{
if(v[i].word.equals(key))
{
v[i] = v[vSize - 1];
vSize--;
}
}
throw new DictionaryItemNotFoundException();
}
public Object find(Comparable key)
{
for(int i = 0; i < vSize; i++)
{
if(v[i].word.equals(key))
return v[i].code;
}
throw new DictionaryItemNotFoundException();
}
public InvertibleDictionary invert()
{
InvertibleDictionary i = new Codificatore();
for( int j = 0; j < vSize; j++)
{
Comparable c = v[j].code;
Comparable c1 = v[j].word;
i.insert((Comparable) v[j].code, (Comparable) v[j].word );
}
return i;
}
private class StringPair
{
public StringPair(String word, String code)
{
this.word = word;
this.code = code;
}
public String getWord()
{
return word;
}
public String getCode()
{
return code;
}
public String toString()
{
return word + " " + code;
}
private String word;
private String code;
}
}
interface InvertibleDictionary
{
boolean isEmpty();
void makeEmpty();
void insert(Comparable key, Comparable value);
void remove(Comparable key);
Object find(Comparable key);
InvertibleDictionary invert();
}
class DictionaryItemNotFoundException extends RuntimeException
{}
A questo punto forse qualcuno guarderà il codice con più interese.
1. Leggi i commenti
2. Leggi il regolamento: devi usare i tag [ code][/ code] oppure [ php][/php]
3. Ti avevo espressamente chiesto lo stackTrace. DOV'E?
4. Una String è un Comparable. L'errore che hai postato non è significativo. A parte il fatto che riporta Comaparable...