You still need to create the array, even if you do not assign it to a variable. Try this:
public int[] getData() {
return new int[] {a,b,c,d};
}
Your code sample did not work because the compiler, for one thing, still needs to know what type you are attempting to create via static initialization {}.
You still need to create the array, even if you do not assign it to a variable. Try this:
public int[] getData() {
return new int[] {a,b,c,d};
}
Your code sample did not work because the compiler, for one thing, still needs to know what type you are attempting to create via static initialization {}.
You been to construct the object that the function is returning, the following should solve your issue.
public int[] getData() {
return new int[]{a,b,c,d};
}
hope this helps
How do you return an array in a method?
How to return a temporary int array in Java - Stack Overflow
memory management - How to return an array in one line in Java? - Stack Overflow
Java array pass without reference - Stack Overflow
Videos
It is returning the array, but all returning something (including an Array) does is just what it sounds like: returns the value. In your case, you are getting the value of numbers(), which happens to be an array (it could be anything and you would still have this issue), and just letting it sit there.
When a function returns anything, it is essentially replacing the line in which it is called (in your case: numbers();) with the return value. So, what your main method is really executing is essentially the following:
public static void main(String[] args) {
{1,2,3};
}
Which, of course, will appear to do nothing. If you wanted to do something with the return value, you could do something like this:
public static void main(String[] args){
int[] result = numbers();
for (int i=0; i<result.length; i++) {
System.out.print(result[i]+" ");
}
}
Of course the method numbers() returns an array, it's just that you're doing nothing with it. Try this in main():
int[] array = numbers(); // obtain the array
System.out.println(Arrays.toString(array)); // now print it
That will show the array in the console.
Today, I had a interview but I failed miserably in the first coding test. And before I can finish it, the interviewer just hung up the phone....sob sob
I am completely stumped how to do this question and have spent the last 2 hours figuring out to no avail and I hope to be able to get some helps here.
So, you are given an array = {1,2,6,4,5}
And a target no say 10.
The method must return the answer in {6,4}.
How can I achieve it ?
Here's my lame attempt:
public static int[][]ab(int[]arr, int target){
target = 10;
int[][]temp = null;
int a = 0; int b = 0;
int sum = 0;
for(int i = 0; i < arr.length; i++) {
for(int j = i; j < arr.length; j++) {
a = arr[i];
b = arr[j];
if ( a + b == 10) {
System.out.printf("Pair : " + arr[i], arr[j] );
}
}
}
return temp;// i do not know how to put arr[i] and arr[j] and store it in a array
}Tks.
I got the idea to use new int[] as the answers below. While, what's the reason the we don't need new int[] when doing initiation?
When you write int[] ret = {0,1};, it is essentially a shortcut of writing int[] ret = new int[]{0,1};. From the doc:
Alternatively, you can use the shortcut syntax to create and initialize an array:
int[] anArray = { 100, 200, 300, 400, 500, 600, 700, 800, 900, 1000 };
Now when you return you have to explicitly write return new int[]{0,1}; because you are not doing an assignment operation(create and initialize as per the doc) to the array and hence you cannot use the shortcut. You will have to create an object using new.
You can do something known as returning an anonymous array (as it has no name).
Do
return new int[] {0, 1};
for that...
do you mean like this?
int[] dim1 = myObject.getCoord();
public int[] getCoord() {
return new int[] {y1, x1} ;
}
only one array is ever created, by the method call, and only has one reference, dim1.
but ideally you probably don't want a "get" method to be creating new things, as just by looking at the declaration you might not expect that. personally, i'd prefer
int[] dim1 = myObject.createCoord();
public int[] createCoord() {
return new int[] {y1, x1} ;
}
which makes it explicit that the method is "creating" a thing.
No matter how you try to shorten your code, it doesn't change the fact that when the call to getCoord() returns, it simply returns a reference to the array, not a copy of the contents of the array.
Trying to make the code more succinct won't help you with your performance problem.
Just don't initialize dim1 to new int[2]; at the beginning if you want, because it gets replaced right after anyways. But that's small potatoes.
It has to be
int[] result = insertionSort(new int[]{10,3,4,12,2});
{10,3,4,12,2} is a syntactic sugar for array initialization, which must go with the declaration statement like the one in the following -
int[] arr = {10,3,4,12,2};
Something as follows is not allowed too -
int[] arr; // already declared here but not initialized yet
arr = {10,3,4,12,2}; // not a declaration statement so not allowed
insertionSort({10,3,4,12,2})
is not valid java because you don't specify a type in your method call. The JVM does not know what type of array this is. Is it an array with double values or with int values?
What you can do is insertionSort(new int[]{10, 3 ,4 12, 2});