Home All Groups Group Topic Archive Search About

Question: arraylist and item

Author
25 Jul 2006 6:46 PM
dotnetnoob
i have a arraylist that look like this

item 0 - hold this value 1,2,4
item 1 - hold this value 3,6,7

if i want to access the item 0 i get the whole 1,2,4 how do i seperate them
out like i want "1" how do i access item 0 but the first value "1".

thank

Author
25 Jul 2006 6:56 PM
iwdu15
so you have a variable you want to hold like 1,5,7? try a multi-demensional
arry


Private arrNumbers(,) As Integer = {{5, 6, 6}, _
                                            {8, 9, 10}}

hope that helps
--
-iwdu15
Author
25 Jul 2006 9:16 PM
dotnetnoob
it's not exactly what i'm looking for but i got it. basically, i'm going to
treate it as string and get the character out of the string in the arraylist

Show quoteHide quote
"iwdu15" wrote:

> so you have a variable you want to hold like 1,5,7? try a multi-demensional
> arry
>    
>
> Private arrNumbers(,) As Integer = {{5, 6, 6}, _
>                                             {8, 9, 10}}
>
> hope that helps
> --
> -iwdu15
Author
25 Jul 2006 7:22 PM
Tom Shelton
dotnetnoob wrote:
> i have a arraylist that look like this
>
> item 0 - hold this value 1,2,4
> item 1 - hold this value 3,6,7
>
> if i want to access the item 0 i get the whole 1,2,4 how do i seperate them
> out like i want "1" how do i access item 0 but the first value "1".
>
> thank

Well...  The only way really to do that is to store this as either an
arraylist of arraylists, or an arraylist of arrays.  I would suggest
the latter if the individual elements are fixed length.  Then you could
do something like (Assuming 2003 - if your useing 2005, then you could
improve the following using generics):

Option Strict On
Option Explicit On

Imports System
Imports System.Collections

Module Module1

    Sub Main()
        Dim list As New ArrayList

        list.Add(New Integer() {1, 2, 3})
        list.Add(New Integer() {5, 6, 7})
        list.Add(New Integer() {7, 15, 25})
        list.Add(New Integer() {8, 27, 10})

        Console.WriteLine(DirectCast(list(3), Integer())(1))

    End Sub

End Module

The above prints 27.

--
Tom Shelton [MVP]
Author
28 Jul 2006 5:59 PM
Travis Sharpe
dotnetnoob wrote:
> i have a arraylist that look like this
>
> item 0 - hold this value 1,2,4
> item 1 - hold this value 3,6,7
>
> if i want to access the item 0 i get the whole 1,2,4 how do i seperate them
> out like i want "1" how do i access item 0 but the first value "1".
>
> thank

You could do this several ways.  One option is to create a structure and
use that for your items:

--------------------------------
Public Structure DataItem
    Dim value1 as Integer
    Dim value2 as Integer
    Dim value3 as Integer
End Structure
--------------------------------

For each item that you have, you could create a new DataItem and assign
the values appropriately.  Using the values from your example above:

--------------------------------
Dim newItem as new DataItem
newItem.value1 = 1
newItem.value2 = 2
newItem.value3 = 4
--------------------------------

After you've created DataItems for all of the data, you could put them
into an array of type DataItem like so:

--------------------------------------------------
Dim DataItems as DataItem() = {var1,var2,var3,...}
--------------------------------------------------

or add them to an ArrayList.  If you use an arraylist, you'll most
likely have to cast the object to type DataItem to access the
..value1,.value2,.value3 items.

A structure is a simple form, you could always create a class to hold
the data, and instantiate the values in a constructor.

Hope this Helps