How do you set a variable equal to an array in Java. For example I have a class for input and a class for calculations which hold arrays. when I accept the user input from the input class how do I pass that variable into the array of my calculation clas开发者_JS百科s?
You should look into varargs. Code sample below:
public MyClass method(String ...arg);
You can call this method as :
method("test1", "test2", "test3"); // with arbitrary number of values.
Or as
String[] test = something;
method(test);
Unless you have strict requirements to use arrays, you should probably be using a Collection, like a List.
For example, if you're trying to manage an array of ints, you could instead do:
List<int> intList = new ArrayList<int>();
Then, if you really need the data in the form of an array, you can do:
intList.toArray();
Which would return an array holding the integer values in your list. Lists are easier to read and use.
精彩评论