code: public static double length3D(CoordinateSequence points) {
          // optimized for processing CoordinateSequences
  
          int numberOfCoords = points.size();
          // Points have no length.
          if (numberOfCoords < 2) {
              return 0.0;
          }
  
          Coordinate currentCoord = new Coordinate();
          points.getCoordinate(0, currentCoord);
          double x0 = currentCoord.x;
          double y0 = currentCoord.y;
          double z0 = currentCoord.z;
  
          double length = 0.0;
          for (int i = 1; i < numberOfCoords; i++) {
              points.getCoordinate(i, currentCoord);
  
              double x1 = currentCoord.x;
              double y1 = currentCoord.y;
              double z1 = currentCoord.z;
  
              double dx = x1 - x0;
              double dy = y1 - y0;
              double dz;
  
              // For 2D geometries, we want to return the 2D length.
              if (Double.isNaN(z0) || Double.isNaN(z1)) {
                  dz = 0.0;
              } else {
                  dz = z1 - z0;
              }
  
              length += Math.sqrt(dx * dx + dy * dy + dz * dz);
              x0 = x1;
              y0 = y1;
              z0 = z1;
          }
          return length;
      }
docs: Computes the length of a LineString specified by a sequence of
  coordinates.
  
  @param points The coordinate sequence
  @return The length of the corresponding LineString