Solved

Arraylist data duplication problem,...................sorta!

Posted on 2013-12-18
7
231 Views
Last Modified: 2013-12-18
I have the following arraylist that may or may not contain duplicate data.

array1        
(0)   "A"                
(1)     3                  
(2)   "G"                  
(3)     2                                                    
(4)   "Z"
(5)     2
(6)   "A"
(7)     2


The letters and integers go together.  I want to produce an Arraylist that keeps the distinct letters and adds the integers together when the letters match.  It should produce the following arraylist:

array1        
(0)   "A"                
(1)     5                  
(2)   "G"                  
(3)     2                                                    
(4)   "Z"
(5)     2

As you can see array1(7) and array1(1) was added together because array1(0) and array1(6) matched.  I don't care if it alters the original arraylist or creates a new one.
0
Comment
Question by:NevSoFly
[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
  • 4
  • 3
7 Comments
 
LVL 75

Expert Comment

by:käµfm³d 👽
ID: 39727613
Is it guaranteed that you will always have letter-number in the ArrayList slots (i.e. an even number of items)? Using ArrayList in this manner (and at all, really) is a very fragile design.
0
 

Author Comment

by:NevSoFly
ID: 39727633
It is guaranteed that the arraylist will start off with a letter and that every other slot will be a letter.  The slots in-between the letters will always be integers and the arraylist will end with an integer.

If you have a better way of doing this I'm all ears.
0
 
LVL 75

Expert Comment

by:käµfm³d 👽
ID: 39727743
Myself, I would create a class to hold the correlations:

Public Class ItemPair
    Public Property Letter As Char
    Public Property Number As Integer
End Class

Open in new window


Then, for type safety, I would use a generic List rather than an ArrayList:

Dim list1 As New List(Of ItemPair)()
Dim list2 As New List(Of ItemPair)()

Dim pair As New ItemPair

pair.Letter = "A"c
pair.Number = 3
list1.Add(pair)
...

pair.Letter = "A"c
pair.Number = 5
list2.Add(pair)
...

Open in new window


Now you don't have to a letter being paired up with another letter, or a number being paired up with another number. You can enforce rules within your class (via methods and properties) so that you always have a letter and a number. Plus, there is more information about the data that is being held by the object. If you had to take a guess, what would you think was in pair.Letter if you had never seen it before? Can you say the same thing about array1(0)?

Now, for your original question, would you be permitted to use LINQ?
0
Independent Software Vendors: 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!

 

Author Comment

by:NevSoFly
ID: 39727937
I like your class based solution and I will likely end up using a class or structure.  This will require me to do a good bit of rewriting, so in the mean time just to get this working I am able to use LINQ.
0
 
LVL 75

Accepted Solution

by:
käµfm³d   👽 earned 500 total points
ID: 39727986
Ah, I misled you slightly. My LINQ question revolved around the class-based solution. Let me show you that first, and then I'll show you what you can do with the ArrayList.

For what I propose above, you could do this:

Dim query = From item1 In list1
            Group item1 By item1.Letter Into Group
            Select New ItemPair With {
                .Letter = Letter,
                .Number = Group.Sum(Function(p) p.Number)
            }

Dim newList As List(Of ItemPair) = query.ToList()

Open in new window



Now, for your ArrayList it's a bit more involved:

Dim mapped As New Dictionary(Of String, Integer)

For i As Integer = 0 To list1.Count - 1 Step 2
    If Not mapped.ContainsKey(list1(i)) Then mapped.Add(list1(i), 0)

    mapped(list1(i)) += list1(i + 1)
Next

Dim newList As New ArrayList()

For Each key In mapped.Keys
    newList.Add(key)
    newList.Add(mapped(key))
Next

Open in new window

0
 

Author Comment

by:NevSoFly
ID: 39728079
list1 is my original arraylist of letters and integers?
0
 

Author Closing Comment

by:NevSoFly
ID: 39728183
great solution, great suggestions
0

Featured Post

On Demand Webinar - Networking for the Cloud Era

This webinar discusses:
-Common barriers companies experience when moving to the cloud
-How SD-WAN changes the way we look at networks
-Best practices customers should employ moving forward with cloud migration
-What happens behind the scenes of SteelConnect’s one-click button

Question has a verified solution.

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

This article explains how to create and use a custom WaterMark textbox class.  The custom WaterMark textbox class allows you to set the WaterMark Background Color and WaterMark text at design time.   IMAGE OF WATERMARKS STEPS Create VB …
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 …
In a recent question (https://www.experts-exchange.com/questions/29004105/Run-AutoHotkey-script-directly-from-Notepad.html) here at Experts Exchange, a member asked how to run an AutoHotkey script (.AHK) directly from Notepad++ (aka NPP). This video…
How to Install VMware Tools in Red Hat Enterprise Linux 6.4 (RHEL 6.4) Step-by-Step Tutorial

737 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