Question: arraylist and item

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • dotnetnoob

    #1

    Question: arraylist and item

    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
  • iwdu15

    #2
    RE: Question: arraylist and item

    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

    Comment

    • Tom Shelton

      #3
      Re: Question: arraylist and item


      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.Collecti ons

      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.WriteLi ne(DirectCast(l ist(3), Integer())(1))

      End Sub

      End Module

      The above prints 27.

      --
      Tom Shelton [MVP]

      Comment

      • dotnetnoob

        #4
        RE: Question: arraylist and item

        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

        "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

        Comment

        • Travis Sharpe

          #5
          Re: Question: arraylist and item

          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,.value 2,.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

          Comment

          Working...