java - wrong setting an arraylist -


i want ask question arraylist. in program defined arraylist , user defined object. problem when want add object arraylist, adds object, next time when give different values user object, sets values of old object added before new one. mean, example have 13 in old object , new 1 14, makes old 1 14. couldn't find solution this. i'm working on drawing program. i'm posting parts of code.

     public class tester extends jpanel implements mousemotionlistener, mouselistener {  arraylist<lines> array = new arraylist<lines>();     lines l1;       ...     public tester(){     l1 = new lines();     l1.point1 = new point();     l1.point2 = new point();     l1.denklem = new int[3]; 

and thi how add object arraylist

         else if(lineci == true){         if(mouseclicks == 0){             l1.point1.x = e.getx();             l1.point1.y = e.gety();             statusbar.settext( string.format( "clicked @ [%d, %d]",                      e.getx(), e.gety() ) );             mouseclicks++;             starter = false;         }         else if(mouseclicks == 1){             l1.point2.x = e.getx();             l1.point2.y = e.gety();             statusbar.settext( string.format( "clicked @ [%d, %d]",                      e.getx(), e.gety() ) );             mouseclicks = 0;             int = l1.point2.y - l1.point1.y;             int b = l1.point1.x - l1.point2.x;             int c = (l1.point2.x * l1.point1.y) - (l1.point1.x * l1.point2.y);             l1.denklem[0] = a;             l1.denklem[1] = b;             l1.denklem[2] = c;              array.add(l1);         //  array3.add(l1);             repaint();         }      } 

when click mouse, if lineci true, draws line according points. last lines when print elements of arraylist. if draw 10 lines, 10 elements of arraylist same. never keeps old values inside arraylist. way boolean starter not important. forget remove it.

you adding same object repeatedly list. each addition list merely adding reference same object: l1. hence, when update state of l1, old "object" appears have changed.

to rectify need create new instance of lines each addition wish perform.


Comments

Popular posts from this blog

objective c - Change font of selected text in UITextView -

php - Accessing POST data in Facebook cavas app -

c# - Getting control value when switching a view as part of a multiview -