How can I change an element of array that is defined in arraylist in java? | Sololearn: Learn to code for FREE!
New course! Every coder should learn Generative AI!
Try a free lesson
+ 1

How can I change an element of array that is defined in arraylist in java?

I want to change an element of an array that is defined in arraylist. Because I want to add an element whose index 0 to my array with an algorithm, but I don't know how I can. Please help me. Thank you.

8th Jul 2017, 9:44 PM
Yusuf
Yusuf - avatar
3 Answers
+ 2
I don't really understand. You have an array and array list. You want to move something from the arraylist to the array?¿ If that is what you want, it's the same as you'd normally modify an array value. myArray[index] = myList.get(index); // index for ^array. index for ^list. myArray[0] = myList.get(5); // index at 0 is now the value of the lists index at 5. If that wasn't what you wanted, can you give an example?
8th Jul 2017, 9:50 PM
Rrestoring faith
Rrestoring faith - avatar
+ 2
Oh. You can actually do the exact same code you just did with the array in java. That syntax looks the same to me. Just need that semi-colon on that first line in the loop.
9th Jul 2017, 12:19 PM
Rrestoring faith
Rrestoring faith - avatar
+ 1
I will write my algorithm with C: int array[]={1,3,5,7,8}; int index=3, fIndex=5, item=9; int j=4; //What I want to do is: fIndex++; while(j>=fIndex) { array[j+1]=array[j] j--; } array[index]=item; //How can I do this in Java?
9th Jul 2017, 9:18 AM
Yusuf
Yusuf - avatar