?
Solved

Defining an ArrayList of objects in Java

Posted on 2012-09-11
9
Medium Priority
?
534 Views
Last Modified: 2012-09-12
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!
0
Comment
Question by:AssetFX
  • 6
  • 3
9 Comments
 
LVL 17

Expert Comment

by:krakatoa
ID: 38388288
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
 

Author Comment

by:AssetFX
ID: 38389702
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
 
LVL 17

Expert Comment

by:krakatoa
ID: 38389961
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
Independent Software Vendors: We Want Your Opinion

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 

Author Comment

by:AssetFX
ID: 38390046
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
 
LVL 17

Accepted Solution

by:
krakatoa earned 2000 total points
ID: 38390370
You'd have to overload the Geometry constructor, and add a String parameter identifying which type you want to instantiate.
0
 
LVL 17

Expert Comment

by:krakatoa
ID: 38390492
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
 

Author Closing Comment

by:AssetFX
ID: 38390493
Thanks Krakatoa, brilliant advice!
0
 
LVL 17

Expert Comment

by:krakatoa
ID: 38390526
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
 
LVL 17

Expert Comment

by:krakatoa
ID: 38390600
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

Featured Post

Free Tool: Path Explorer

An intuitive utility to help find the CSS path to UI elements on a webpage. These paths are used frequently in a variety of front-end development and QA automation tasks.

One of a set of tools we're offering as a way of saying thank you for being a part of the community.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Java had always been an easily readable and understandable language.  Some relatively recent changes in the language seem to be changing this pretty fast, and anyone that had not seen any Java code for the last 5 years will possibly have issues unde…
In this post we will learn different types of Android Layout and some basics of an Android App.
Viewers will learn about the different types of variables in Java and how to declare them. Decide the type of variable desired: Put the keyword corresponding to the type of variable in front of the variable name: Use the equal sign to assign a v…
Viewers will learn about the regular for loop in Java and how to use it. Definition: Break the for loop down into 3 parts: Syntax when using for loops: Example using a for loop:
Suggested Courses

864 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question