This is the example that I found online(I modified it) with a method that returns array. The array being passed is iterated to be a new array in a reversed order.
public class MultDim {
public static void main(String[] args)
{
double[] myList = {1.9, 2.9, 3.4, 3.5};
double[] returned = returnArr(myList);
for (double elem : returned) {
System.out.println(elem);
}
}
public static double[] returnArr(double[] list) {
double[] result = new double[list.length];
for (int i = 0, j = list.length - 1; i < list.length; i++, j--) {
result[j] = list[i];
}
return result;
}
}
What I want is to pass the elements of the array being passed to the method to be in the same order in the array that is returned. Here is how I tried to solve it. But is returns an error which is(The primitive type int of list.length does not have a field i)
public class MultDim {
public static void main(String[] args) {
double[] myList = {1.9, 2.9, 3.4, 3.5};
double[] returned = returnArr(myList);
for (double elem : returned) {
System.out.println(elem);
}
}
public static double[] returnArr(double[] list) {
double[] result = new double[list.length];
for (int i = 0, j = 0; j < list.length, i < list.length; i++, j++) {
result[j] = list[i];
}
return result;
}
}
I am just wondering what I concept missed from this.
j < list.length, i < list.length- I imagine that's expected to only ever be a single boolean condition. That said, since bothiandjare going to be the same value, andlistandresultwill both have the same length, you can get rid ofjentirely:for(int i = 0; i < list.length; i++) {result[i] = list[i];}.j < list.length && i < list.length