“使用未经检查或不安全的操作”警告

时间:2022-07-31 15:25:01

While trying to compile the compiler gives following error :

尝试编译编译器时会出现以下错误:

Note: SleepingBarbers.java uses unchecked or unsafe operations. Note: Recompile with -Xlint:unchecked for details.

注意:SleepingBarbers.java使用未经检查或不安全的操作。注意:使用-Xlint重新编译:取消选中以获取详细信息。

public class SleepingBarbers extends JPanel implements ActionListener{

    private ImageIcon asleep=new ImageIcon("barberAsleep.gif"),awake=new ImageIcon("barberAwake.gif");

    private static BufferedImage[][] images=new BufferedImage[1][8];

    private JButton addCustomer;

    private BufferedImage backdrop;

    private javax.swing.Timer timer=new javax.swing.Timer(30, this);

    private LinkedList<Customer> customers=new LinkedList<Customer>(), 
    seatedAtQueue=new LinkedList<Customer>();

    private boolean queued[]=new boolean[3], seated[]=new boolean[3];

    private int customerCount=1, left=0, served=0;

    private Barber barbers[]=new Barber[3];

    private JLabel leftLabel=new JLabel("0"), servedLabel=new JLabel("0");


    public SleepingBarbers() throws IOException{
        super();
        setLayout(null);
        for(int ctr=0; ctr<3; ctr++){
            try{
                barbers[ctr]=new Barber();
                add(barbers[ctr]);
            }catch(IOException e){}
        }
        for(int ctr=0; ctr<8; ctr++){
            try{
                images[0][ctr]=ImageIO.read(new File("char"+0+"_"+ctr+".gif"));
            }catch(Exception e){}
        }
        backdrop=ImageIO.read(new File("barbersBack.jpg"));
        leftLabel.setForeground(Color.WHITE);
        add(leftLabel);
        leftLabel.setBounds(615,483,100,20);
        servedLabel.setForeground(Color.WHITE);
        add(servedLabel);
        servedLabel.setBounds(615,470,100,20);

        addCustomer=new JButton(new ImageIcon("addCust.jpg"));
        add(addCustomer);
        addCustomer.addActionListener(this);
        addCustomer.setBounds(487,530,263,29);
        addCustomer.setRolloverIcon(new ImageIcon("addCustOver.jpg"));
        addCustomer.setPressedIcon(new ImageIcon("addCustPressed.jpg"));
        addCustomer.setBorder(BorderFactory.createEmptyBorder(0,0,0,0));
        barbers[0].setBounds(92,112,30,130);
        barbers[1].setBounds(71,235,30,130);
        barbers[2].setBounds(49,354,30,130);

        timer.start();
    }

    public void actionPerformed(ActionEvent e){
        Object src=e.getSource();
        timer.stop();
        if(src==addCustomer){
            Customer currentCust=new Customer((int)(Math.random()*1));
            add(currentCust);
            customers.add(currentCust);
            currentCust.setBounds(500,80,40,130);

        }
        if(src==timer&&!customers.isEmpty()){
            Collections.sort(customers);
            for(int ctr=0; ctr<customers.size(); ctr++){
                remove(customers.get(ctr));
            }
            for(int ctr=0; ctr<customers.size(); ctr++){
                add(customers.get(ctr));
            }
            for(int ctr=0; ctr<customers.size(); ctr++){
                for(int ctr1=0; ctr1<seatedAtQueue.size(); ctr1++){
                    (seatedAtQueue.get(ctr1)).findBarber();
                }
                (customers.get(ctr)).animate();
                if((customers.get(ctr)).exit){
                    remove(customers.get(ctr));
                    customers.remove(ctr);
                    //System.out.println("cutomer size "+customers.size());
                }
            }
        }
        repaint();
        validate();
        timer.restart();
    }

    private class Customer extends JPanel implements Comparable{
        private int  character, atQueue, move, time, barberNo;
        private boolean beenQueued, exit, atBarber, sit, done;
        private BufferedImage image;
        private LinkedList actions=new LinkedList<String>(), motions=new LinkedList<Integer>();
        //private Integer moveX, moveY;

        private Customer(int c){
            super(new BorderLayout());
            character=c;
            setOpaque(false);
            motions.add(2);
            actions.add("down");
            actions.add("enter");
            image=images[c][0];
        }

        public void animate(){
            if(!actions.isEmpty()){
                if(!motions.isEmpty()) move=(Integer)motions.getFirst();
                //System.out.println(move+" moving ");
                if(actions.getFirst().equals("down")){
                    setLocation(getX(), getY()+4);
                    if(!done)image=images[character][0];
                    else image=images[character][6];
                    move--;
                }else if(actions.getFirst().equals("up")){
                    setLocation(getX(), getY()-4);
                    image=images[character][1];
                    move--;
                }else if(actions.getFirst().equals("left")){
                    image=images[character][3];
                    setLocation(getX()-4, getY());
                    move--;
                }else if(actions.getFirst().equals("right")){
                    if(!done)image=images[character][2];
                    else image=images[character][7];
                    setLocation(getX()+4, getY());
                    move--;
                }else if(actions.getFirst().equals("exit")){
                    exit=true;
                    if(!atBarber){
                        left++;
                        leftLabel.setText(""+left);
                    }
                    return;
                }else if(actions.getFirst().equals("sit")){
                    if(!sit){
                        sit=true;
                        setLocation(getX(), getY()-1);
                        image=images[character][4];
                    }
                }
                if(actions.getFirst().equals("cut")){
                    image=images[character][4];
                    setOpaque(false);
                    if(time==5100){
                        done=true;
                        served++;
                        servedLabel.setText(""+served);
                        actions.removeFirst();
                        //System.out.println("waiting "+time);
                        seated[barberNo]=false;
                        barbers[barberNo].sleep();
                        motions.add(20);
                        actions.add("right");
                        if(barberNo==0){
                            motions.add(64);
                            actions.add("down");
                        }else if(barberNo==1){
                            motions.add(33);
                            actions.add("down");
                        }else if(barberNo==2){
                            motions.add(3);
                            actions.add("down");
                        }
                        motions.add(50);
                        actions.add("right");
                        motions.add(10);
                        actions.add("down");
                        actions.add("exit");
                    }else{
                        barbers[barberNo].wakeUp();
                        time+=30;
                    }
                    return;
                }
                if(actions.getFirst().equals("enter")){
                    findBarber();
                    if(atBarber) return;
                    //System.out.println(actions.getFirst());
                    actions.removeFirst();
                    for(int ctr=0; ctr<3; ctr++){
                        if(!queued[ctr]){
                            seatedAtQueue.add(this);
                            queued[ctr]=true;
                            beenQueued=true;
                            atQueue=ctr;
                            if(ctr==0){
                                motions.add(33);    
                                actions.add("left");
                                actions.add("sit");

                            }else if(ctr==1){
                                motions.add(24);
                                actions.add("right");
                                actions.add("sit");

                            }else if(ctr==2){
                                motions.add(39);
                                actions.add("right");
                                actions.add("sit");                             
                            }
                            //System.out.println("queue at "+ctr);
                            break;
                        }
                    }
                    //System.out.println(actions.getFirst());
                    if(!beenQueued){
                        actions.add("wander");
                    }
                    return;
                }
                if(actions.getFirst().equals("wander")){
                    actions.removeFirst();
                    beenQueued=true;
                    actions.add("left");
                    motions.add(10);
                    actions.add("right");
                    motions.add(20);
                    actions.add("left");
                    motions.add(10);
                    actions.add("up");
                    motions.add(15);
                    actions.add("exit");
                    return;
                }

                if(!motions.isEmpty()) motions.set(0, move);
                if(move==0&&!motions.isEmpty()){
                    actions.removeFirst();
                    motions.removeFirst();
                }
            }
        }

        public void findBarber(){
            //System.out.println("hello");
            if(!motions.isEmpty()) return;
            for(int ctr=0; ctr<3; ctr++){
                if(!seated[ctr]){
                    seatedAtQueue.remove(this);
                    seated[ctr]=true;
                    atBarber=true;
                    barberNo=ctr;
                    actions.clear();
                    //System.out.println("going to "+ctr);
                    if(beenQueued){
                        setLocation(getX(), getY()+1);
                        queued[atQueue]=false;
                        if(atQueue==0){
                            //System.out.println("yep");
                            motions.add(43);
                            actions.add("left");
                        }else if(atQueue==1){
                            motions.add(101);
                            actions.add("left");
                        }else if(atQueue==2){
                            motions.add(115);
                            actions.add("left");
                        }

                    }else{
                        motions.add(77);
                        actions.add("left");
                    }
                    if(ctr==0){
                        motions.add(15);
                        actions.add("down");
                        motions.add(19);
                        actions.add("left");
                    }else if(ctr==1){
                        motions.add(46);
                        actions.add("down");
                        motions.add(24);
                        actions.add("left");
                    }else if(ctr==2){
                        motions.add(76);
                        actions.add("down");
                        motions.add(30);
                        actions.add("left");
                    }

                    actions.add("cut");
                    //System.out.println(actions.getFirst());
                    break;
                }
            }
        }

        public int compareTo(Object other){
            if(((Customer)other).getY()>getY()){
                return 1;
            }else{
                return -1;
            }
        }

        public void paintComponent(Graphics g){
            super.paintComponent(g);
            if(actions.getFirst().equals("cut")) ((Graphics2D)g).drawImage(new ImageIcon("char0_5.gif").getImage(),0,0,this);
                else ((Graphics2D)g).drawImage(image, null,0,0);
        }
    }

    public void paintComponent(Graphics g){
        super.paintComponent(g);
        ((Graphics2D)g).drawImage(backdrop, null, 0, 0);
    }

    private class Barber extends JPanel{
        private ImageIcon barberImage;
        private Barber() throws IOException{
            super();
            setOpaque(false);
            sleep();
        }

        private void sleep(){
            barberImage=asleep;
        }

        private void wakeUp(){
            barberImage=awake;
        }

        public void paintComponent(Graphics g){
            super.paintComponent(g);
            ((Graphics2D)g).drawImage(barberImage.getImage(),0,0,this);
        }
    }
    public static void createAndShowGUI(){
        try{
            JFrame x=new JFrame("Sleeping Barber's Problem");
            x.setSize(799,730);
            x.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
            x.add(new SleepingBarbers());
            x.setResizable(false);
            x.setVisible(true);
        }catch(IOException e){}

    }
    public static void main(String args[]){
        SwingUtilities.invokeLater(new Runnable(){
                public void run() {
                createAndShowGUI();
            }
        });
    }
}

while compiling with Xlint it gives following warnings:-
SleepingBarbers.java:69: warning: [unchecked] unchecked method invocation: metho d sort in class Collections is applied to given types Collections.sort(customers);

在使用Xlint进行编译时,它会发出以下警告: - SleepingBarbers.java:69:警告:[未经检查]未经检查的方法调用:类中的方法类别集合应用于给定类型Collections.sort(customers);

                            ^

required: List

found: LinkedList

where T is a type-variable:

其中T是一个类型变量:

T extends Comparable<? super T> declared in method <T>sort(List<T>)

SleepingBarbers.java:93: warning: [rawtypes] found raw type: Comparable private class Customer extends JPanel implements Comparable{ ^

SleepingBarbers.java:93:警告:[rawtypes]发现原始类型:可比私有类Customer扩展JPanel实现Comparable {^

missing type arguments for generic class Comparable where T is a type-variable:

缺少泛型类的类型参数Comparable其中T是一个类型变量:

T extends Object declared in interface Comparable

SleepingBarbers.java:93: warning: [serial] serializable class SleepingBarbers.Cu stomer has no definition of serialVersionUID

SleepingBarbers.java:93:警告:[serial]可序列化类SleepingBarbers.Cu stomer没有serialVersionUID的定义

private class Customer extends JPanel implements Comparable{
        ^

SleepingBarbers.java:314: warning: [serial] serializable class SleepingBarbers.B arber has no definition of serialVersionUID

SleepingBarbers.java:314:警告:[serial]可序列化类SleepingBarbers.B arber没有serialVersionUID的定义

private class Barber extends JPanel{
        ^

SleepingBarbers.java:9: warning: [serial] serializable class SleepingBarbers has no definition of serialVersionUID

SleepingBarbers.java:9:警告:[serial]可序列化类SleepingBarbers没有serialVersionUID的定义

public class SleepingBarbers extends JPanel implements ActionListener{ ^

公共类SleepingBarbers扩展JPanel实现ActionListener {^

SleepingBarbers.java:113: warning: [cast] redundant cast to Integer if(!motions.isEmpty()) move=(Integer)motions.getFirst();

SleepingBarbers.java:113:警告:[cast]冗余强制转换为Integer if(!motions.isEmpty())move =(Integer)motions.getFirst();

                                                ^

6 warnings

2 个解决方案

#1


4  

That's a warning, not an error. You are using collections without type specifiers. As it suggests, recompile with -Xlint:unchecked for details.

这是一个警告,而不是错误。您正在使用没有类型说明符的集合。如它所示,使用-Xlint重新编译:取消选中以获取详细信息。

Change

private LinkedList actions=new LinkedList<String>(), motions=new LinkedList<Integer>();

To

private LinkedList<String> actions = new LinkedList<String>();
private LinkedList<Integer> motions = new LinkedList<Integer>();

In Java 7, you don't have to do it that way. Read here for details.

在Java 7中,您不必这样做。请阅读此处了解详情。

#2


1  

do like this

这样做

private LinkedList<Customer> customers =new LinkedList<Customer>();
private LinkedList<Customer> seatedAtQueue =new LinkedList<Customer>();

#1


4  

That's a warning, not an error. You are using collections without type specifiers. As it suggests, recompile with -Xlint:unchecked for details.

这是一个警告,而不是错误。您正在使用没有类型说明符的集合。如它所示,使用-Xlint重新编译:取消选中以获取详细信息。

Change

private LinkedList actions=new LinkedList<String>(), motions=new LinkedList<Integer>();

To

private LinkedList<String> actions = new LinkedList<String>();
private LinkedList<Integer> motions = new LinkedList<Integer>();

In Java 7, you don't have to do it that way. Read here for details.

在Java 7中,您不必这样做。请阅读此处了解详情。

#2


1  

do like this

这样做

private LinkedList<Customer> customers =new LinkedList<Customer>();
private LinkedList<Customer> seatedAtQueue =new LinkedList<Customer>();