473,542 Members | 2,701 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

array data type conversion

10 New Member
I have a float[] array, which I need to convert to a Double[] array. The task does not seem as trivial as I expected it to be. Can anyone give me some guidance?
Aug 24 '09 #1
2 5135
JosAH
11,448 Recognized Expert MVP
@ElderGeek
You have to do it element by element and store the new elements in your new array. e.g.

Expand|Select|Wrap|Line Numbers
  1. Double[] convert(float[] f) {
  2.  
  3.    Double[] d= new Double[f.length];
  4.    for (int i= 0; i < f.length; i++)
  5.       d[i]= new Double(f); // convert any way you like
  6.    return d;
  7. }
  8.  
Arrays of type D can't be cast to an array of type B, not even when type D extends type B (a D 'is a' B). Here is a small example that shows why you can't do it:

Expand|Select|Wrap|Line Numbers
  1. public class Animal { ... }
  2. public class Poodle extends Animal { ... }
  3. public class Lion extends Animal { ... }
  4.  
  5. Poodle[] poodles = { /* cute little poodles in here */ };
  6. Animal[] animals= (Animal[])poodles; // this is not allowed because:
  7. animals[0]= new Lion(); // the poodles won't survive ;-)
  8.  
kind regards,

Jos
Aug 25 '09 #2
ElderGeek
10 New Member
Nice and concise -- thanks!
Aug 25 '09 #3

Sign in to post your reply or Sign up for a free account.

Similar topics

58
10056
by: jr | last post by:
Sorry for this very dumb question, but I've clearly got a long way to go! Can someone please help me pass an array into a function. Here's a starting point. void TheMainFunc() { // Body of code... TCHAR myArray; DoStuff(myArray);
8
2344
by: chessc4c6 | last post by:
The program below creates a char pointer call charPtr...... i then declare an char array string "Good Luck" When i assign charPtr = string, I expect an error. However, It actually runs and outputs: G Good Luck
38
5153
by: VK | last post by:
Hello, In my object I have getDirectory() method which returns 2-dimentional array (or an imitation of 2-dimentional array using two JavaScript objects with auto-handled length property - please let's us do not go into an "each dot over i" clarification discussion now - however you want to call - you call it ;-) array contains records of...
11
2094
by: x-pander | last post by:
given the code: <file: c.c> typedef int quad_t; void w0(int *r, const quad_t *p) { *r = (*p); }
204
12899
by: Alexei A. Frounze | last post by:
Hi all, I have a question regarding the gcc behavior (gcc version 3.3.4). On the following test program it emits a warning: #include <stdio.h> int aInt2 = {0,1,2,4,9,16}; int aInt3 = {0,1,2,4,9};
9
2365
by: Codemonkey | last post by:
Hi, Sorry for a stupid question, but is it possible to do a narrowing conversion with an object array with Option Strict On in VB? E.g: ------------------ Dim aBase as Base() = {New Derived(), New Derived(), New Derived()}
10
1618
by: Christian Christmann | last post by:
Hi, I'm wondering if my small example is not "dangerous": #define SIZE 10 char global; char* globalPtr = global; int main()
0
2715
by: k1ckthem1dget | last post by:
I need to display the unsorted list of names and display the sorted list of names. My program is getting a bunch of errors though, and i dont know why. I am getting the following errors. 28: error: cannot convert `char (*)' to `int*' for argument `1' to `void showArray(int*, int)' 33: error: expected unqualified-id before "for" 33:...
2
3235
by: k1ckthem1dget | last post by:
I need to display the unsorted list of names and display the sorted list of names. My program is getting a bunch of errors though, and i dont know why. I am getting the following errors. 28: error: cannot convert `char (*)' to `int*' for argument `1' to `void showArray(int*, int)' 33: error: expected unqualified-id before "for" 33:...
152
9743
by: vippstar | last post by:
The subject might be misleading. Regardless, is this code valid: #include <stdio.h> void f(double *p, size_t size) { while(size--) printf("%f\n", *p++); } int main(void) { double array = { { 3.14 }, { 42.6 } }; f((double *)array, sizeof array / sizeof **array); return 0;
0
7395
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main...
0
7723
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that...
0
7673
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the...
1
5250
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes...
0
3382
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in...
0
3383
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
1803
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system
1
946
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
0
624
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.