[SOLVED] Difficuties with Creating Arrays

Discussion in 'Plugin Development' started by caldabeast, Jun 2, 2012.

Thread Status:
Not open for further replies.
  1. Offline

    caldabeast

    In the plugin I am currently coding, I need to create an array with technically infinite amounts of items in them, and then use it in the code-snippit I have below. I have tried several different things but I am unable to get it to do what I need it to do.

    What I need it to do:
    Code:
    if (players.length == 0){ //if there arent any tellPlayers defined yet, set [0] as a new one
                        players[0] = new tellPlayer(p.getName());
                    }else{
                        Boolean found = false;
                        for (tellPlayer tP : players){ //if there are some that exist already, can through them to see if there is one that has the name of the player that needs one
                            if (tP.getName().equals(p.getName())){
                                found = true;
                            }
                        }
                        
                        if (!found){ //creates it if the player does not already have a tellPlayer object
                            players[players.length] = new tellPlayer(p.getName());
                        }
                        
                    }
    
    The tellPlayer object is basically an object that stores two strings:
    The owner's name and The owner's target (used a bunch in the plugin)

    My issue is that I cannot get this array actually created. I have tried the things below, but it does not work with it:
    Code:
    tellPlayer[] players;
    
    Code:
    tellPlayer[] players = new tellPlayer[100];
    
    Code:
    tellPlayer[] players;
    players = new tellPlayer[100];
    
    what could I possibly do for this array to be created properly?
    Thanks for any help you may give!
     
  2. Offline

    Tempelchat

    All this statements are correct. However in the first one, players will be null afterwards.


    This make no sense because the length is only "0" if you wrote new tellPlayer[0] which means you can't save anything in there.
    in other words:
    Code:java
    1. tellPlayer[] players = new tellPlayer[100];
    2. System.out.println(players.length); //prints: 100



    If you want to find out if there is already a player stored in the array, you have to iterate over the array and see if there is a object in it.

    edit:
    Have a look at ArrayList or LinkedList then.

    PS: By convention, class names start with uppercase
     
  3. Offline

    caldabeast

    Tempelchat
    How would I iterate over an array to check if there is something already stored in it? Would I check for null?
     
  4. Offline

    Tempelchat

    This is correct.
    For dynamic (infinite) elements as you want to have, you should have a look at ArrayList or LinkedList.
     
  5. Thing to remember when using arrays: length != last index. Because the index starts with 0 not with 1. Meaning if the length 1 the only index will be 0. If the length is 0, no index exists.
     
  6. Offline

    caldabeast

    that is an extreme derp moment on my part.

    Thanks! Im pretty sure you just solved my problem!
     
Thread Status:
Not open for further replies.

Share This Page