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?
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?
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
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.
Re: I want to make an array in a method...
Quote:
Originally Posted by
Solarsonic
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
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.
Re: I want to make an array in a method...
@andie,
Its not like its an honorous task to increment an array size.
Code:
Object[] temp = new Object[mainArray.getLength()+1];
for (int i = 0; i < mainArray.getLength(); i++)
temp[i] = mainArray[i];
mainArray = temp;
Re: I want to make an array in a method...
Quote:
Originally Posted by
popeus
@andie,
Its not like its an honorous task to increment an array size.
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.