i want to campare 2 arraylist and show the values ..
arraylist1 = {1,2,3,4,5,6,7,8,9}
arraylist2 = {2,4,5}
i have compare and the value to listview .. like this
1 Not available
2 available
3 Not available
4 available
5 available
6 Not available
i write the program like this but looping many times ..
For position As Integer = 0 To arraylist1.Count - 1
Dim words As String() = arraylist2(position).Split(New Char() {" "c})
arr(1) = words(3)
For i = 0 To arr.Length - 1
If arraylist1(i).Contains(arr(1)) Then
arr(0) = i
arr(2) = "working"
itm = New ListViewItem(arr)
lv1.Items.Add(itm)
Else
arr1(0) = i
arr1(1) = arrproc(i)
arr1(2) = "NOT working"
itm = New ListViewItem(arr1)
lv1.Items.Add(itm)
End If
Next
Next
as Tim was suggesting use List(Of Integer)
kind of like this
Dim list1 As New List(Of Integer) From {1, 2, 3, 4, 5, 6, 7, 8, 9}
Dim list2 As New List(Of Integer) From {2, 4, 5}
For Each i As Integer In list1
If list2.Contains(i) Then
Console.WriteLine(i & " available")
Else
Console.WriteLine(i & " Not available")
End If
Next
also in your code within the first loop, you use the index of arraylist1 in arraylist2 to get the words this will fail if both lists don't have the same amount of items and will run into an out of bounds exception
EDIT
So there are a few more problems to solve i guess
you loop over the arraylist1 to retrieve items from arraylist2
you use the forth word of that items and assign it to a random array (arr())
then you loop over that random array and compare the second item in
that array with all item from the arraylist1 at the indices of that
random array
so my suggestion is to review your code again
this might work but still some questions to answer
For Each item_list1 As String In arraylist2
Dim words As String() = item_list1.Split(New Char() {" "c})
If arraylist1.contains(words(3)) Then
itm = New ListViewItem(arraylist1.IndexOf(words(3)))
itm.SubItems.Add(words(3))
itm.SubItems.Add("working")
Else
itm = New ListViewItem(arraylist1.IndexOf(words(3)))
itm.SubItems.Add(arrproc(arraylist1.IndexOf(words(3))))
itm.SubItems.Add("NOT working")
End If
lv1.items.add(itm)
Next
You can use such linq query to compare those lists and return the appearance status of items of first list in seccond list:
Dim list1 = {1, 2, 3, 4, 5, 6, 7, 8, 9}
Dim list2 = {2, 4, 5}
Dim data = list1.Select(Function(item)
Return New With
{
.Value = item,
.Status = String.Format("{0} {1}", item, IIf(list2.Contains(item), "Available", "Not Available"))
}
End Function).ToList()
Then you can simply add them to ListView this way:
For Each item In data
Me.ListView1.Items.Add(item.Value.ToString()).SubItems.Add(item.Status)
Next
Full example
Sub Main()
Dim list1 As New List(Of Integer)() From {1, 2, 3, 4, 5, 6, 7, 8, 9}
Dim list2 As New List(Of Integer)() From {2, 4, 5}
Dim rows = From i In list1
Group Join j In list2
On j Equals i Into g = Group
From j In g.DefaultIfEmpty()
Select i, j
For Each r In rows
Console.WriteLine("{0} {1}", r.i, If(r.i = r.j, "exist", "not exist"))
Next
Console.ReadLine()
End Sub
Related
How to Combine or Joining nearest items are Similar for example
Dim _lstOfFontsList As New List(Of String)
From {"Arial", "Arial", "Arial", "GEORGIA", "Arial",
"Arial", "GEORGIA", "GEORGIA",
"ABOlD", "ABOlD", "Arial"}
_lstOfFontsList has 11 values itself, here 0,1,2 items are similar and 5,6 and 7,8 and 9,10 items are similar here i want to group the similar items so tried to groupby funtion like below
Dim Cntdd = _lstOfFontsList.GroupBy(Function(S) S).ToDictionary
(Function(a) a.Key, Function(a) a.Count)
but output is like below
[Arial, 6]
[GEORGIA, 3]
[ABOlD, 2]
but expected output is
Arial = 3
Georgia =1
Arial=2
Georgia=2
Abold=2
Arial=1
how to combine or group or join nearest (Next) similar items in list of string
Update :
i am trying to Loop condition for getting above values but the expected result won't coming here is the code
Dim _fntCnt% = 0
Dim _finalItms As New List(Of String)
Dim _finalFntName$ = ""
Dim LASTITEMS As New List(Of String)
For i = 0 To _lstOfFontsList.Count - 1
If i = 0 Then
_fntCnt += 1
_finalItms.Add(_lstOfFontsList(i) & "->" & _fntCnt)
_finalFntName$ = _lstOfFontsList(i)
End If
Trace.WriteLine(i)
If i <> _lstOfFontsList.Count - 1 Then
If _lstOfFontsList(i) = _lstOfFontsList(i + 1) Then
_fntCnt += 1
_finalItms.Add(_lstOfFontsList(i) & "->" & _fntCnt)
Else
For Each _itmm In _finalItms
LASTITEMS.Add(_itmm & " " & _finalFntName.Count)
Next
_finalItms.Clear()
End If
End If
Next
please can anybody help
I'm sure there must be simpler solutions, but this shows the working behind the idea too. As JM said you need a loop and a check to see if the item has more to come. You can't use a dictionary in this case as you need unique values, so we must use a list (of pair).
Hope this makes sense and helps!
Private Sub Button1_Click(sender As Object, e As EventArgs) Handles Button1.Click
Dim _lstOfFontsList As New List(Of String) _
From {"Arial", "Arial", "Arial", "GEORGIA", "Arial",
"Arial", "GEORGIA", "GEORGIA",
"ABOlD", "ABOlD", "Arial"}
Dim Output As List(Of KeyValuePair(Of String, Integer)) = New List(Of KeyValuePair(Of String, Integer))
Dim Count As Integer = 1
For i = 0 To _lstOfFontsList.Count - 1
Dim Itm As String = _lstOfFontsList(i)
'Erro trap as the last i will error as there is no next item at the end of the list
Dim Nxt_Itm As String = Nothing
If i + 1 < _lstOfFontsList.Count Then Nxt_Itm = _lstOfFontsList(i + 1)
If Itm = Nxt_Itm Then
'Same as next - so we +1 to the count
Count += 1
Else
'Not same, so we need to add to the collection and reset the count to 1
Output.Add(New KeyValuePair(Of String, Integer)(Itm, Count))
Count = 1
End If
Next
For Each pair In Output
Debug.WriteLine(pair.Key & " | " & pair.Value)
'Will write "Arial | 3" for example...
Next
End Sub
Hth
Chicken
I have the following code, the objective of it is to take an initial list and to take each element within the list and store it in an array of lists, with each list in the array, holding each element in its own list. For instance
The list 2, 2, 3, 3, 3, 3, 5, 5, 7, 9, 9. Would create five lists:
List 1: 2, 2
List 2: 3, 3, 3, 3,
List 3: 5, 5
list 4: 7
List 5: 9, 9
This is my current code:-
Dim cnt As Integer = 0
Dim lists(uniqueFactors.Count) As List(Of Integer)
Dim saver As Integer = factors.Item(0)
Console.WriteLine(saver)
For Each i In factors
lists(cnt).Add(i)
If saver <> i Then
cnt = cnt + 1
End If
saver = i
Next
Thanks all in advance!
You'd probably be better off using a Dictonary<TKey, TValue>.
Dim storage As New Dictionary(Of Integer, Integer)
' Number Occurrences
' | |
storage.Add(2, 2)
storage.Add(3, 4)
storage.Add(5, 2)
storage.Add(7, 1)
storage.Add(9, 2)
You can iterate the list like this:
For Each item As KeyValuePair(Of Integer, Integer) In storage
Dim number As Integer = item.Key
Dim occurrences As Integer = item.Value
Next
Get the number of occurrences for a given number like this:
Dim number As Integer = 9
Dim occurrences As Integer = storage(number) 'Return: 2
Change the number of occurrences:
storage.Item(number) = 4 'Set to 4
storage.Item(number) += 1 'Increase from 2 to 3
storage.Item(number) -= 1 'Decrease from 2 to 1
Create an enumerator, array and/or list of occurrences for a given number:
Dim iterator As IEnumerable(Of Integer) = Enumerable.Repeat(number, occurrences).ToList()
Dim array As Integer() = Enumerable.Repeat(number, occurrences).ToArray()
Dim list As List(Of Integer) = Enumerable.Repeat(number, occurrences).ToList()
You can also write an extension method:
Public Module Extensions
<System.Runtime.CompilerServices.Extension()>
Public Function ToList(Of TKey)(source As Dictionary(Of TKey, Integer), key As TKey) As List(Of TKey)
Return Enumerable.Repeat(key, source.Item(key)).ToList()
End Function
End Module
Now you create a list by simply writing:
Dim occurrences As List(Of Integer) = storage.ToList(number)
Why not use a List of Lists like this?
Dim last As Integer
Dim first As Boolean = True
Dim ints As List(Of Integer)
Dim lists As New List(Of List(Of Integer))
For Each i In factors
If first Then
first = False
ints = New List(Of Integer)
ints.Add(i)
lists.Add(ints)
last = i
ElseIf i = last Then
ints.Add(i)
Else
ints = New List(Of Integer)
ints.Add(i)
lists.Add(ints)
last = i
End If
Next
How can I detect whether the items of two given lists are equal?
Dim list1 As New List(Of Integer)
list1.AddRange({1, 2, 3})
Dim list2 As New List(Of Integer)
list2.AddRange({3, 2, 1})
If I compare them using SequenceEqual I get False because the order of the items is not the same. How can I compare them without sorting them first, though?
EDIT: Please take into account that this should respect duplicates, for example {1, 2, 3, 1} is not the same as {1, 2, 3} (item 1 occurs two times in the first list).
If you want to know if both lists contain the same items you can use Enumerable.Except:
Dim bothContainSameItems As Boolean
If list1.Count > list2.Count Then
bothContainSameItems = Not list1.Except(list2).Any()
Else
bothContainSameItems = Not list2.Except(list1).Any()
End If
or, with the help of HashSet(Of T):
Dim l1Set = New HashSet(Of Integer)(list1)
Dim l2Set = New HashSet(Of Integer)(list2)
bothContainSameItems = l1Set.SetEquals(l2Set)
Note that both approaches will ignore duplicates. So they will return equal for:
list1.AddRange({1, 1, 2, 3})
list2.AddRange({3, 2, 1, 3})
Here's a possible way to also check if all numbers have the same count in both lists:
bothContainSameItems = list1.Count = list2.Count
If bothContainSameItems Then
Dim l1Ordered = list1.OrderBy(Function(i) i).ToList()
Dim l2Ordered = list2.OrderBy(Function(i) i).ToList()
For i As Int32 = 0 To l1Ordered.Count - 1
If l1Ordered(i) <> l2Ordered(i) Then
bothContainSameItems = False
Exit For
End If
Next
End If
Also working with
Dim list1 As New List(Of Integer)
list1.AddRange({1, 2, 3})
Dim list2 As New List(Of Integer)
list2.AddRange({3, 2, 1})
Dim list3 = list1.Union(list2)
if list3.OrderBy(Function(i) i).SequenceEqual(list1.OrderBy(Function(i) i)) then
Console.WriteLine("Equal")
else
Console.WriteLine("Not Equal")
end if
IEnumerable.Union
Return Value: An IEnumerable(Of T) that contains the elements from both input sequences,
excluding duplicates.
<System.Runtime.CompilerServices.Extension()> _
Function AreItemsEqual(Of T)(col1 As IEnumerable(Of T), col2 As IEnumerable(Of T)) As Boolean
' performance checks
If col1 Is col2 Then Return True
If col1 Is Nothing OrElse col2 Is Nothing Then Return False
If col1.Count <> col2.Count Then Return False
' compare their elements
Dim o1 As IEnumerable(Of T) = col1.OrderBy(Function(i) i)
Dim o2 As IEnumerable(Of T) = col2.OrderBy(Function(i) i)
Return o1.SequenceEqual(o2)
End Function
Usage:
If list1.AreItemsEqual(list2) Then
...
This question is about this topic:
Vb.net all combinations
Question:
I use this code for my application but i've got a problem.
The chance exists that i have much items which has to be combinated.
But i just want to show the first 10 combinations/results.
What i want is the text to be completely unique.
so the example at my topic you see in the beginning of this question there's an ape cow deer ... example.
It doesn't matter here.
but if i got something like this:
1|2|3|4|5
6|7|8|9
3|2|1
0|9|8|7|6|5
( sometimes it's even bigger )
the first 10 results are:
1-6-3-0
1-6-3-9
1-6-3-8
1-6-3-7
1-6-3-6
1-6-3-5
1-6-2-0
1-6-2-9
1-6-2-8
1-6-2-7
but they are almost the same.
i want the first 10 results to be something like this then:
1-8-1-6
3-6-1-5
4-8-3-0
etc...
Is this possible??
Here is my solution that I converted from c# using http://www.developerfusion.com/tools/convert/csharp-to-vb/:
Dim numbers = New Integer()() { _
New Integer() {1, 2, 3, 4, 5}, _
New Integer() {6, 7, 8, 9}, _
New Integer() {3, 2, 1}, _
New Integer() {0, 9, 8, 7, 6, 5} _
}
Dim random = New Random()
Dim codes = New HashSet(Of String)()
Dim newCode As String
For resultNr As Integer = 0 To 9
' Try to generate random codes until a non exisiting one is found.
Do
Dim sb = New StringBuilder()
For i As Integer = 0 To 3
Dim r As Integer = random.[Next](numbers(i).Length)
sb.Append(numbers(i)(r)).Append("-")
Next
sb.Length -= 1
newCode = sb.ToString()
Loop While codes.Contains(newCode)
codes.Add(newCode)
Console.WriteLine(newCode)
Next
Console.ReadKey()
If I have the following array:
Dim Array(4, 10) As String
Array(0, 0) = "100"
Array(0, 1) = "200"
Array(1, 0) = "300"
Array(1, 1) = "400"
Array(1, 2) = "500"
Array(1, 3) = "600"
How do I get the following count:
0 = 2
1 = 4
It sounds like you're trying to count the number of non-Nothing values in each dimension of the array. The following function will allow you to do that
Public Function CountNonNothing(ByVal data As String(,), ByVal index As Integer) As Integer
Dim count = 0
For j = 0 To data.GetLength(1) - 1
If data(index, j) IsNot Nothing Then
count += 1
End If
Next
Return count
End Function
And it can be invoked like so
Dim count1 = CountNonNothing(Array, 0)
Dim count2 = CountNonNothing(Array, 1)
Note: I used a C# to VB converter so hopefully the VB syntax is correct.
I made a simple extension method that makes this pretty easy:
Public NotInheritable Class Extensions
Private Sub New()
End Sub
<System.Runtime.CompilerServices.Extension> _
Public Shared Function GetNonNullItems(Of T)(array As T(,), index As Integer) As IEnumerable(Of T)
For i As Integer = 0 To array.GetLength(index) - 1
If array(index, i) IsNot Nothing Then
yield Return array(index, i)
End If
Next
End Function
End Class
Then to use it:
Dim Array As String(,) = New String(4, 10) {}
Array(0, 0) = "100"
Array(0, 1) = "200"
Array(1, 0) = "300"
Array(1, 1) = "400"
Array(1, 2) = "500"
Array(1, 3) = "600"
Dim countArray0 As Integer = Array.GetNonNullItems(0).Count()
Dim countArray1 As Integer = Array.GetNonNullItems(1).Count()
The extension method will give you back all non null items found for a given index. From that you can get the count, filter, query, or use them however you want.
Converted from c# but it could be something like this.
Dim count As Integer() = New Integer(Array.GetLength(0) - 1) {}
For i As Integer = 0 To Array.GetLength(0) - 1
For j As Integer = 0 To Array.GetLength(1) - 1
If Array(i, j) IsNot Nothing Then
count(i) += 1
End If
Next
Next
Now count of 0's would be in count(0), count of 1's would be in count(1), so on...