我是第一次使用LinkedHashMap。
我的 LinkedHashMap 被定义为包含一个键 String 和一个类值。
我定义了以下类:
// --- Geometric Element Class ---------
class GeoElement
{
String elemType = "Generic"; // Element type
}
// --- Point Element Class ---------
class PointElement extends GeoElement
{
final String elemType = "Point";
String pointName = ""; // Point name
double pointCoord[] = {0.0, 0.0, 0.0}; // Point coordinates
public PointElement(String pName, double px, double py, double pz) // Overloaded Constructor
{
this.pointName = pName;
this.pointCoord[0] = px;
this.pointCoord[1] = py;
this.pointCoord[2] = pz;
}
}
// --- Line Element Class ---------
class LineElement extends GeoElement
{
final String elemType = "Line";
String lineName = ""; // Line name
String startPoint = ""; // Start-Point or Anchor-Point
String endPoint = ""; // End-Point used only for Two-Point lines
String angle = ""; // Angle degrees
String oldLineName = ""; // Base-Line name
String startPointMethod = ""; // Start-Point method
String startParam = ""; // Start-Parameter
String endPointMethod = ""; // end-Point method
String endParam = ""; // End-Parameter
String groupName = ""; // Group name
// --------------------------------------------- Overloaded Constructor --------------------------------------------------------------------------------------------------
LineElement(String lName, String strPoint, String endPoint, String ang, String bLine,
String strPtMet, String strPar, String endPtMet, String endPar, String gName )
{
this.lineName = lName;
this.startPoint = strPoint;
this.endPoint = endPoint;
this.angle = ang;
this.oldLineName = bLine;
this.startPointMethod = strPtMet;
this.startParam = strPar;
this.endPointMethod = endPtMet;
this.endParam = endPar;
this.groupName = gName;
}
}
我的LinkedHashMap定义如下:
public static LinkedHashMap<String, GeoElement> geoElemMap = new LinkedHashMap<String, GeoElement>(); // LinkedHashMap for data transfer to/from GUI and Cliff's code
添加到 geoElemMap 的项目可以是
public void linkedHashMapTest (LinkedHashMap<String, MyNewGUI.GeoElement> linkedHashMap)
{
System.out.print("\n =================================================== \n ");
for (Entry<String, MyNewGUI.GeoElement> entry : linkedHashMap.entrySet())
{
System.out.println("Key: " + entry.getKey()
+ ", Value: "
+ entry.getValue().toString());
}
System.out.print(" =================================================== \n ");
// Create List that store values
List<GeoElement> list = new ArrayList< GeoElement>(linkedHashMap.values());
// display List
System.out.println("List - " + list);
}
不幸的是我无法展开geoElemMap值部分的内容。下面是 geoElemMap 打印输出的示例:
列表 - [MyNewGUI$PointElement@5ce1e0d4、MyNewGUI$PointElement@59dd4912、MyNewGUI$PointElement@2bd30507、MyNewGUI$PointElement@76979715、MyNewGUI$PointElement@25a33e53]*
我想提取并打印出在 geoElemMap 的值部分中编码的几何元素属性
您应该重写从 GeoElement 派生的每个类中的
toString()
方法。PointElement
:
@Override
public String toString()
{
return (elemType + " " + pointName + ....); // fill the dots with a string representation of the coordinates.
}