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?
memory management - How to return an array in one line in Java? - Stack Overflow
java return array in method - Stack Overflow
A comparison of Rust’s borrow checker to the one in C#
Videos
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.
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.
1)You cannot have methods inside method in Java. Move your oddIndex() methods outside main() method.
2) And you cannot local variables of a method in another method. So I moved your variable j to oddIndex() method
public class Main {
public static void main(String[] args) {
int[] arr = { 1, 2, 3, 4, 5, 6, 7, 8 };
System.out.println("1.e odd index numbers in array : " + oddIndex(arr));
}
public static int[] oddIndex(int[] array) {
int j = 0;
int newArrSize = array.length;
if ((newArrSize % 2) != 0) {
newArrSize--;
}
int[] newArr = new int[newArrSize];
for (int i = 0; i < array.length; i++)
if ((array[i] % 2) == 0) {
newArr[j] = array[i];
j++;
}
return newArr;
}
}
And also, as Jhamon commented, your method name and logic inside are not matched. Odd index != odd value.
Issue with your code:
- you can not define method inside another method.
- If you want to return an array that contains the odd index elements from the original array. you should check
index%2!=0instead of checking array value of that index.
try this
public class Main {
public static void main(String[] args) {
int[] arr = {1, 2, 3, 4, 5, 6, 7, 8};
System.out.println("1.e odd index numbers in array : " + Arrays.toString(oddIndex(arr)));
}
public static int[] oddIndex(int[] array){
int[] newArr = new int[array.length];
int j=0;
for (int i = 0; i < array.length; i++){
if ((i % 2) != 0) {
newArr[j++] = array[i];
}
}
return Arrays.copyOf(newArr, j);
}
}
output:
1.e odd index numbers in array : [2, 4, 6, 8] // odd index elements from original array