Help With TreeMap Warning (Noob?)

L

Luc The Perverse

Sorry if I am asking a dumb question - but I am having a problem, an usual
warning is coming up when I compile my simple program.

When I compile

....\Desktop>javac edit_dist.java
Note: edit_dist.java uses unchecked or unsafe operations.
Note: Recompile with -Xlint:unchecked for details.

Recompiling

E:\Documents and Settings\Luc\Desktop>javac edit_dist.java -Xlint:unchecked
edit_dist.java:41: warning: [unchecked] unchecked call to put(K,V) as a
member of the raw type java.util.TreeMap
mtm.put(myCurKey, new Integer(min));
^

Here is my code

import java.util.*;
import java.lang.*;
import java.math.*;

public class edit_dist{
TreeMap mtm = null;
edit_dist(){
mtm = new TreeMap<String, Integer>();
}
public static int naive_editDistance(String Source, String Dest){
if(Source.equals("") || Dest.equals(""))
return Dest.length() + Source.length();
if(Source.charAt(0) == Dest.charAt(0))
return naive_editDistance(Source.substring(1), Dest.substring(1));
int min = 536870911; //arbitrarily large number above max length of
string
for(int i=0;i<2;i++)
for(int j=(i!=0?0:1);j<2;j++)
min = Math.min (min,
1 + naive_editDistance(Source.substring(i),

Dest.substring(j)));
return min; // :)
}
private static String makeLen(String in, int OSL){
int a = OSL - in.length();
for(int i=0;i<a;i++)
in+=(char) 1;
return in;
}
int memoize_editDistance(String Source, String Dest, int OSL){
if(Source.equals("") || Dest.equals(""))
return Dest.length() + Source.length();
String myCurKey = makeLen(Source, OSL) + Dest;
if(mtm.containsValue(myCurKey))
return ((Integer)mtm.get(myCurKey)).intValue();
if(Source.charAt(0) == Dest.charAt(0))
return naive_editDistance(Source.substring(1), Dest.substring(1));
int min = 536870911; //arbitrarily large number above max length of
string
for(int i=0;i<2;i++)
for(int j=(i!=0?0:1);j<2;j++)
min = Math.min (min,
1 + naive_editDistance(Source.substring(i),

Dest.substring(j)));
mtm.put(myCurKey, new Integer(min));
return min; // :)
}

public static int memoize_editDistance(String Source, String Dest){
return (new edit_dist()).memoize_editDistance(Source, Dest,
Source.length());


}
public static void main(String[] xs){
String[] x = {"accept", "allow", "ask", "believe", "borrow", "break",
"bring",

"buy", "can/beable", "cancel", "change", "clean", "comb", "complain",
"cough", "count", "cut"};
int[] res = {0, 5, 5, 6, 6, 5, 6, 6, 8, 4, 6, 4, 5, 7, 5, 4, 4, 5, 0, 4,
6, 4, 5,

5, 5, 9, 5, 6, 4, 5, 7, 5, 5, 5, 5, 4, 0, 7, 6, 4, 5, 3, 9, 5, 5, 5, 4, 7,
5, 5, 3, 6, 6, 7, 0,

6, 5, 5, 6, 8, 6, 6, 5, 7, 8, 7, 7, 7, 6, 4, 6, 6, 0, 4, 4, 5, 9, 6, 6, 6,
5, 7, 5, 5, 6, 5, 5,

4, 5, 4, 0, 3, 4, 8, 6, 6, 3, 5, 7, 5, 5, 5, 6, 5, 5, 5, 4, 3, 0, 4, 9, 6,
4, 5, 5, 7, 5, 4, 5,

6, 5, 3, 6, 5, 4, 4, 0, 9, 6, 6, 5, 4, 8, 4, 4, 2, 8, 9, 9, 8, 9, 8, 9, 9,
0, 5, 7, 7, 8, 8, 9,

9, 9, 4, 5, 5, 6, 6, 6, 6, 6, 5, 0, 3, 5, 5, 7, 5, 5, 5, 6, 6, 5, 6, 6, 6,
4, 6, 7, 3, 0, 4, 5,

7, 4, 4, 5, 4, 4, 5, 5, 6, 3, 5, 5, 7, 5, 4, 0, 4, 5, 4, 4, 4, 5, 5, 4, 7,
5, 5, 5, 4, 8, 5, 5,

4, 0, 5, 3, 3, 3, 7, 7, 7, 8, 7, 7, 7, 8, 8, 7, 7, 5, 5, 0, 6, 6, 7, 5, 5,
5, 7, 5, 5, 5, 4, 9,

5, 4, 4, 3, 6, 0, 2, 3, 4, 5, 5, 7, 5, 5, 4, 4, 9, 5, 4, 4, 3, 6, 2, 0, 2,
4, 5, 3, 7, 6, 5, 5,

2, 9, 5, 5, 4, 3, 7, 3, 2, 0}; //results from naive_editDistance assumed
correct
int nc = 0;
int lv = 0;
for(String C : x)
for(String Y : x)
if((lv=memoize_editDistance(C, Y))!=res[nc++])
System.out.println("Failed on " + C +" to " + Y + " got "

+lv + " expecting " + res[nc-1]);


}


}
 
L

Lew

Luc said:
...\Desktop>javac edit_dist.java
Note: edit_dist.java uses unchecked or unsafe operations.
Note: Recompile with -Xlint:unchecked for details.
This warning alerts you that your TreeMap declaration was not generic.
E:\Documents and Settings\Luc\Desktop>javac edit_dist.java -Xlint:unchecked
edit_dist.java:41: warning: [unchecked] unchecked call to put(K,V) as a
member of the raw type java.util.TreeMap
mtm.put(myCurKey, new Integer(min));
^

Here is my code

import java.util.*;
import java.lang.*;
You never need to import java.lang. It is the language itself, so to speak.
import java.math.*;

public class edit_dist{
TreeMap mtm = null;
The initialization to null is both redundant and superfluous.

You did not use a generic type in the Map declaration.
edit_dist(){
You should consider making the constructor public, or omitting it altogether.
mtm = new TreeMap<String, Integer>();
}

By convention, you should name classes with an initial upper-case letter, each
word part capitalized, and eschew underscores: EditDist.

In most scenarios you should prefer to declare variables with the interface
type rather than the concrete class type:

Map said:
public static int naive_editDistance(String Source, String Dest){

By convention, you should name variables and methods with an initial
lower-case letter, each word part capitalized, and eschew underscores:

public static int naiveEditDistance( String source, String dest )
..

They call this "camelCase".

- Lew
 
O

Oliver Wong

Lew said:
You should consider making the constructor public, or omitting it
altogether.

There are reasons for having a non-public constructor. It's frequently
used in the singleton pattern, for example.

- Oliver
 
L

Luc The Perverse

Oliver Wong said:
There are reasons for having a non-public constructor. It's frequently
used in the singleton pattern, for example.

I did not actually deliberately make it private - but as you can see, every
invocation of the memoized edit distance function required its own copy of
the class for it's own mapping.

In general this is probably poor programming practice though.

I do not know if there is an equivalent in Java - but in C++ I found
protected constructors very useful for classes which were solely designated
as helper classes. Likely Java takes care of this through the use of non
public classes that can't be seen outside the file.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,994
Messages
2,570,223
Members
46,810
Latest member
Kassie0918

Latest Threads

Top