Results 1 to 4 of 4
  1. #1
    AGuyWhoSkis is offline Member
    Join Date
    May 2013
    Posts
    2
    Rep Power
    0

    Question Creating variables/arrays while the program runs

    I need to make a program (specifically, a server side plugin for Minecraft) which needs to hold an array for each player. This could mean as little as 1 array, or as many as a few thousand of arrays needed (Some Minecraft servers have even more players). I am stumped as to how I can solve this, and have done some research on the subject, but nothing stuck out. Can any of you help? It would be greatly appreciated!

  2. #2
    jim829 is offline Senior Member
    Join Date
    Jan 2013
    Location
    Northern Virginia, United States
    Posts
    3,777
    Rep Power
    5

    Default Re: Creating variables/arrays while the program runs

    You can always create an array on the fly.

    int size= 10;
    String [] strings = new String[size];

    if you need to increase the size, you can use Arrays.copyOf to copy the array and reallocate a new size at the same time.

    However, I recommend using Lists. Check out ArrayList. It grows dynamically, and you can delete and insert values into the List.

    Regards,
    Jim
    The JavaTM Tutorials | SSCCE | Java Naming Conventions
    Poor planning on your part does not constitute an emergency on my part

  3. #3
    AGuyWhoSkis is offline Member
    Join Date
    May 2013
    Posts
    2
    Rep Power
    0

    Default Re: Creating variables/arrays while the program runs

    Thanks for the quick reply, Jim.

    I cannot figure out a way to name an Array List differently every time a new player joins the game. I can recognize when the player joins and I have assigned the player's name to a string, but if I try and plug in the variable it says it's a duplicate and gives me an error. Here is the code:
    (By the way, the code would work normally, if I were to

    @EventHandler
    public void onPlayerJoin(PlayerJoinEvent event) {
    Player player = event.getPlayer();
    ArrayList<String> player = new ArrayList<String>(); //"player" is underlined and gives me the duplicate variable error

    }
    Last edited by AGuyWhoSkis; 05-31-2013 at 03:52 AM. Reason: format

  4. #4
    jim829 is offline Senior Member
    Join Date
    Jan 2013
    Location
    Northern Virginia, United States
    Posts
    3,777
    Rep Power
    5

    Default Re: Creating variables/arrays while the program runs

    Check out HashMap. I think it will do what you want. It uses a key value pair to look up elements. One element can be an ArrayList of whatever type you need
    and the key can be the user name.

    Java Code:
    Map<String, List<String>> userDB = new HashMap<>();
    
    //then somewhere in the program you can do
    
    String userName;
    // new user joins game
    userDB.put(userName, new ArrayList<String>());  // creates the entry for a user.
    // get List specific for user
    List<String> userList = userDB.get(userName);  // retrieves the entry for the user.

    I suggest you write some small programs to play around with lists and maps to see if they solve your problem.

    Regards,
    Jim
    The JavaTM Tutorials | SSCCE | Java Naming Conventions
    Poor planning on your part does not constitute an emergency on my part

Similar Threads

  1. Using Arrays as Variables.
    By wired-in=p in forum New To Java
    Replies: 4
    Last Post: 07-24-2011, 03:32 PM
  2. playing music while program runs
    By liluma in forum New To Java
    Replies: 2
    Last Post: 05-04-2011, 05:16 PM
  3. creating variables in a for loop?
    By sehudson in forum New To Java
    Replies: 7
    Last Post: 03-01-2011, 03:37 AM
  4. Replies: 0
    Last Post: 10-04-2009, 02:47 PM
  5. Program Runs in NetBEans but Not in cmd
    By MeathUltra in forum NetBeans
    Replies: 1
    Last Post: 12-09-2008, 05:38 AM

Posting Permissions

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