0

I am trying to fill an arrayList with objects created from reading integers from a text file that represent points (two integers per line). I'm attempting to do this in a loop. The ArrayList seems to fill, but when I print it out after, all the elements in each index are the same as the last element added to the ArrayList. This seems to have something to do with each arrayList index pointing to the object (my newbie guess). Do I have to create a unique object for each arrayList entry? Is there an easy way to add to this code to do this?

public class Point2DDemo extends Point2D<Double>
{
    ArrayList<Point2DDemo> Points = new ArrayList<Point2DDemo>(7);


    /**
     * Constructor for objects of class Point2DDemo
     */
    public Point2DDemo()
    {

    }

    public Point2DDemo(double first, double second)
    {
        setFirst(first);
        setSecond(second);
    }

    public void putPair(double point1, double point2){
        this.setFirst(point1);
        this.setSecond(point2);
    }

    /**
     * 
     * 
     * @param  y   a sample parameter for a method
     * @return     the sum of x and y 
     */
    public void createList()
    {
        FileIO readFile = new FileIO();
        readFile.openInputFile();
        String pointLine = null;

        Point2DDemo newPoints = new Point2DDemo();

        StringTokenizer stringSplit = null;

        while(readFile.hasInputLine())
        {

           pointLine = readFile.readInputLine();
           stringSplit = new StringTokenizer(pointLine);

           double pointX = Double.parseDouble(stringSplit.nextToken());
           double pointY = Double.parseDouble(stringSplit.nextToken());


           newPoints.putPair(pointX, pointY);

           Points.add(newPoints);





        }
        for(int i = 0; i < Points.size(); i++)
         System.out.println(Points.get(i));

         readFile.closeInputFile();

    }
Vinay
  • 6,891
  • 4
  • 32
  • 50
Mike D
  • 41
  • 1
  • 3
  • 7

3 Answers3

1

Obviously, you have only one Point2DDemo object newPoint through out the code. In your while loop, you are changing the same newPoint with different values, and it finally has one pair of values.

You should put the Point2DDemo newPoints = new Point2DDemo(); INTO while loop:

public void createList()
{
    FileIO readFile = new FileIO();
    readFile.openInputFile();
    String pointLine = null;

    StringTokenizer stringSplit = null;

    while(readFile.hasInputLine())
    {


       Point2DDemo newPoints = new Point2DDemo();
       pointLine = readFile.readInputLine();
       stringSplit = new StringTokenizer(pointLine);

       double pointX = Double.parseDouble(stringSplit.nextToken());
       double pointY = Double.parseDouble(stringSplit.nextToken());


       newPoints.putPair(pointX, pointY);

       Points.add(newPoints);

    }
    for(int i = 0; i < Points.size(); i++)
     System.out.println(Points.get(i));

     readFile.closeInputFile();

}
shuangwhywhy
  • 5,475
  • 2
  • 18
  • 28
0

All you're currently doing is repeatedly setting the values of a single Point2DDemo and adding multiple references to it to the ArrayList.

To answer your question: yes, you'll need to create a new Object for every set of points.

Something like:

Point2D point = new Point2D( xPoint, yPoint );

Should do. (this code won't work as written, but will get you moving in the right direction.)

Ray Stojonic
  • 1,260
  • 1
  • 7
  • 11
0

What happens is that you populate your array list with references to the same object.

In your loop you need to create new newpoints for each iteration.

Something along these lines:

// Outside of the lopp
Pont2DDemo newpoints;

// inside the loop
newpoints = new Point2DDemo();
newPoints.putPair(pointX, pointY);
Points.add(newPoints);
PM 77-1
  • 12,933
  • 21
  • 68
  • 111