473,326 Members | 2,133 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,326 software developers and data experts.

Array conversion

dmjpro
2,476 2GB
Expand|Select|Wrap|Line Numbers
  1. List<String> list = new ...;
  2. String arr[] = (String[])list.toArray(); //error why?
  3.  
Expand|Select|Wrap|Line Numbers
  1. Object arr1[] = new String[1];
  2. String arr2[] = (String[])arra1; //correct
  3.  
Can you explain me please?
Aug 11 '09 #1
9 2831
r035198x
13,262 8TB
What error message did you get for the first case?
Aug 11 '09 #2
dmjpro
2,476 2GB
Hey how does it happen?
When i complied the program again then it shown no error :(

Earlier it shown me that, can't cast Object[] to String[], exactly i can't remember but the message was something like this ..
Aug 11 '09 #3
r035198x
13,262 8TB
Well then you and your compiler need to make up your minds as to whether that code produces an error or not. Nothing we can do about this thread till then.
Aug 11 '09 #4
dmjpro
2,476 2GB
@r035198x
<Snipped>
My brain gone somewhere .... actually that was .. runtime error.
I think now i m right...you can guess what error message i got .. ;)
Aug 11 '09 #5
dmjpro
2,476 2GB
Then what's the cause ?
Please explain ..!!
Aug 11 '09 #6
JosAH
11,448 Expert 8TB
@dmjpro
Don't act so desparate because your example has no practical use whatsoever. It is just an 'edge case' for the typing system. Arrays aren't covariant, i.e. if a D extends a B then a D 'is a' B; but an array of Ds is not an array of Bs. Your List example is even easier: the no-arg List.toArray() method returns an Object[]. You should be able to figure out the rest of it.

kind regards,

Jos
Aug 11 '09 #7
dmjpro
2,476 2GB
Sorry for rude behavior .
but in my second object i am casting Object[](which is originally String[]) to String[], same thing happens in my first code snippet then how do they behave differently ?
Aug 12 '09 #8
JosAH
11,448 Expert 8TB
Show us the examples you have tried to compile and run with the correct comments, because the comments don't make sense in your original code snippets. Don't just compile your examples but try to run them as well. Also reread my previous reply. Remember that List.toArray() (no arguments) returns an Object[] and that a D[] is not a B[] where D extends B.

kind regards,

Jos
Aug 13 '09 #9
dmjpro
2,476 2GB
Well

This code snippet
Expand|Select|Wrap|Line Numbers
  1. List<String> list = new Vector<String>();
  2. String arr[] = (String [])list.toArray();
  3.  
leads to java.lang.ClassCastException: [Ljava.lang.Object; cannot be cast to [Ljava.lang.String.

And this code snippet
Expand|Select|Wrap|Line Numbers
  1. Object arr1[] = new String[2];
  2. String arr2[] = (String[])arr1;
  3.  
runs correct.

Now let me explain what i tried get while i was posting the reply...;)
List.toArray function something like ..
Expand|Select|Wrap|Line Numbers
  1. {
  2.  int size = list.size();
  3.  Object arr = new Object[size];
  4.  for(int i=0;i<size;i++) arr[i] = list.get(i);
  5.  return arr;
  6. }
  7.  
In second code snippet, during runtime the arra1 is String[], so it runs... but in first code snippet though array contains String at runtime, still it's Object[]; so it results to error. Array conversation not looks into the content but the type ..that's what the explanation ;)

Thanks!
Aug 13 '09 #10

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

Similar topics

58
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...
8
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...
11
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
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 =...
9
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...
20
by: subramanian | last post by:
Hello I have a doubt in the following piece of code: int a; printf("a=%p\n", a); printf("&a=%p\n", &a); these printf statements print the same value for both 'a' and '&a". I tried in...
0
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:...
2
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:...
6
by: Lighter | last post by:
How to read "The lvalue-to-rvalue, array-to-pointer, and function-to- pointer standard conversionsare not applied to the left expressions"? In 5.18 Comma operator of the C++ standard, there is a...
152
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 = { {...
0
by: DolphinDB | last post by:
Tired of spending countless mintues downsampling your data? Look no further! In this article, you’ll learn how to efficiently downsample 6.48 billion high-frequency records to 61 million...
0
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, we are pleased to welcome back...
0
by: Vimpel783 | last post by:
Hello! Guys, I found this code on the Internet, but I need to modify it a little. It works well, the problem is this: Data is sent from only one cell, in this case B5, but it is necessary that data...
0
by: jfyes | last post by:
As a hardware engineer, after seeing that CEIWEI recently released a new tool for Modbus RTU Over TCP/UDP filtering and monitoring, I actively went to its official website to take a look. It turned...
1
by: PapaRatzi | last post by:
Hello, I am teaching myself MS Access forms design and Visual Basic. I've created a table to capture a list of Top 30 singles and forms to capture new entries. The final step is a form (unbound)...
1
by: CloudSolutions | last post by:
Introduction: For many beginners and individual users, requiring a credit card and email registration may pose a barrier when starting to use cloud servers. However, some cloud server providers now...
1
by: Defcon1945 | last post by:
I'm trying to learn Python using Pycharm but import shutil doesn't work
1
by: Shællîpôpï 09 | last post by:
If u are using a keypad phone, how do u turn on JavaScript, to access features like WhatsApp, Facebook, Instagram....
0
by: Faith0G | last post by:
I am starting a new it consulting business and it's been a while since I setup a new website. Is wordpress still the best web based software for hosting a 5 page website? The webpages will be...

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.