How can i store the return of a 2D array? For example
public Class A{
public String something(){
String []some_array=new String[2];
//stuff in here...sets elements of our array to something
//unsure of the next line
return some_array[];
}
public static void main(String[] args) {
String []some_other_array=new String[2];
A myA=new A();
//unsure of the next line
some_other_array[]=myA.something();
How do i go about returning the first-second element of the array that is being returned to be the first-second element of the array that i am storing it in?
Also can anyone clarify is it legal to use parameter variables from my method something() inside of it without making them equal to another variable first? I always thought you either had to declare another variable inside the method and make it equal to the parameter and use the new variable you created.
Change the return type of your method as the one shown below:
Also note the return statement doesn’t have
[]brackets beside the variablesome_array.And in your main method you should write like this:
Also note in the above code that while assigning the array returned by your method to a local variable (here
some_other_array) you don’t have to use[]brackets.And don’t initialize your
some_other_arrayvariable, just make a declaration so that when you assign it the array returned by the method then it will be automatically have the size of the returned array.