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? | Sololearn: Learn to code for FREE!
New course! Every coder should learn Generative AI!
Try a free lesson
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?

19th Mar 2016, 4:38 PM
Rubén Aguado Cosano
Rubén Aguado Cosano - avatar
3 Answers
0
Are you trying to increase the length of the array or are you trying to change the last value of the array?
24th Jun 2016, 12:34 AM
Scylla
Scylla - avatar
0
why r u posting java related question in a python community
13th Jul 2016, 12:28 PM
Subhankar Paul
Subhankar Paul - avatar
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.
13th Jul 2016, 12:42 PM
Subhankar Paul
Subhankar Paul - avatar