title: Why I cannot type cast with (Point2d.Double)? - Java
body: <p>I have below code snippet - Which <strong>doesn't work</strong>.</p>
  
  <pre><code>private void startAddingTrackPointValue()
      {
          Set&lt;Point2D.Double&gt; keySet = this.trackPointList.keySet();
          Point2D.Double[] keys = (Point2D.Double[]) keySet.toArray();
          for(int i = 0; i &lt; keys.length; i++)
          {
              System.out.println(keys[i]);
          }
      }
  </code></pre>
  
  <p>Which generates <strong>exception is</strong>, This...</p>
  
  <pre><code>Exception in thread "main" java.lang.ClassCastException: [Ljava.lang.Object; cannot be cast to [Ljava.awt.geom.Point2D$Double;
      at SegmentFactory.startAddingTrackPointValue(SegmentFactory.java:170)
      at SegmentFactory.&lt;init&gt;(SegmentFactory.java:44)
      at TestClass.main(TestClass.java:8)
  </code></pre>
  
  <p>But this <strong>works fine</strong>, If I do it like this,</p>
  
  <pre><code>private void startAddingTrackPointValue()
      {
          Set&lt;Point2D.Double&gt; keySet = this.trackPointList.keySet();
          Object[] keys = (Object[]) keySet.toArray();
          for(int i = 0; i &lt; keys.length; i++)
          {
              System.out.println(keys[i]);
          }
      }
  </code></pre>
  
  <p>Question is, <strong>why can't I typecast with Point2D.Double[]?</strong></p>