Defining an ArrayList of objects in Java

Hi,

I would like to define an ArrayList of Geometries, a geometry can be either a point, polyline or polygon. The last two in turn are defined as an ArrayList of points.

I am getting confused on how to create such class structures for this?

This is just a mock up of how I would like the end result

    ArrayList Geometry
        Element 1> Point
        Element 2> Point
        Element 3> Polyline (This will be an ArrayList of points)
        Element 4> Polygon (This will be an ArrayList of points)
        Element 5> Point

Any help would be greatly appreciated!
AssetFXAsked:
Who is Participating?
 
krakatoaCommented:
You'd have to overload the Geometry constructor, and add a String parameter identifying which type you want to instantiate.
0
 
krakatoaCommented:
ArrayList arr = new ArrayList();
arr.add(new Point());
arr.add(new Polygon());
arr.add(new Rectangle());

for(int a=0;a<arr.size();a++){

System.out.println(arr.get(a));
}

Open in new window


. . . etc . . .
0
 
AssetFXAuthor Commented:
Hi krakatoa,

appreciate your help! I am good with how to create an ArrayList now.

What I am having trouble with is forming the classes, the point, polyline and polygon classes are easy and I've done them as follows:
public class point {
    public final double dX;
    public final double dY;

    public point(double cX, double cY) {
        this.dX = cX;
        this.dY = cY;
    }
	    
    public point(String pointCoordsXY) {
        String[] pointDef = pointCoordsXY.split(" ");
        this.dX = Double.parseDouble(pointDef[0]);
        this.dY = Double.parseDouble(pointDef[1]);
    }
}

public class polyline {
    ArrayList<point> polylineList;
		
    public polyline(String lineCoordsXY) {
        String[] pointCoordsXYArray = lineCoordsXY.split(", ");
        for(String pointCoordsXY : pointCoordsXYArray){
            this.polylineList.add(new point(pointCoordsXY));
        }
    }
}

public class polygon {
    ArrayList<point> polygonList;

    public polygon(String lineCoordsXY) {
        String[] pointCoordsXYArray = lineCoordsXY.split(", ");
        for(String pointCoordsXY : pointCoordsXYArray){
            this.polygonList.add(new point(pointCoordsXY));
        }
    }
}

Open in new window

I was thinking that I would declare my ArrayList as follows so it has some idea of what is populating it and then populate with a bunch of correct geometry types.
public ArrayList<geometry> geometries = new ArrayList<geometry>();
geometries.add(new point("23.456 32.567"));
geometries.add(new polyline("23.456 32.567, 23.6776 32.987"));
geometries.add(new polygon("23.456 32.567, 23.6776 32.987, 23.456 32.567"));

Open in new window

So do I now define everything as follows or have I got this wrong?
Actually I know I have this wrong, I basically want the geometry class to say it can either be a point, polyline or polygon, how to do that is the real question.
public class geometry {
	
    public class point {
        public final double dX;
        public final double dY;

        public point(double cX, double cY) {
            this.dX = cX;
            this.dY = cY;
        }
	    
        public point(String pointCoordsXY) {
            String[] pointDef = pointCoordsXY.split(" ");
            this.dX = Double.parseDouble(pointDef[0]);
            this.dY = Double.parseDouble(pointDef[1]);
        }
    }

    public class polyline {
        ArrayList<point> polylineList;
		
        public polyline(String lineCoordsXY) {
            String[] pointCoordsXYArray = lineCoordsXY.split(", ");
            for(String pointCoordsXY : pointCoordsXYArray){
                this.polylineList.add(new point(pointCoordsXY));
            }
        }
    }

    public class polygon {
        ArrayList<point> polygonList;

        public polygon(String lineCoordsXY) {
            String[] pointCoordsXYArray = lineCoordsXY.split(", ");
            for(String pointCoordsXY : pointCoordsXYArray){
                this.polygonList.add(new point(pointCoordsXY));
            }
        }
    }
}

Open in new window

0
Get expert help—faster!

Need expert help—fast? Use the Help Bell for personalized assistance getting answers to your important questions.

 
krakatoaCommented:
Actually I know I have this wrong, I basically want the geometry class to say it can either be a point, polyline or polygon, how to do that is the real question.

"say" ? Say to whom?
0
 
AssetFXAuthor Commented:
Hi,

'Say' to the ArrayList.

Maybe I'm thinking about it in the wrong way, lets try another approach.

End result: I would like to add geometry objects into the ArrayList.
When I call the geometry constructor it will hold two things, a geometry type which will be String and the object of type point, polyline or polygon.
How can I define an object to hold one of the three types?

geoObj = new point();
geoObj = new polyline();
geoObj = new polygon;

What kind of variable does geoObj need to be that it can hold either of the three?
0
 
krakatoaCommented:
import java.awt.*;
import java.util.*;


public class Geometry {

public String geoType=null;


 
 public Geometry (String pointCoordsXY, String type){switch (type){case "strP":System.out.println("Point made of String");break;case "poLi":System.out.println("PolyLine");break;case "Pgn":System.out.println("Polygon");break;}}
 public Geometry (Double x, Double y){System.out.println("Point made of Doubles");}
	
    public class Point{
        public final double dX;
        public final double dY;

        public Point(double cX, double cY) {
            this.dX = cX;
            this.dY = cY;
            geoType = "Point";
        }
	    
        public Point(String pointCoordsXY) {
            String[] pointDef = pointCoordsXY.split(" ");
            this.dX = Double.parseDouble(pointDef[0]);
            this.dY = Double.parseDouble(pointDef[1]);
           geoType = "Point";
        }
    }

    public class Polyline {
        ArrayList<Point> polylineList;
		
        public Polyline(String lineCoordsXY) {
            String[] pointCoordsXYArray = lineCoordsXY.split(", ");
            for(String pointCoordsXY : pointCoordsXYArray){
                this.polylineList.add(new Point(pointCoordsXY));
            }
            geoType = "Polyline";
        }
    }

    public class Polygon {
        ArrayList<Point> polygonList;

        public Polygon(String lineCoordsXY) {
            String[] pointCoordsXYArray = lineCoordsXY.split(", ");
            for(String pointCoordsXY : pointCoordsXYArray){
                this.polygonList.add(new Point(pointCoordsXY));
            }
          geoType = "Polygon";
        }
    }

  public String getType(){

	return(this.geoType);
 }
}

Open in new window


 . . . as a schema perhaps.

(Test this crudely with  : )

class TestGeometry {


public static void main(String args[]){

Geometry geoObj = new Geometry("1000 2000","poLi");
System.out.println(geoObj.getType());

geoObj = new Geometry("3000 4000","strP");
System.out.println(geoObj.getType());

geoObj = new Geometry(new Double(5000), new Double(6000));
System.out.println(geoObj.getType());

geoObj = new Geometry("7000 8000","Pgn");
System.out.println(geoObj.getType());


}

}

Open in new window

0
 
AssetFXAuthor Commented:
Thanks Krakatoa, brilliant advice!
0
 
krakatoaCommented:
Sure. However, there's probably a better way of doing it based on better OO principles. Not sure.

Course, in an ArrayList, you can always obtain the class of the object with instanceof when you need to know what it is, instead of this rather convoluted approach . . . for which I'm sure you've got your own good reasons.
0
 
krakatoaCommented:
BTW, this :

public Polyline(String lineCoordsXY) {
            String[] pointCoordsXYArray = lineCoordsXY.split(", ");
            for(String pointCoordsXY : pointCoordsXYArray){
                this.polylineList.add(new Point(pointCoordsXY));
            }

Open in new window



looks like it is going to throw an Exception, as you are asking the String to be split again.
0
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

All Courses

From novice to tech pro — start learning today.