dongshijiao6890 2016-06-18 18:15 采纳率: 0%
浏览 82

Java - 交换两种不同类型的多维数组

Is it possible for me to swap one character type multidimensional array with another integer type array? This problem was asked to me during a PHP developer interview. Since in PHP there are no data types just $ variables so I think it's possible. I wanna know if it is possible in Java.

int A[][]=new int[4][4];
char B[][]=new char[4][4];
A=|1234
  |5678
  |9 10 11 12
  |13 14 15 16 
B=|ABCD
  |EFGH
  |IJKL
  |MNOP

result

A=|ABCD
  |EFGH
  |IJKL
  |MNOP

B=|1234
  |5678
  |9 10 11 12
  |13 14 15 16
  • 写回答

2条回答 默认 最新

  • duangong1979 2016-06-18 18:31
    关注

    No, this is not literally possible. An integer array is an array of integers and can by definition not contain chars. You will never be able to store 'A' as an int, because it isn't.

    On the other hand, it is possible to cast integers to chars and the other way around. You'll just get the number representing the char internally when you cast a char to an int and you'll get the char that is represented by the int when you cast an into to a char.

    Together with casting any element you read from one of the arrays back to the type you want it to have, this could work as a hack to do what you're trying to do. I don't see why you would want it'though.

    评论

报告相同问题?

悬赏问题

  • ¥15 ETLCloud 处理json多层级问题
  • ¥15 matlab中使用gurobi时报错
  • ¥15 这个主板怎么能扩出一两个sata口
  • ¥15 不是,这到底错哪儿了😭
  • ¥15 2020长安杯与连接网探
  • ¥15 关于#matlab#的问题:在模糊控制器中选出线路信息,在simulink中根据线路信息生成速度时间目标曲线(初速度为20m/s,15秒后减为0的速度时间图像)我想问线路信息是什么
  • ¥15 banner广告展示设置多少时间不怎么会消耗用户价值
  • ¥15 可见光定位matlab仿真
  • ¥15 arduino 四自由度机械臂
  • ¥15 wordpress 产品图片 GIF 没法显示