Results 1 to 3 of 3
  1. #1
    i8java is offline Member
    Join Date
    Apr 2010
    Posts
    6
    Rep Power
    0

    Default emptying/clearing an array

    public void removePlayers(Player[] client)
    {
    String G = "";
    for (int i = 0; i < numPlayers; ++i)
    {
    G = G + client[i];
    }
    JOptionPane.showMessageDialog(null, G, "Player Info", 1);
    }

    i have set up this fubction as i want to clear all the data in the array. the info is all brought forward with the string and i want to be able to clear this wen sent back.

    either code or a button that will be able to clear this will work. i have ran into so many walls and cannot solve it.

    any help will be gretfully accepted,

    thank you

  2. #2
    JosAH's Avatar
    JosAH is online now Moderator
    Join Date
    Sep 2008
    Location
    Voorschoten, the Netherlands
    Posts
    13,002
    Blog Entries
    7
    Rep Power
    20

    Default

    Quote Originally Posted by i8java View Post
    public void removePlayers(Player[] client)
    {
    String G = "";
    for (int i = 0; i < numPlayers; ++i)
    {
    G = G + client[i];
    }
    JOptionPane.showMessageDialog(null, G, "Player Info", 1);
    }

    i have set up this fubction as i want to clear all the data in the array. the info is all brought forward with the string and i want to be able to clear this wen sent back.

    either code or a button that will be able to clear this will work. i have ran into so many walls and cannot solve it.

    any help will be gretfully accepted,

    thank you
    You can either make that method return (another) empty Player array (and have the caller of that method accept that, as in:

    Java Code:
    Player[] client= ...; // values in here
    client= removePlayers(client);
    ...
    
    Player[] removePlayers(Player[] client) {
       ...
       return new Player[numPlayers]; // return empty array
    }
    or you can wipe out all the elements of the existing array in your method:

    Java Code:
    void removePlayers(Player[] client) {
       ...
       for (int i= 0; i < numPlayers; i++)
          client[i]= null;
    }
    kind regards,

    Jos

  3. #3
    alacn's Avatar
    alacn is offline Senior Member
    Join Date
    Jun 2010
    Location
    UK. near London
    Posts
    118
    Rep Power
    0

    Default

    PHP Code:
    client = removePlayers(client,client.length);
    PHP Code:
        
    public players[] removePlayers(players clients[],int length){
    
            for(int i=0;i<length;i++){
                clients[i] = null;
            }
    
    
            return clients;
        }
    Teaching myself java so that i can eventually join the industry! Started in June 2010

Similar Threads

  1. convert byte array into char array
    By kgkamaraj in forum New To Java
    Replies: 4
    Last Post: 09-13-2011, 11:32 AM
  2. Help Clearing BufferedImage
    By ketann in forum Java 2D
    Replies: 19
    Last Post: 03-29-2010, 07:45 PM
  3. Help needed Clearing the contents of a File
    By sandeepsai17 in forum New To Java
    Replies: 5
    Last Post: 07-02-2009, 02:31 PM
  4. Replies: 1
    Last Post: 03-31-2009, 06:40 AM
  5. MouseListener - clearing a textField when clicked on?!
    By sailor_girl in forum AWT / Swing
    Replies: 4
    Last Post: 03-01-2009, 05:01 PM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •