0

I currently have a char array

char [] firstArray = {'1', 'a', 'c', '4', 'q'};

I want to implicitly self-cast this array to a int one. I mean not to create a new int array and to try to assign him the index's values of this one, but to change this array to an int array.

Sotirios Delimanolis
  • 274,122
  • 60
  • 696
  • 724
Stavi94
  • 1
  • 1
  • 1
    You can not. That's not a valid conversion. Please explain what you are trying to do. – Sotirios Delimanolis Oct 23 '15 at 21:35
  • Let's say you can do it. What will `a` turn into? `97`? `0`? `-1`? – Emz Oct 23 '15 at 21:38
  • You can convert an array of A into an array of B if B is a subclass of A ([like this](http://stackoverflow.com/questions/1018750/how-to-convert-object-array-to-string-array-in-java)), but `int` is not a subclass of `char`. – azurefrog Oct 23 '15 at 21:40
  • I'm trying to use the cesaer chiper code. As far as I've been told a char array should be read from the console and after that a shift (for example +5) should been typed. This should transform my array (first in an int array) and after that using the shift variable the same array with new values should be displayed. – Stavi94 Oct 23 '15 at 21:41
  • What is a "cesaer chiper"? – azurefrog Oct 23 '15 at 21:44
  • transforming is not casting. transforming usually means you create new object from (or based on) an existing one – Nir Levy Oct 23 '15 at 21:45
  • 1
    If you are doing a caesar cipher, you'd want to use just `char[]` or `String`. I can't see why you want to use an `int[]`. – Paul Boddington Oct 23 '15 at 21:45

2 Answers2

0

You can't change the declared type of an object with casting. Casting will allow you to treat an object as another type, not to change it's type.

let's say I have this code:

String s = '';
Object o;

I can do this:

o = (Object)s; 

what it will do - o will have a reference to s and will treat it as an Object. however, s is still a String, nothing we can do to change that.

Nir Levy
  • 12,750
  • 3
  • 21
  • 38
0

You can't change a char array to an int array. The only way you can achieve this is to create a new int array, iterate through the old firstArray and store the int value of the chars into the new array.

sparkhee93
  • 1,381
  • 3
  • 21
  • 30