Is there a way to edit a certain item in an arraylist? For example, I have a arraylist consisting of 2 strings "ball" at index 0 and "dog" at index 1. I want to change "ball" at index 0 to be "cat". Is there a way or other helpful advises?
[SIZE=2][COLOR=#0000ff][SIZE=2][COLOR=#0000ff]Dim[/COLOR][/SIZE][/COLOR][/SIZE][SIZE=2] i [/SIZE][SIZE=2][COLOR=#0000ff][SIZE=2][COLOR=#0000ff]As[/COLOR][/SIZE][/COLOR][/SIZE][SIZE=2][COLOR=#0000ff][SIZE=2][COLOR=#0000ff]Integer[/COLOR][/SIZE][/COLOR][/SIZE]
[SIZE=2]
[/SIZE][SIZE=2][COLOR=#008000][SIZE=2][COLOR=#008000]'Instantiate new instance of arraylist[/COLOR][/SIZE][/COLOR][/SIZE]
[SIZE=2]
[/SIZE][SIZE=2][COLOR=#0000ff][SIZE=2][COLOR=#0000ff]Dim[/COLOR][/SIZE][/COLOR][/SIZE][SIZE=2] myAL [/SIZE][SIZE=2][COLOR=#0000ff][SIZE=2][COLOR=#0000ff]As[/COLOR][/SIZE][/COLOR][/SIZE][SIZE=2][COLOR=#0000ff][SIZE=2][COLOR=#0000ff]New[/COLOR][/SIZE][/COLOR][/SIZE][SIZE=2] Collections.ArrayList[/SIZE]
[SIZE=2]
[/SIZE][SIZE=2][COLOR=#008000][SIZE=2][COLOR=#008000]'Add two items to arraylist[/COLOR][/SIZE][/COLOR][/SIZE]
[SIZE=2] myAL.Add([/SIZE][SIZE=2][COLOR=#a31515][SIZE=2][COLOR=#a31515]"Ball"[/COLOR][/SIZE][/COLOR][/SIZE][SIZE=2])[/SIZE]
[SIZE=2] myAL.Add([/SIZE][SIZE=2][COLOR=#a31515][SIZE=2][COLOR=#a31515]"Dog"[/COLOR][/SIZE][/COLOR][/SIZE][SIZE=2])[/SIZE]
[SIZE=2]
[/SIZE][SIZE=2][COLOR=#008000][SIZE=2][COLOR=#008000]'Find the position of ball which in this example is 0 and assign it to an variable so we can remember the index "Ball was at[/COLOR][/SIZE][/COLOR][/SIZE]
[SIZE=2] i = myAL.IndexOf([/SIZE][SIZE=2][COLOR=#a31515][SIZE=2][COLOR=#a31515]"Ball"[/COLOR][/SIZE][/COLOR][/SIZE][SIZE=2])[/SIZE]
[SIZE=2]
[/SIZE][SIZE=2][COLOR=#008000][SIZE=2][COLOR=#008000]'Writing the values to test that "Ball" is as at index 0[/COLOR][/SIZE][/COLOR][/SIZE]
[SIZE=2] Console.WriteLine([/SIZE][SIZE=2][COLOR=#a31515][SIZE=2][COLOR=#a31515]"{0} was at {1}"[/COLOR][/SIZE][/COLOR][/SIZE][SIZE=2], myAL(0), i)[/SIZE]
[SIZE=2]
[/SIZE][SIZE=2][COLOR=#008000][SIZE=2][COLOR=#008000]'Remove the item in the array at the indexof "Ball"[/COLOR][/SIZE][/COLOR][/SIZE]
[SIZE=2] myAL.RemoveAt(myAL.IndexOf([/SIZE][SIZE=2][COLOR=#a31515][SIZE=2][COLOR=#a31515]"Ball"[/COLOR][/SIZE][/COLOR][/SIZE][SIZE=2]))[/SIZE]
[SIZE=2]
[/SIZE][SIZE=2][COLOR=#008000][SIZE=2][COLOR=#008000]' Insert the new value at the same position Ball was at[/COLOR][/SIZE][/COLOR][/SIZE]
[SIZE=2] myAL.Insert(i, [/SIZE][SIZE=2][COLOR=#a31515][SIZE=2][COLOR=#a31515]"Cat"[/COLOR][/SIZE][/COLOR][/SIZE][SIZE=2])[/SIZE]
[SIZE=2]
[/SIZE][SIZE=2][COLOR=#008000][SIZE=2][COLOR=#008000]'Test again[/COLOR][/SIZE][/COLOR][/SIZE]
[SIZE=2] Console.WriteLine([/SIZE][SIZE=2][COLOR=#a31515][SIZE=2][COLOR=#a31515]"{0} is now at {1}"[/COLOR][/SIZE][/COLOR][/SIZE][SIZE=2], myAL(0), i)[/SIZE]
[SIZE=2] Console.ReadKey()
[/SIZE]
No, the link was supposed to be ArrayList.IndexOf Method (Object)Skilleddreamer said:Are the properties of array is the same with arraylist?
When you have the index you can set the new value with Item property:Skilleddreamer said:want to change "ball" at index 0 to be "cat".
help said:Gets or sets the element at the specified index.