我如何清除整个阵列?

时间:2015-10-19 09:25:12

标签: java arrays save load clear

我必须创建一个清除按钮,按下该按钮可清除屏幕上的所有图像。图像目前在一个数组中。我刚刚将数组中的内容保存到save.txt文件中。现在我需要点击load,它会在加载之前清除屏幕上的所有图像。我该怎么做?这是我的保存代码

else if (savePicture.isPointInElement(clickX, clickY)){ 
    FileWriter fw = new FileWriter("save.txt");
    for (int i = 0; i < image; i++){

        if (arraysticker[image] != null){
            String name;
            int x,y;
            name = sticker.getname();
            x = sticker.getx();
            y = sticker.gety();
            fw.write(name + " " + x + " " + y + "\n");
        }
    }
    fw.flush();
    fw.close();
    System.out.println("saved");
}

我的整个代码是

public class main {
public static void main(String[] args) throws IOException {
    EZ.initialize(1644,1022);//sets page size
    sticker [] arraysticker = new sticker [20];//creates an array to hold 20 sticker
    EZImage backgroundPicture = EZ.addImage("background.png", EZ.getWindowWidth() / 2, EZ.getWindowHeight() / 2); //set position of background
    EZImage rectanglePicture = EZ.addImage("rectangle.png", EZ.getWindowWidth() / 2, EZ.getWindowHeight () / 7); //set position of palette
    EZImage hatPicture = EZ.addImage("hat.png",1* EZ.getWindowWidth() / 10, EZ.getWindowHeight () / 6); //set position of stickers
    EZImage bluntPicture = EZ.addImage("blunt.png",1* EZ.getWindowWidth() / 3, EZ.getWindowHeight () / 6); //set position of stickers
    EZImage dealwithitPicture = EZ.addImage("dealwithit.png",3* EZ.getWindowWidth() / 5, EZ.getWindowHeight () / 6); //set position of stickers
    EZImage weedPicture = EZ.addImage("weed.png",10* EZ.getWindowWidth() / 11, EZ.getWindowHeight () / 6); //set position of stickers
    EZImage savePicture = EZ.addImage("save.png", 19* EZ.getWindowWidth() / 20, 3* EZ.getWindowHeight () / 9);  //set position of save button
    EZImage loadPicture = EZ.addImage("load.png", 19* EZ.getWindowWidth() / 20, 4* EZ.getWindowHeight () / 9);
    EZImage clearPicture = EZ.addImage("clear.png", 19* EZ.getWindowWidth() / 20, 5* EZ.getWindowHeight () / 9);

    EZSound hatsound = EZ.addSound("airhorn.wav"); //imports airhorn.wav and assigns it to hatsound
    EZSound bluntsound = EZ.addSound("yungdog.wav"); //import yungdog.wav and assigns it to bluntsound
    EZSound dealwithitsound = EZ.addSound("sandstorm.wav"); //imports sandstorm.wav and assigns it to dealwithitsound
    EZSound weedsound = EZ.addSound("weed.wav"); //imports weed.wav and assigns it to weedsound

    sticker arrayhat = new sticker (20, "hat.png");
    sticker arrayblunt = new sticker (20, "blunt.png");
    sticker arraydealwithit = new sticker (20, "dealwithit.png");
    sticker arrayweed = new sticker (20, "weed.png");

    int image = 0;//declares an integer variable named image
    //creates a boolean variable and sets it to false
    boolean hatSoundPlay = false;
    boolean bluntSoundPlay = false;
    boolean dealwithitSoundPlay = false;
    boolean weedSoundPlay = false;
        while (true){//while it is true
        if (EZInteraction.wasMouseLeftButtonReleased()){//if left button is released
        int clickX = EZInteraction.getXMouse(); //get x coordinates
        int clickY = EZInteraction.getYMouse();//get y coordinates

            if (hatPicture.isPointInElement(clickX, clickY)){//if hat is in the x and y coordinates
                bluntSoundPlay = false; //turn all sounds false
                weedSoundPlay = false; 
                dealwithitSoundPlay = false; 
                if (!hatSoundPlay) { //if hatsound not play
                    hatsound.play(); //hatsound will play
                    hatSoundPlay = true; //hat sound is now true    
                }
            }
            else if(bluntPicture.isPointInElement(clickX, clickY)){//if blunt is in the x and y coordinates
                hatSoundPlay = false;//turn all sounds false
                weedSoundPlay = false;
                dealwithitSoundPlay = false;
                if (!bluntSoundPlay) {//if bluntsound not play
                    bluntsound.play(); //then bluntsound will play
                    bluntSoundPlay = true; //bluntSoundPLay is now assigned to true
                }
            }
            else if(dealwithitPicture.isPointInElement(clickX, clickY)){//if dealwithit is in the x and y coordinates
                hatSoundPlay = false;//turn all sounds false
                bluntSoundPlay = false;
                weedSoundPlay = false;
                if (!dealwithitSoundPlay) {//if dealwithitsound not play
                    dealwithitsound.play(); //then dealwithitsound will pay
                    dealwithitSoundPlay = true; //dealwithitSoundPlay is assigned as true
                }
            }
            else if(weedPicture.isPointInElement(clickX, clickY)){//if weed is in the x and y coordinates
                dealwithitSoundPlay = false;//turn all sounds false
                hatSoundPlay = false;
                bluntSoundPlay = false;
                if (!weedSoundPlay) {//if weedsound not play
                    weedsound.play(); //then weedsound will play
                    weedSoundPlay = true; //sets the varialbe weedSoundPlay to true
                } 
            }


            else if (backgroundPicture.isPointInElement(clickX, clickY) && !savePicture.isPointInElement(clickX, clickY) 
                    && !loadPicture.isPointInElement(clickX, clickY) && !clearPicture.isPointInElement(clickX, clickY) 
                    && !rectanglePicture.isPointInElement(clickX, clickY)){
                if (hatSoundPlay){ //if hatSoundPlay is true
                    hatsound.play(); //and a hatsound will play
                    sticker sticker1 = new sticker();//creates a new sticker1
                    sticker1.arraysticker(hatPicture, "hat.png", clickX, clickY);//places sticker
                    image ++;//increments image by 1
                    arraysticker[image] = sticker1;//puts sticker 1 into the array
                    FileWriter fw = new FileWriter("save.txt");


                } else if (bluntSoundPlay){ //else if bluntSound is true
                    bluntsound.play(); //and blunt sound will play
                    sticker sticker2 = new sticker();
                    sticker2.arraysticker(bluntPicture, "blunt.png", clickX, clickY);//places sticker
                    image ++;//increments image by 1
                    arraysticker[image] = sticker2;//puts sticker 2 into the array

                }else if (dealwithitSoundPlay){ //if dealwithitsound is true
                    dealwithitsound.play(); //dealwithitsound will play
                    sticker sticker3 = new sticker();
                    sticker3.arraysticker(dealwithitPicture, "dealwithit.png", clickX, clickY);//places sticker
                    image ++;//increments image by 1
                    arraysticker[image] = sticker3;//puts sticker 3 into the array

                }else if (weedSoundPlay){ //if weedsound is true
                    weedsound.play(); //weedsound will also play
                    sticker sticker4 = new sticker();
                    sticker4.arraysticker(weedPicture, "weed.png", clickX, clickY);//places sticker
                    image ++;//increments image by 1
                    arraysticker[image] = sticker4;//puts sticker 4 into the array
                }
            }
            else if (savePicture.isPointInElement(clickX, clickY)){ 
                FileWriter fw = new FileWriter("save.txt");
                for (int i = 0; i < image; i++){

                    if (arraysticker[image] != null){
                        String name;
                        int x,y;
                        name = sticker.getname();
                        x = sticker.getx();
                        y = sticker.gety();
                        fw.write(name + " " + x + " " + y + "\n");
                    }
                }

                fw.flush();
                fw.close();
                System.out.println("saved");
            }
            /*else if (loadPicture.isPointInElement(clickX, clickY)){
                Scanner sc = new Scanner (new File ("save.txt"));
                for (int i = 0; i < image; i++){
                    arraysticker.removeImage(arraysticker.image1[image]);
                }
            }*/

    }
        EZ.refreshScreen();     
    }
}

}

2 个答案:

答案 0 :(得分:1)

如果arraysticker不是final,那么简单的重新分配就可以了:

arraysticker = new sicker[arraysticker.length];

这假设您需要数组保持相同的大小。如果没有必要,那么创建一个空数组:

arraysticker = new sicker[0];

如果它是最终的那么你可以将所有元素归零:

Arrays.fill( arraysticker, null );

答案 1 :(得分:0)

只需将null指定给参考。

arraysticker = null; // garbage collector will take care

arraysticker = new sicker[arraysticker.length]; // initialized to new length

或使用

Arrays.fill(arraysticker, null);

  

它只是遍历每个元素并将其设置为null。这个   当然不允许你调整数组的大小(为零),如果这是什么   你的意思是“空”。

public static void fill(Object[] a,Object val)
  

将指定的Object引用分配给。的每个元素   指定的对象数组。

     

参数:       a - 要填充的数组       val - 要存储在数组的所有元素中的值抛出:       ArrayStoreException - 如果指定的值不是可以存储在指定数组中的运行时类型