array casting


DevX Home    Today's Headlines   Articles Archive   Tip Bank   Forums   

Results 1 to 4 of 4

Thread: array casting

  1. #1
    Join Date
    Jul 2004
    Posts
    10

    array casting

    Hi, guys,
    I'm having trouble casting an int array into a double array. This is how I did it:
    Code:
    int[] a = {1, 2, 3, 4, 5};
    double[] b = (double[])a;
    And it gave out this:
    Code:
    test.java:7: inconvertible types
    found   : int[]
    required: double[]
                    double[] b = (double[])a;
                                           ^
    1 error
    Can anyone shed some light on this? Thanks.

  2. #2
    Join Date
    May 2004
    Posts
    219
    You'd have to use a loop. You can't cast arrays like that.

  3. #3
    Join Date
    Jul 2004
    Posts
    10
    That's what I thought. Thanks.

  4. #4
    Join Date
    Feb 2004
    Posts
    808
    you only use this to convert an array of a parent type to an array of child type..

    suppose you stored a String[] into a Vector. when you get it out again, its a Object.. so you cast back to String[]:

    String[] ss = (String[])(myVector.elementAt(2));

    this is called type conversion..

    by attempting int[] to double[] youre trying to perform data conversion, which must be done on an each-array-element level
    The 6th edict:
    "A thing of reference thing can hold either a null thing or a thing to any thing whose thing is assignment compatible with the thing of the thing" - ArchAngel, www.dictionary.com et al.
    JAR tutorial GridBag tutorial Inherited Shapes Inheritance? String.split(); FTP?

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  
HTML5 Development Center
 
 
FAQ
Latest Articles
Java
.NET
XML
Database
Enterprise
Questions? Contact us.
C++
Web Development
Wireless
Latest Tips
Open Source


   Development Centers

   -- Android Development Center
   -- Cloud Development Project Center
   -- HTML5 Development Center
   -- Windows Mobile Development Center