1

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.

2
  • I suspect the problem is this part: j < list.length, i < list.length - I imagine that's expected to only ever be a single boolean condition. That said, since both i and j are going to be the same value, and list and result will both have the same length, you can get rid of j entirely: for(int i = 0; i < list.length; i++) {result[i] = list[i];}. Commented Dec 22, 2014 at 14:05
  • And if he wants anyway to check i and j: j < list.length && i < list.length Commented Dec 22, 2014 at 14:08

5 Answers 5

2

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];
}
Sign up to request clarification or add additional context in comments.

4 Comments

new is a reserved keyword
sorry for that, I just edit it while you are posting
@armnotstrong so I am not supposed to put two conditions that way. thank you for the solution.
yes, the stop condition required to be a boolean expression
1

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];
}

Comments

1

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];
}

Comments

0

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;
  }
}

Comments

0

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.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.