Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8713197
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 13, 20262026-06-13T05:17:13+00:00 2026-06-13T05:17:13+00:00

My question is similar to this one Add elements to Arraylist and it replaces

  • 0

My question is similar to this one Add elements to Arraylist and it replaces all previous elements in Java . Though my variables are not static. Still everytime I add one, the other values will be that value.

Important code:

int counter = 1;
// Threshold the image to get a binary image
image.threshold(44);
image.showImage();
int[] directionFIRST = new int[2];
// Get the first white pixel on the boundary
int[] pixelFIRST = image.getFirstBoundaryPixel();

image.updatePicture(pixelFIRST[0], pixelFIRST[1]);

directionFIRST = getInitialDirection(image, pixelFIRST);

//Create an array for the output.  It will hold the (x,y) coordinates of
//every pixel around the border of the region to be contour-traced.  The
//directions are also saved for the chain code.
List<int[]> listCONTOUR = new ArrayList<int[]>();
List<int[]> listDIRECTION = new ArrayList<int[]>();

// Create a variable which will be used to tell the algorithm when to stop:
boolean stopCondition = false;
int[][] ROTmatrix90 = new int[][]{{0, 1}, {-1, 0}};
int[][] ROTmatrix180 = new int[][]{{-1, 0}, {0, -1}};

int[] tempPIX = pixelFIRST;
int[] tempDIR = directionFIRST;

while (!stopCondition) {

    //Take the direction opposit the current direction
    tempDIR = multiply(ROTmatrix180, tempDIR);

    tempPIX[0] = tempPIX[0] + tempDIR[0];
    tempPIX[1] = tempPIX[1] + tempDIR[1];
    if (image.get(tempPIX[0], tempPIX[1]) == 1) {
        listCONTOUR.add(tempPIX);
        listDIRECTION.add(tempDIR);
    } else {
        tempDIR = multiply(ROTmatrix90, tempDIR);
        tempPIX[0] = tempPIX[0] + tempDIR[0];
        tempPIX[1] = tempPIX[1] + tempDIR[1];
        if (image.get(tempPIX[0], tempPIX[1]) == 1) {
            listCONTOUR.add(tempPIX);
            listDIRECTION.add(tempDIR);
        } else {
            tempDIR = multiply(ROTmatrix90, tempDIR);
            tempPIX[0] = tempPIX[0] + tempDIR[0];
            tempPIX[1] = tempPIX[1] + tempDIR[1];
            if (image.get(tempPIX[0], tempPIX[1]) == 1) {
                listCONTOUR.add(tempPIX);
                listDIRECTION.add(tempDIR);
            } else {
                tempDIR = multiply(ROTmatrix90, tempDIR);
                tempPIX[0] = tempPIX[0] + tempDIR[0];
                tempPIX[1] = tempPIX[1] + tempDIR[1];
                if (image.get(tempPIX[0], tempPIX[1]) == 1) {
                    listCONTOUR.add(tempPIX);
                    listDIRECTION.add(tempDIR);
                } else {
                    tempDIR = multiply(ROTmatrix90, tempDIR);
                    tempPIX[0] = tempPIX[0] + tempDIR[0];
                    tempPIX[1] = tempPIX[1] + tempDIR[1];
                    if (image.get(tempPIX[0], tempPIX[1]) == 1) {
                        listCONTOUR.add(tempPIX);
                        listDIRECTION.add(tempDIR);
                    }
                }
            }
        }
    }

    counter++;
    image.updatePicture(tempPIX[0], tempPIX[1]);
    System.out.println(tempPIX[0] + " , " + tempPIX[1]);

    if(tempPIX[0]== tempPIX[1]){
        System.out.println("test");
    }

    if ((listCONTOUR.size() > 2) && (tempPIX[0] == listCONTOUR.get(1)[0]) && (tempPIX[0] == listCONTOUR.get(1)[1])) {
        stopCondition = true;
        listCONTOUR.remove(listCONTOUR.get(listCONTOUR.size() - 1));
        listDIRECTION.remove(listDIRECTION.get(listDIRECTION.size() - 1));
    }
}

When I check the values of listCONTOUR after lets say 5 runs of the loop, all the values are the same, which is not possible. I searched for a solution, but all solutions are pointing towards the fact that the variable is static. While in my case it isn’t. It is just a simple local variable which is initiated in a function and used within 1 function.

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-13T05:17:14+00:00Added an answer on June 13, 2026 at 5:17 am

    tempPIX is reference to an int[] array in memory.

    Each time you update the array and add it to the list, you are simply adding the same reference to the same array over and over again.

    A better solution would be to create a new array on each loop…

    int[] tmpAry = new int[2];
    tmpAry[0] = ntempPIX[0] + tempDIR[0];
    tmpAry[1] = tempPIX[1] + tempDIR[1];
    
    tempPIX = tmpAry; // Reassign the reference so the rest of the code doesn't need to be updated
    

    UPDATED from comments

    Well, all I can say is, I don’t know what you’re doing…

    public class TestArrays {
    
        public static void main(String[] args) {
            List<int[]> listOfValues = new ArrayList<int[]>();
            int[] outter = new int[] {1, 2, 3, 4};
    
            listOfValues.add(outter);
            dump(outter);
            for (int index = 0; index < 5; index++) {            
                int[] inner = new int[] {
                    rand(),
                    rand(),
                    rand(),
                    rand()
                };
                outter = inner;
                dump(outter);
                listOfValues.add(outter);            
            }
    
            int index = 0;
            for (int[] values : listOfValues) {
                System.out.print("[" + index + "] ");
                dump(values);
                index++;
            }
    
        }
    
        public static void dump(int[] values) {
            for (int value : values) {
                System.out.print(value + ", ");
            }
            System.out.println("\b\b"); // Cheeck...;)
        }
    
        public static int rand() {
            return (int)Math.round(Math.random() * 100);
        }
    
    }
    

    Which outputs something like…

    1, 2, 3, 4
    44, 35, 76, 9
    44, 11, 17, 35
    99, 24, 39, 23
    20, 31, 9, 66
    45, 50, 60, 27
    [0] 1, 2, 3, 4
    [1] 44, 35, 76, 9
    [2] 44, 11, 17, 35
    [3] 99, 24, 39, 23
    [4] 20, 31, 9, 66
    [5] 45, 50, 60, 27
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

This question is similar to this one How do I add options to a
This question is similar to this one, but with an extra wrinkle: Auto-removing all
The question is similar to this question. However, this one is about exceptions, not
This is quite similar question to one older but the solution did not work
I know this is a similar question to my previous one however its slightly
First of all, I know I asked a similar question before but this one
This question is similar to this one , but not a duplicate because I'm
I'm looking for an answer to a question similar to this one: protect users'
my question is similar to this one , but I don't want to use
My question is similar to this one , but I would like to replicate

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.