0
How can I add an item at the end of the array? The most easy way (arr [5]=8) returns "out of range", and I suppose it's because first I instantiate it as "int [] arr=new int[5]", so it's out of the range. Can I amplify that range?
3 Respostas
0
Are you trying to increase the length of the array or are you trying to change the last value of the array?
0
why r u posting java related question in a python community
0
however, the array indexing starts from 0, so when u define ' int[] arr = new int[5] ', its making an array of size 5, so when u write ' arr[5] =8 ' its trying to access the 6th position of the array which is not valid as its size is bound to 5.
In java you can't change the size of an array once created. u either have to allocate bigger size or U can make use of ArrayList which is dynamic in nature.