【问题标题】:The method currentSegment(float[]) in the type PathIterator is not applicable for the arguments (Double[])PathIterator 类型中的方法 currentSegment(float[]) 不适用于参数 (Double[])
【发布时间】:2017-05-10 14:50:01
【问题描述】:

我正在尝试在 java 程序中读取 Path2D.Double 的段,而 Eclips 不断告诉我:

PathIterator 类型中的 currentSegment(float[]) 方法不适用于参数 (Double[])

但是在 PathIterator 接口中有 2 种方法似乎让 Eclipse(或我)感到困惑

/**
 * Returns the coordinates and type of the current path segment in
 * the iteration.
 * The return value is the path-segment type:
 * SEG_MOVETO, SEG_LINETO, SEG_QUADTO, SEG_CUBICTO, or SEG_CLOSE.
 * A float array of length 6 must be passed in and can be used to
 * store the coordinates of the point(s).
 * Each point is stored as a pair of float x,y coordinates.
 * SEG_MOVETO and SEG_LINETO types returns one point,
 * SEG_QUADTO returns two points,
 * SEG_CUBICTO returns 3 points
 * and SEG_CLOSE does not return any points.
 * @param coords an array that holds the data returned from
 * this method
 * @return the path-segment type of the current path segment.
 * @see #SEG_MOVETO
 * @see #SEG_LINETO
 * @see #SEG_QUADTO
 * @see #SEG_CUBICTO
 * @see #SEG_CLOSE
 */
public int currentSegment(float[] coords);

/**
 * Returns the coordinates and type of the current path segment in
 * the iteration.
 * The return value is the path-segment type:
 * SEG_MOVETO, SEG_LINETO, SEG_QUADTO, SEG_CUBICTO, or SEG_CLOSE.
 * A double array of length 6 must be passed in and can be used to
 * store the coordinates of the point(s).
 * Each point is stored as a pair of double x,y coordinates.
 * SEG_MOVETO and SEG_LINETO types returns one point,
 * SEG_QUADTO returns two points,
 * SEG_CUBICTO returns 3 points
 * and SEG_CLOSE does not return any points.
 * @param coords an array that holds the data returned from
 * this method
 * @return the path-segment type of the current path segment.
 * @see #SEG_MOVETO
 * @see #SEG_LINETO
 * @see #SEG_QUADTO
 * @see #SEG_CUBICTO
 * @see #SEG_CLOSE
 */
public int currentSegment(double[] coords);

我自己的代码如下:

PathIterator it= ((Path2D.Double) p3d).getPathIterator(null, 2d);
  Double x, y;
  while (!it.isDone()) {
     Double coords[]= new Double[6];
     System.out.println("1");
     int art= it.currentSegment(coords);
     System.out.println("2");
     switch (art) {
     case PathIterator.SEG_MOVETO:
        movetoXY(coords[0], coords[1]);
        x= coords[0];
        y= coords[1];
        break;
     case PathIterator.SEG_LINETO:
        linetoXY(coords[0], coords[1]);
        break;
     case PathIterator.SEG_CLOSE:
        linetoXY(x, y);
        break;
     default:
        System.out.println("unbekanntes Segment " + art);
        break;
     }
     it.next();
  }

一行

     int art= it.currentSegment(coords);

标记为红色,工具提示显示:

PathIterator 类型中的方法 currentSegment(float[]) 不适用于参数 (Double[])

有一个带有 float[] 的方法和另一个带有 double[] 但名称相同的方法 我在这里想念什么?

【问题讨论】:

  • double != Double ....
  • 对不起,阅读我自己的 QAuestion 另一个时间做了。 Double[] 和 double[] 之间存在差异。所以正确的行必须是:double coords[]= new double[6];

标签: java eclipse path-iterator


【解决方案1】:

Double[] 和 double[] 不是同一个类型,所以你需要转换这些类型...

Java8 Streams 可以以一种非常清晰的语法方式做到这一点:

只有一件重要的事情:Double 是一个对象,所以 Double[] 可以保存一个空值……你需要检查一下!

Double[] d = { 1.0, 2.0, 3.0, 4.0, 5.0, null };
double[] dArray = Arrays.stream(d).mapToDouble(x -> x == null ? 0.0 : x.doubleValue()).toArray();
System.out.println(Arrays.toString(dArray));

如果你 100% 确定你的 Double[] 没有空值,那么就这样做:

double[] dArray = Arrays.stream(d).mapToDouble(Double::doubleValue).toArray();

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2014-01-09
    • 2014-06-26
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多