转换INT []数组在Java中短期[]数组 [英] Convert int[] array to short[] array in Java

查看:110
本文介绍了转换INT []数组在Java中短期[]数组的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有,我有100个元素分配空间中的 INT 阵列。还有另外一个数组 inShort [] 。我怎么能转换 inInt [] inShort []

是否有必要为分配新的内存inShort [] 或没有通过,我可以转换为 inInt []

  INT inInt [] =新INT [100];
短inShort [];


解决方案

 短inShort [] =新的短[100];的for(int i = 0; I< 100;我++)
{
    inShort [I] =(短)inInt [I]
}

但是,你很可能会漫短,如果int是短期的范围之外(即超过32,767小于-32,768或更大)。它实际上很容易溢出短,因为一个int范围从-2,147,483,648到2,147,483,647。

I have an int array for which i have allocated space for 100 elements. There is another array inShort[]. How can I convert inInt[] to inShort[]?

Is it necessary to allocate new memory for inShort[] or there is a way through which I can cast to inInt[]?

int inInt[] = new int[100];
short inShort[];

解决方案

short inShort[] = new short[100];

for(int i = 0; i < 100; i++)
{
    inShort[i] = (short)inInt[i];
}

However, you are very likely to overflow the short if the int is outside the range of the short (i.e. less than -32,768 or greater than 32,767). It's actually really easy to overflow the short, since an int ranges from -2,147,483,648 to 2,147,483,647.

这篇关于转换INT []数组在Java中短期[]数组的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆