Possible Lossy conversion from long to int

后端 未结 6 709
借酒劲吻你
借酒劲吻你 2021-01-01 02:15

I wish to enter one int and another long ex: 1 and 1000000000, and now I wish to create an array of size 1000000000. And then at each index of arra

6条回答
  •  遥遥无期
    2021-01-01 02:48

    I think you have some misconception about typecasting here. In Java down casting is allowed as already mentioned you can down cast it to integer

    arr = new long[(int)(y+1)];
    

    But here the main problem is that array allocation should takes integer value so that the same number of homogeneous space can be allocated. If you want more space than an integer range then you should use ArrayList which is dynamically grow-able array because if we give that much liability to a user to declare a large amount array memory then our system may run out of the memory as array is a static memory allocation data structure. The same stands true for

    arr[j]=4;
    

    It should be:

    arr[(int)j]=4;
    

提交回复
热议问题