Метод currentSegment(float[]) в типе PathIterator не применим для аргументов (Double[])

Я пытаюсь прочитать Сегменты Path2D.Double в Java-программе, и Eclips постоянно говорит мне:

Метод currentSegment(float[]) в типе PathIterator не применим для аргументов (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);

помечается красным, а подсказка говорит:

Метод currentSegment(float[]) в типе PathIterator не применим для аргументов (Double[])

Есть метод с плавающей точкой [] и другой с двойным [], но с тем же именем. Что мне здесь не хватает?

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();
Другие вопросы по тегам