我将如何清除整个阵列?

Posted

技术标签:

【中文标题】我将如何清除整个阵列?【英文标题】:How would I clear an entire array? 【发布时间】:2015-10-19 09:25:12 【问题描述】:

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

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();     
    

【问题讨论】:

arraysticker = new sticker[arraysticker.length]; 够了吗? 【参考方案1】:

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

arraysticker = new sicker[arraysticker.length];

这假设您需要数组保持相同的大小。如果不需要,则创建一个空数组:

arraysticker = new sicker[0];

如果它是最终的,那么你可以清空所有元素:

Arrays.fill( arraysticker, null );

【讨论】:

我也希望它从屏幕上清除。以前放置的所有图像,我希望它全部消失。因为当我单击加载按钮时,它必须清除整个运行屏幕并加载我保存的内容。【参考方案2】:

只需将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 - 如果指定的值不是可以存储在指定数组中的运行时类型

【讨论】:

以上是关于我将如何清除整个阵列?的主要内容,如果未能解决你的问题,请参考以下文章

如何使用 Tkinter 清除整个 Treeview

我将如何清除所有 Rails 会话?

如何清除堆上分配的未使用数组的整个内存

如何在不清除整个队列的情况下从 Resque 队列中删除特定作业?

如何永久清除在启用软删除的情况下创建的整个 azure keyvault?

Postgres:在从 bash 脚本重新创建/重新填充之前清除整个数据库