Changing/editing value in arraylist

ljpv14

Active member
Joined
Nov 28, 2011
Messages
44
Programming Experience
3-5
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?
 
Are the properties of array is the same with arraylist? Honestly, I can't really understand or find in the link that you gave me the things I needed. Maybe I just can't comprehend it properly.
 
Replacing item in arraylist at specified index

I'm not sure if this is right or best practice but it does work. All I've done is removed an item in the arraylist at specified index and then added a new one in at that same index.

VB.NET:
[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]

 
Skilleddreamer said:
Are the properties of array is the same with arraylist?
No, the link was supposed to be ArrayList.IndexOf Method (Object)
You'll find arrays and various collection types have much the same members though. One difference is that Array class has most methods shared, and collections usually have those as instance methods.
Skilleddreamer said:
want to change "ball" at index 0 to be "cat".
When you have the index you can set the new value with Item property:
help said:
Gets or sets the element at the specified index.
 
I made this work.


I just need to point out the index where I want to have changes. For example, I have a arraylist words then at word.item(2) it contains "ball"

It works with this simple code.

word.item(2) = cstr("dog")

And now, word.item(2) contains "dog'. :D Thanks for the help though! :D
 
Back
Top