import java.util.*;
import java.io.*;class fruit{
public String name;
public float price;
public int quantity;
fruit(String name, float price,int quantity){
this.name=name;
this.price=price;
this.quantity=quantity;
}
}
public class goumai{
public static void main(String[] args){
fruit[] fr=new fruit[3];
fr[0]=new fruit("apple",2,100);
fr[1]=new fruit("pear",2,200);
fr[2]=new fruit("banana",3,100);
Hashtable hash=new Hashtable();
hash.put("A",fr[0]);
hash.put("B",fr[1]);
hash.put("C",fr[2]);
Scanner read=new Scanner(System.in);
String br=read.nextLine();
//String br=read.nextLine();
//Object obj=hash.get(br);
//System.out.println(obj.name);//
System.out.println(((fruit)hash.get(br)).name);
//System.out.println(br);
}
}
错误信息如下:
D:\java>javac goumai.java
注意: goumai.java 使用了未经检查或不安全的操作。
注意: 要了解详细信息,请使用 -Xlint:unchecked 重新编译。我的程序的目的是,用户输入(A B C)后,输出键值对应的对象属性,可是有这个不安全操作的问题,运行不了。
import java.io.*;class fruit{
public String name;
public float price;
public int quantity;
fruit(String name, float price,int quantity){
this.name=name;
this.price=price;
this.quantity=quantity;
}
}
public class goumai{
public static void main(String[] args){
fruit[] fr=new fruit[3];
fr[0]=new fruit("apple",2,100);
fr[1]=new fruit("pear",2,200);
fr[2]=new fruit("banana",3,100);
Hashtable hash=new Hashtable();
hash.put("A",fr[0]);
hash.put("B",fr[1]);
hash.put("C",fr[2]);
Scanner read=new Scanner(System.in);
String br=read.nextLine();
//String br=read.nextLine();
//Object obj=hash.get(br);
//System.out.println(obj.name);//
System.out.println(((fruit)hash.get(br)).name);
//System.out.println(br);
}
}
错误信息如下:
D:\java>javac goumai.java
注意: goumai.java 使用了未经检查或不安全的操作。
注意: 要了解详细信息,请使用 -Xlint:unchecked 重新编译。我的程序的目的是,用户输入(A B C)后,输出键值对应的对象属性,可是有这个不安全操作的问题,运行不了。
import java.io.*;class fruit{
public String name;
public float price;
public int quantity;
fruit(String name, float price,int quantity){
this.name=name;
this.price=price;
this.quantity=quantity;
}
}
public class goumai{
public static void main(String[] args){
fruit[] fr=new fruit[3];
fr[0]=new fruit("apple",2,100);
fr[1]=new fruit("pear",2,200);
fr[2]=new fruit("banana",3,100);
Hashtable hash=new Hashtable();
hash.put("A",fr[0]);
hash.put("B",fr[1]);
hash.put("C",fr[2]);
// Scanner read=new Scanner(System.in);
// String br=read.nextLine();
//String br=read.nextLine();
//Object obj=hash.get(br);
//System.out.println(obj.name);//
System.out.println(((fruit)hash.get("A")).name);
//System.out.println(br);
}
}
测试一下,没问题。
output:
apple
D:\java>javac -Xlint:unchecked goumai.java
javac -Xlint:unchecked goumai.java
就可以编译了,很可能是用了不赞成(Deprecated)的方法
D:\java>javac -Xlint:unchecked goumai.java
goumai.java:23: 警告: [unchecked] 对作为普通类型 java.util.Hashtable 的成员的 p
ut(K,V) 的调用未经检查
hash.put("A",fr[0]);
^
goumai.java:24: 警告: [unchecked] 对作为普通类型 java.util.Hashtable 的成员的 p
ut(K,V) 的调用未经检查
hash.put("B",fr[1]);
^
goumai.java:25: 警告: [unchecked] 对作为普通类型 java.util.Hashtable 的成员的 p
ut(K,V) 的调用未经检查
hash.put("C",fr[2]);
可是在javac goumai.java 进行编译 ,还是不行
还是同样的错误