Results 1 to 8 of 8
- 10-01-2011, 06:43 PM #1
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.
- 10-01-2011, 08:55 PM #2
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?
- 10-01-2011, 09:00 PM #3
- Join Date
- Sep 2008
- Location
- Voorschoten, the Netherlands
- Posts
- 11,601
- Blog Entries
- 7
- Rep Power
- 17
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,
JosWhen people rob a bank they get a penalty; when banks rob people they get a bonus.
- 10-02-2011, 02:41 AM #4
Senior Member
- Join Date
- Mar 2011
- Posts
- 261
- Rep Power
- 3
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.
- 10-02-2011, 07:00 AM #5
- Join Date
- Sep 2008
- Location
- Voorschoten, the Netherlands
- Posts
- 11,601
- Blog Entries
- 7
- Rep Power
- 17
Re: I want to make an array in a method...
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,
JosWhen people rob a bank they get a penalty; when banks rob people they get a bonus.
- 10-02-2011, 07:49 AM #6
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.
- 10-02-2011, 07:58 AM #7
Member
- Join Date
- Sep 2011
- Posts
- 12
- Rep Power
- 0
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.
-
Re: I want to make an array in a method...
Similar Threads
-
How to make a morph Method
By AnthonyTTaylor in forum Java 2DReplies: 6Last Post: 11-15-2011, 11:41 PM -
creating a make grid method
By Matt198525 in forum New To JavaReplies: 13Last Post: 10-29-2010, 02:47 AM -
Trying to make an array list // inserting an element to middle of array
By javanew in forum New To JavaReplies: 2Last Post: 09-06-2010, 01:03 AM -
Static method cannot make new objects?
By zerkz in forum New To JavaReplies: 2Last Post: 10-15-2009, 03:17 AM -
want to make an array
By doha786 in forum New To JavaReplies: 3Last Post: 03-29-2009, 01:49 AM


LinkBack URL
About LinkBacks
Reply With Quote

Bookmarks