vector---未经检查或是不安全的操作!
程序本身已经能运行了,可是搞不懂怎样才能在去掉程序中红色部分@SuppressWarnings({ "serial", "unchecked" })也能让程序继续运行?谢谢!!!!!import java.util.*;
class CD{
String id;
float price;
String name;
String company;
CD(String id,float price,String name,String company){
this.id=id;
this.price=price;
this.name=name;
}
}
@SuppressWarnings({ "serial", "unchecked" })
public class CDMessage extends Vector{
public static Scanner scanner=new Scanner(System.in);
Vector myVector;
public void createCDMessage(){
myVector=new Vector(50,1);
String name,id,company;
float price;
boolean N=true;
while(N){
System.out.println("id");
try{
id=scanner.next();
}catch(Exception e){
System.out.println("you are wrong");
id="nul";
}
System.out.println("price");
try{
price=scanner.nextFloat();
}catch(Exception e){
System.out.println("you are wrong");
price=0;
}
System.out.println("name");
try{
name=scanner.next();
}catch(Exception e){
System.out.println("you are wrong");
name="nul";
}
System.out.println("company");
try{
company=scanner.next();
}catch(Exception e){
System.out.println("you are wrong");
company="nul";
}
CD cd=new CD(id,price,name,company);
System.out.println(cd.name);
myVector.addElement(cd);
System.out.println("1continue \n else break:\nplease enter your choice");
int i=scanner.nextInt();
if(i!=1)
N=false;
}
}
public void findByName(){
int m=0;
String name;
System.out.println("qing shuru CD name");
try{
name=scanner.next();
}catch(Exception e){
System.out.println("you are wrong");
name="nul";
}
for(m=0;m<myVector.size();m++)
{
CD cd=(CD)myVector.elementAt(m);
if(((cd.name).trim()).compareTo(name.trim())==0){
System.out.println("name "+cd.name+" id "+cd.id+" company "+ price "+cd.price);
break;
}
}
System.out.println("do not find the message");
}
public void deletedByName(){
String name;
int m;
System.out.println("qing shuru the deleted CD name");
try{
name=scanner.next();
}catch(Exception e){
System.out.println("you are wrong");
name="nul";
}
for(m=0;m<myVector.size();m++)
{
CD cd=(CD)myVector.elementAt(m);
if(((cd.name).trim()).compareTo(name.trim())==0){
System.out.println("the deleleted cd message\nname "+cd.name+" id "+cd.id+" company "+ price "+cd.price);
myVector.removeElementAt(m);
break;
}
}
}
public static void main(String[] args){
CDMessage myCdMessage=new CDMessage();
myCdMessage.createCDMessage();
myCdMessage.findByName();
myCdMessage.deletedByName();
myCdMessage.findByName();
}
}
[ 本帖最后由 任永旺 于 2010-7-18 12:25 编辑 ]