Results 1 to 8 of 8
  1. #1
    nhmllr's Avatar
    nhmllr is offline Member
    Join Date
    Aug 2011
    Posts
    51
    Rep Power
    0

    Default I want to make an array in a method...

    This is going to be an array of objects that will be created inside my "startRoom()" method.
    The idea is that this method looks at a map I've already made, and counts how many of an object to put in.
    Then the method makes an array, called "objectsInRoom[]" and the length is how many objects were counted to be in the room, based on the map that I've already made for it. The thing is, how do I make a an array IN a method, of changing length, that will still be around once the method is done?

    Thanks

    EDIT: Also, everytime a new room starts, the method has to change the array to make it a new length. Is there a way to clear out an array of values?
    Last edited by nhmllr; 10-01-2011 at 06:46 PM.

  2. #2
    nhmllr's Avatar
    nhmllr is offline Member
    Join Date
    Aug 2011
    Posts
    51
    Rep Power
    0

    Default Re: I want to make an array in a method...

    In other words, can I make an array, clear out all of the data, and then make a new array of a different length of the same name?

  3. #3
    JosAH's Avatar
    JosAH is offline Moderator
    Join Date
    Sep 2008
    Location
    Voorschoten, the Netherlands
    Posts
    13,436
    Blog Entries
    7
    Rep Power
    20

    Default Re: I want to make an array in a method...

    Don't use an array; arrays are kind of stupid, i.e. when their size is set you can't change it. Use an ArrayList instead (or any other class that implements the List interface).

    kind regards,

    Jos
    cenosillicaphobia: the fear for an empty beer glass

  4. #4
    Solarsonic is offline Senior Member
    Join Date
    Mar 2011
    Posts
    261
    Rep Power
    4

    Default Re: I want to make an array in a method...

    If you create your array within a method then it's what is called a local variable, meaning it's only existence is within that method.

    You want to create a class variable, by instantiating it outside methods, like at the very top of the class for example.

    Then when you give it a value inside your method, the value will stay in other methods as well.

    Also an array has a set number of elements, if you want to have a different number of elements then you'll have to make another array.

    In this situation you'd best use an ArrayList, which is a list that doesn't have a set number of elements.
    Last edited by Solarsonic; 10-02-2011 at 02:44 AM.

  5. #5
    JosAH's Avatar
    JosAH is offline Moderator
    Join Date
    Sep 2008
    Location
    Voorschoten, the Netherlands
    Posts
    13,436
    Blog Entries
    7
    Rep Power
    20

    Default Re: I want to make an array in a method...

    Quote Originally Posted by Solarsonic View Post
    If you create your array within a method then it's what is called a local variable, meaning it's only existence is within that method.

    You want to create a class variable, by instantiating it outside methods, like at the very top of the class for example.

    Then when you give it a value inside your method, the value will stay in other methods as well.

    Also an array has a set number of elements, if you want to have a different number of elements then you'll have to make another array.

    In this situation you'd best use an ArrayList, which is a list that doesn't have a set number of elements.
    This 'advice' (mind the quotes) doesn't make much sense; if the only reference to an array is local, then the array will be lost for the posterity when the local variable goes out of scope; if you keep a reference that doesn't go out of scope then the array will stay from the claws of the garbage collector. You can create the array anywhere you want.

    kind regards,

    Jos
    cenosillicaphobia: the fear for an empty beer glass

  6. #6
    Join Date
    Oct 2011
    Location
    HELL
    Posts
    8
    Rep Power
    0

    Default Re: I want to make an array in a method...

    is it possible to change the length of an array? i think its not because if you wanna add or lessen the length of an array, you have to put the elements of an array into a new array with different length which means that you have to create a new array to pass the thing within the old array with the help of a method in our Java API (library)

    So the easiest way to do it is to use arraylist.

  7. #7
    popeus is offline Member
    Join Date
    Sep 2011
    Posts
    12
    Rep Power
    0

    Default Re: I want to make an array in a method...

    @andie,
    Its not like its an honorous task to increment an array size.

    Java Code:
          Object[] temp = new Object[mainArray.getLength()+1];
          for (int i = 0; i < mainArray.getLength(); i++)
             temp[i] = mainArray[i];
          mainArray = temp;
    Last edited by popeus; 10-02-2011 at 08:01 AM.

  8. #8
    Fubarable's Avatar
    Fubarable is offline Moderator
    Join Date
    Jun 2008
    Posts
    19,316
    Blog Entries
    1
    Rep Power
    26

    Default Re: I want to make an array in a method...

    Quote Originally Posted by popeus View Post
    @andie,
    Its not like its an honorous task to increment an array size.

    Java Code:
          Object[] temp = new Object[mainArray.getLength()+1];
          for (int i = 0; i < mainArray.getLength(); i++)
             temp[i] = mainArray[i];
          mainArray = temp;
    ArrayLists do this and more, and in a more intelligent manner. To the original poster, you're much better to ignore this advice and go with Jos's recommendation to use a generic ArrayList.

Similar Threads

  1. How to make a morph Method
    By AnthonyTTaylor in forum Java 2D
    Replies: 6
    Last Post: 11-15-2011, 11:41 PM
  2. creating a make grid method
    By Matt198525 in forum New To Java
    Replies: 13
    Last Post: 10-29-2010, 02:47 AM
  3. Replies: 2
    Last Post: 09-06-2010, 01:03 AM
  4. Static method cannot make new objects?
    By zerkz in forum New To Java
    Replies: 2
    Last Post: 10-15-2009, 03:17 AM
  5. want to make an array
    By doha786 in forum New To Java
    Replies: 3
    Last Post: 03-29-2009, 01:49 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
  •