Why are these variables not loading in ArrayList

I cannot find whats wrong with this code. I have short type array called "data". I assign data[i] value to private Point objects variable x then I add this object to ArrayList and proceed with next i. In the arraylist all instances have same value - last one that was added. Why do you think its so?

    ArrayList<Point> p = new ArrayList<Point>();

        System.out.println("start test");
    for (int i=0;i<data.length;i++){

        bPunkt.x=(int) data[i];
        p.add(bPunkt);
        System.out.println(""+bPunkt.x);
    }
        System.out.println("middle of test");
    for (int i=0;i<p.size();i++){
        System.out.println(""+p.get(i).x);
    }
        System.out.println("end test");

this is what system.out prints :

start test
1
0
1
3
3
5
3
5
5
4
middle of test
4
4
4
4
4
4
4
4
4
4
end test

Answers


You are modifying the same instance bPunkt and sticking it in the list. So in the end you have a list with the same object over and over in it.


Need Your Help

Android: listener on the status bar notification

android listener statusbar

android:Is there a way to listen to the status bar notification when it is being pulled down?

How to post a form to a switch statement to display a value in PHP

php html forms switch-statement

what I need help with today is "How do I submit a form, so that if a certain value is selected, it will display a echo" - Here is what I have so far, but no data is being displayed: