?
Solved

How to overwrte an arraylist location

Posted on 2009-04-02
14
Medium Priority
?
196 Views
Last Modified: 2012-05-06
How can I overwrite an arraylist location?

Example
4000
0
4001

I need to overwrite the value 0 which in in my arraylist with the value 4000

0
Comment
Question by:cmdolcet
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 7
  • 5
  • 2
14 Comments
 
LVL 48

Expert Comment

by:jpaulino
ID: 24053667
Can you simply remove it ?
        Dim arr As New ArrayList
        arr.Add(4000)
        arr.Add(0)
        arr.Add(4001)
 
        ' Removes the 0
        arr.Remove(0)

Open in new window

0
 

Author Comment

by:cmdolcet
ID: 24053873
but I don't want to remove it i want to update it with the previous arraylist value
0
 
LVL 86

Expert Comment

by:Mike Tomlinson
ID: 24053903
Just access it by Index:

    arr(1) = 4000 ' replace the second element with 4000
0
Industry Leaders: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 
LVL 48

Expert Comment

by:jpaulino
ID: 24053978
Then follow the Idle_Mind suggestion ... I thought you wanted to delete the wrong entry.
If you don't know the index of the 0 (zero) you can use:

arr(arr.IndexOf(0)) = New Value
0
 

Author Comment

by:cmdolcet
ID: 24054275
I keep getting a break error after the second loop through
I have _Arraylist_1Inch which has 24 spot in the arraylist. Every 2 index position I have written a 0 integer value what this loop should do is loop through the       _Arraylist_1Inch       and replace the 0 value with the goodpre_intarrayindex value

However its breaking


    For Each intarrayindex In _Arraylist_1Inch
                    If intarrayindex = 0 Then
                        _Arraylist_1Inch(intarrayindex) = pre_intarrayindex
                    Else
                        pre_intarrayindex = 0
                        pre_intarrayindex = intarrayindex
                    End If
                Next
0
 
LVL 48

Expert Comment

by:jpaulino
ID: 24054548

intarrayindex gives you the value in the array and you're trying to use as the index of the  array  _Arraylist_1In in here
  _Arraylist_1Inch(intarrayindex)
I don't know what you want to replace with but you can do something like this (and change the 123 with your value)

        Dim pos As Integer
        While True
            pos = _Arraylist_1Inch.IndexOf(0)
            If pos = -1 Then Exit While
            _Arraylist_1Inch(pos) = 123
        End While

Open in new window

0
 
LVL 86

Expert Comment

by:Mike Tomlinson
ID: 24054582
I think you want?

        For intarrayindex As Integer = 0 To _Arraylist_1Inch.Count - 1
            If _Arraylist_1Inch(intarrayindex) = 0 Then
                _Arraylist_1Inch(intarrayindex) = pre_intarrayindex
            End If
        Next
0
 
LVL 48

Expert Comment

by:jpaulino
ID: 24054676
Nice solution Idle_Mind, the only problem is that loops in all the arraylist.
But, could be not a problem :)
0
 
LVL 86

Accepted Solution

by:
Mike Tomlinson earned 2000 total points
ID: 24054910
Yeah...it's not very clear how it's supposed to work...

If you LITERALLY want to visit "Every 2 index position" then use the Step keyword on the end of the For loop:

        For intarrayindex As Integer = 0 To _Arraylist_1Inch.Count - 1 Step 2
            If _Arraylist_1Inch(intarrayindex) = 0 Then
                _Arraylist_1Inch(intarrayindex) = pre_intarrayindex
            End If
        Next

You would just need to start at 0 or 1 depending on which slot was the first one you needed.
0
 
LVL 86

Expert Comment

by:Mike Tomlinson
ID: 24055059
@jpaulino: With regard to this part of your comment:

    "...the only problem is that loops in all the arraylist."

I do hope you realize that your code is ALSO visiting all slots in the ArrayList.  Internally, the IndexOf() method is doing exactly what I have done: loop thru iteratively, inorder, until it finds a match.

In fact, your algorithm is potentially ALOT worse because EACH call to IndexOf() is starting from the BEGINNING of the ArrayList and looking for a match.  So you will be iterating over some nodes multiple times!  In fact, if the whole thing was full of zeroes then you would be doing multiple passes with a total of (1 + 2 + 3 + 4 + ... + n) checks instead of a just 1 pass with n checks.   =\

You would need to use the IndexOf() overload that accepts a "starting position" to overcome this:

        Dim pos As Integer, startIndex = 0
        While True
            pos = _Arraylist_1Inch.IndexOf(0, startIndex)
            If pos = -1 Then Exit While
            _Arraylist_1Inch(pos) = 123
            startIndex = pos + 1
        End While

~IM   =)
0
 
LVL 48

Expert Comment

by:jpaulino
ID: 24055526
Strange I have made a few tests and the time from your method and my (including you change) is very close. Sometimes my runs fasts, sometimes your code runs faster.
But that the startIndex it's needed or else it will increase ALLOT the cycle time
Loved you sign ~IM :)
0
 
LVL 48

Expert Comment

by:jpaulino
ID: 24055534
Typo ... Should be:

Loved your sign ~IM :)
0
 
LVL 86

Expert Comment

by:Mike Tomlinson
ID: 24055604
Lol...and of course, if the size of the list is small, then the milliseconds saved doesn't really matter!   =O


0
 
LVL 48

Expert Comment

by:jpaulino
ID: 24055717
I have tested with 20000 records!
0

Featured Post

Free Tool: Port Scanner

Check which ports are open to the outside world. Helps make sure that your firewall rules are working as intended.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

This tutorial demonstrates one way to create an application that runs without any Forms but still has a GUI presence via an Icon in the System Tray. The magic lies in Inheriting from the ApplicationContext Class and passing that to Application.Ru…
I think the Typed DataTable and Typed DataSet are very good options when working with data, but I don't like auto-generated code. First, I create an Abstract Class for my DataTables Common Code.  This class Inherits from DataTable. Also, it can …
Do you want to know how to make a graph with Microsoft Access? First, create a query with the data for the chart. Then make a blank form and add a chart control. This video also shows how to change what data is displayed on the graph as well as form…
In this video you will find out how to export Office 365 mailboxes using the built in eDiscovery tool. Bear in mind that although this method might be useful in some cases, using PST files as Office 365 backup is troublesome in a long run (more on t…
Suggested Courses

762 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question