0

Can someone please help me with the following code. My temp array of Point2D's never overwrite the previous one when the for loop execute again.

I used for-loops to print data in order to test if temp array does change...and it certainly don't... Why would something like this happen?

Point2D is defined in a different class. It's x, y and z values are public.

I think it may be the .clone() function?

Thanks!


Point2D[] pointArr;
pointArr = populateArr(N);  

    for (int i = 0; i < N; i++)
    {
        Point2D[] temp = pointArr.clone();

        if (i >= 0) 
        { 
            Point2D exch = temp[i]; 
            temp[i] = temp[0]; 
            temp[0] = exch;
        //temp[0].z = 0.0;
        }

        System.out.println();

        temp = determine_slopes(temp, N);

        Arrays.sort(temp, temp[0].X_ORDER);

    }

EDIT 1: Just to clarify... I actually WANT the Point2D[] temp to change with each iteration... But for some reason it doesn't change. What may be the problem?

EDIT 2: My output for pointArr is as follow:

10000 0 0.0 0 10000 0.0 3000 7000 0.0 7000 3000 0.0 20000 21000 0.0 3000 4000 0.0 14000 15000 0.0 6000 7000 0.0

When I print I print the temp after the first Iteration my ouput does not look like pointArr's... It's totally different.

3 Answers 3

4

The array declaration should be moved outside of the loop. Else you will create a new instance of the array each iteration of the loop, overwriting the previous one:

Point2D[] pointArr;
pointArr = populateArr(N);
Point2D[] temp = pointArr.clone();  //move to here

    for (int i = 0; i < N; i++)
    {


        if (i >= 0) 
        { 
            Point2D exch = temp[i]; 
            temp[i] = temp[0]; 
            temp[0] = exch;
        //temp[0].z = 0.0;
        }

        System.out.println();

        temp = determine_slopes(temp, N);

        Arrays.sort(temp, temp[0].X_ORDER);

    }

Edit: Im not sure this will help, but try creating a the copy of pointArr using a loop instead of clone:

Point2D[] pointArr;
pointArr = populateArr(N);
Point2D[] temp;

for(int i=0; i<pointArr.length; i++)
    pointArr[i]=temp[i];

If you, as you say, for some reason want temp to be overwritten, move the loop etc(that copy the array) inside your loop

Sign up to request clarification or add additional context in comments.

6 Comments

Thanks for the help! I actually want the temp array to be overwritten... Each iteration of the loop I want a new array that's exactly like pointArr. I changed my code, but it still doesn't overwrite the temp[]
Well, you are creating a clone of pointArr with temp each iteration. This means that it will contain the same elements as pointArr. So, you are creating a new array each iteration, that's a copy of pointArr, and since pointArr never change temp will look the same each time it's created. Im not sure what you're trying to achive here :)
That's exactly what I try to achieve, but it's not working... For some reason the temp does not change...
Well, temp does not change since it's a copy of pointArr, and pointArr never change. Try adding something to pointArr each iteration, and that will be added to temp aswell
If you read the api for clone(), it says that in order to be able to call clone(), the object needs to implement Clonable. Read more here docs.oracle.com/javase/1.4.2/docs/api/java/lang/… . Nevermind this. When reading about Point2D, it implements Clonable (unless you're using an own instance of Point2D)
|
2

This line should be outside the for loop

Point2D[] temp = pointArr.clone();

Comments

1

I think your issue is that every time you loop through, you are re-setting your temp array to the pointArr clone. That's why when you exchange the values, it is lost every time b/c you are changing it in your temp array, but then setting temp array to the clone every time. Try declaring your temp array out of the for loop.

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.