Reputation: 4706
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.
Upvotes: 1
Views: 336
Reputation: 19201
To solve the actual problem - how to copy array data you should probably use the functions provided by the Java libraries.
Example:
final double[] copy1 = Arrays.copyOf(myList, myList.length);
Other options include System.arraycopy.
Upvotes: 0
Reputation: 1332
the code's in first image will reverse array fields but this is yours and will worked!
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;
}
}
Upvotes: 0
Reputation: 4086
You should take a look on how a for
needs to be constructed
for (init; stop_condition; actions)
init
can be a list of java initialisation, separated with comma (,
)
stop_condition
needs to be a single java expression that evaluates to a boolean, in your case it should be :
j < list.length && i < list.length
action can be a list of java expressions, separated with comma (,
)
way more simple you could write (as i and j are always the same):
for(int i = 0;i < list.length; i++)
{
result[j] = list[i];
}
Upvotes: 1
Reputation: 72844
The compiler error is due to the condition in the for
loop. j < list.length, i < list.length
is an invalid condition. If you meant both j < list.length
and i < list.length
, then use the logical AND operator (&&
):
j < list.length && i < list.length
Having said this, you don't need to use both i
and j
in the loop since the returned array has the same size as the array passed as an argument. So you could just do with:
for (int i = 0; i < list.length; i++) {
result[i] = list[i];
}
Upvotes: 1
Reputation: 9065
stop condition in the for
loop can't write as this
for(int i = 0, j = 0; j < list.length, i < list.length; i++, j++)
in this situation a simple
for(int i = 0, j = 0; i < list.length; i++, j++)
is good enough
or just:
for(int i=0; i<list.length; i++){
result[i] = list[i];
}
Upvotes: 2