剛剛參考別人網誌對於Hashtable的使用...遇到了幾個提示
原址:http://blog.yam.com/bluelanguage/article/8401664
以下是人家的 code 經過我重新修正後:
package hoge;
import java.util.*;
import java.io.*;
class Student {
private String number;
private String name;
private double score;
public Student(String number, String name, double score) {
this.number = number;
this.name = name;
this.score = score;
}
public String getNumber() {
return number;
}
public String getName() {
return name;
}
public double getScore() {
return score;
}
}
public class Convert2 {
public static void main(String[] args) {
try {
OpHashtable();
}
catch(IOException e) {
System.out.println(e.toString());
}
}
private static void OpHashtable() throws IOException {
Hashtable table = new Hashtable();
BufferedReader buf = new BufferedReader(
new InputStreamReader(System.in));
int select;
String number, name;
double score;
Object val;
Student student;
System.out.println("使用Hashtable類別");
while(true) {
System.out.println("(1) 加入學生資料");
System.out.println("(2) 查詢學生資料");
System.out.println("(3) 移除學生資料");
System.out.println("(4) 列出所有資料");
System.out.println("(5) 離開");
System.out.print("> ");
select = Integer.parseInt(buf.readLine());
switch(select) {
case 1:
System.out.print("學號: ");
number = buf.readLine();
System.out.print("姓名: ");
name = buf.readLine();
System.out.print("成績: ");
score = Double.parseDouble(buf.readLine());
student = new Student(number, name, score);
val = table.put(number, student);
if(val == null){
System.out.println("資料存入....");}
else{
System.out.println("資料取代....");
break;}
case 2:
System.out.print("學號: ");
number = buf.readLine();
student = (Student) table.get(number);
if(student != null)
{System.out.println("學號: " + student.getNumber() +
" 姓名: " + student.getName() +
" 成績: " + student.getScore());}
else
{System.out.println("查無此筆資料....");
break;}
case 3:
System.out.print("學號: ");
number = buf.readLine();
val = table.remove(number);
if(val != null)
{System.out.println("學生 " + number + " 資料移除");}
else
{System.out.println("查無此筆資料....");
break;}
case 4:
{Enumeration e = table.elements();
while(e.hasMoreElements()) {
student = (Student) e.nextElement();
System.out.println("學號: " + student.getNumber() +
" 姓名: " + student.getName() +
" 成績: " + student.getScore());
}
break;}
case 5:
{efault:System.exit(0);}
}
}
}
}
在Hashtable的地方看到了一個提示
「 **** is a raw type. References to generic type ***** should be parameterized. 」
拿去Google後有人建議先去看泛型的 document......, 索性整理了一下。
http://www.javaworld.com.tw/jute/post/view?bid=5&id=105545&sty=3&tpg=1&age=0
http://java.sun.com/j2se/1.5/pdf/generics-tutorial.pdf
沒有留言:
張貼留言