Results 1 to 18 of 18

Thread: [RESOLVED] Jagged Array Dimension

  1. #1

    Thread Starter
    Frenzied Member oceanebelle's Avatar
    Join Date
    Jun 2005
    Location
    my n00k.
    Posts
    1,064

    Resolved [RESOLVED] Jagged Array Dimension

    How do I differentiate between
    single, multiple, and jagged type arrays?

    Right now I only have the rank function to rely to, but that only works for multidimensional arrays.

    I have this function that will print the values of an array.
    The arrays passed may either be single, multiple or jagged.

    I'd like the function intelligent enough to print jagged arrays as well.

    Help will be appreciated.

  2. #2

    Thread Starter
    Frenzied Member oceanebelle's Avatar
    Join Date
    Jun 2005
    Location
    my n00k.
    Posts
    1,064

    Re: Jagged Array Dimension

    how do I do this without using foreach?

  3. #3
    Super Moderator jmcilhinney's Avatar
    Join Date
    May 2005
    Location
    Sydney, Australia
    Posts
    111,221

    Re: Jagged Array Dimension

    A jagged array is a single dimensional array where each element is a single dimensional array. If you want to visit every element of a jagged array it would be something like:
    VB Code:
    1. Dim outerArr As String()()
    2.  
    3. '...
    4.  
    5. For Each innerArr As String() In outerArr
    6.     For each str As String In innerArr
    7.         MessageBox.Show(str)
    8.     Next str
    9. Next innerArr
    or:
    VB Code:
    1. Dim outerArr As String()()
    2.  
    3. '...
    4.  
    5. Dim innerArr As String()
    6. Dim str As String
    7.  
    8. For i As Integer = 0 To outerArr.GetUpperBound(0) Step 1
    9.     innerArr = outerArr(i)
    10.  
    11.     For j As Integer = 0 To innerArr.GetUpperBound(0) Step 1
    12.         str = innerArr(j)
    13.  
    14.         MessageBox.Show(str)
    15.     Next j
    16. Next i
    Why is my data not saved to my database? | MSDN Data Walkthroughs
    VBForums Database Development FAQ
    My CodeBank Submissions: VB | C#
    My Blog: Data Among Multiple Forms (3 parts)
    Beginner Tutorials: VB | C# | SQL

  4. #4
    Super Moderator jmcilhinney's Avatar
    Join Date
    May 2005
    Location
    Sydney, Australia
    Posts
    111,221

    Re: Jagged Array Dimension

    Ah b*gger! As soon as I posted I remembered that this is the C# forum. If you need it translated post back but you should be able to get the idea from that.
    Why is my data not saved to my database? | MSDN Data Walkthroughs
    VBForums Database Development FAQ
    My CodeBank Submissions: VB | C#
    My Blog: Data Among Multiple Forms (3 parts)
    Beginner Tutorials: VB | C# | SQL

  5. #5

    Thread Starter
    Frenzied Member oceanebelle's Avatar
    Join Date
    Jun 2005
    Location
    my n00k.
    Posts
    1,064

    Re: Jagged Array Dimension

    Yeah. LOLz. naah I can understand it. I know Visual Basic

    Was just discovering C#.

    Yeah i figured that'd be the way..

    but is there a way to know that the elements in single array (jagged) are of type array?

  6. #6
    I'm about to be a PowerPoster!
    Join Date
    Jan 2005
    Location
    Everywhere
    Posts
    13,647

    Re: Jagged Array Dimension

    If its typed as string[][] then each element can only be of type string[].

  7. #7

    Thread Starter
    Frenzied Member oceanebelle's Avatar
    Join Date
    Jun 2005
    Location
    my n00k.
    Posts
    1,064

    Re: Jagged Array Dimension

    Quote Originally Posted by penagate
    If its typed as string[][] then each element can only be of type string[].

    Code:
            private void displayElements(Array x) {
                
                Console.WriteLine("{1} - {0}", x.Rank, "data");
    
                if(x.GetType().Equals(Type.GetType("int[]"))) {
                    foreach(int data in x){
                        Console.WriteLine(data);
                    }
                }
                if (x.GetType().Equals(Type.GetType("string[]")))
                {
                    foreach (string data in x)
                    {
                        Console.WriteLine(data);
                    }
                }           
            }
    this won't work...
    maybe i should overload this function for each type... there is no basetype in C#?

    sorry for being a n00b, i just decided to study them this morning.
    and i want to have a common data printer for all sizes and types of arrays...

  8. #8
    I'm about to be a PowerPoster!
    Join Date
    Jan 2005
    Location
    Everywhere
    Posts
    13,647

    Re: Jagged Array Dimension

    Oh. Ah. Heh. I see.

    Base type is object...

    Are you looking for something like PHP's var_dump or print_r?

  9. #9

    Thread Starter
    Frenzied Member oceanebelle's Avatar
    Join Date
    Jun 2005
    Location
    my n00k.
    Posts
    1,064

    Re: Jagged Array Dimension

    Quote Originally Posted by penagate
    Oh. Ah. Heh. I see.

    Base type is object...

    Are you looking for something like PHP's var_dump or print_r?

    yeah is there a function like that?
    or is it toString()?

    and i printed the type that get's passed...

    this were the results

    Code:
    System.Int32[]
    System.String[,]
    System.Int32[][]
    :S

  10. #10

    Thread Starter
    Frenzied Member oceanebelle's Avatar
    Join Date
    Jun 2005
    Location
    my n00k.
    Posts
    1,064

    Re: Jagged Array Dimension

    Quote Originally Posted by penagate
    Oh. Ah. Heh. I see.

    Base type is object...

    Are you looking for something like PHP's var_dump or print_r?
    including the int and string types?



    Code:
    private void displayElements(Array x) {
                
                Console.WriteLine("{1} - {0}", x.Rank, x.GetType());
                
                foreach(object data in x){
                    Console.WriteLine(data);
                }
                
            }
    printed the elements.. but for the jagged arrays it printed the type instead.

  11. #11
    I'm about to be a PowerPoster!
    Join Date
    Jan 2005
    Location
    Everywhere
    Posts
    13,647

    Re: Jagged Array Dimension

    Hold on, I'll see what I can do.

  12. #12
    I'm about to be a PowerPoster!
    Join Date
    Jan 2005
    Location
    Everywhere
    Posts
    13,647

    Re: Jagged Array Dimension

    Are you using 1.1 or 2.0?

  13. #13

    Thread Starter
    Frenzied Member oceanebelle's Avatar
    Join Date
    Jun 2005
    Location
    my n00k.
    Posts
    1,064

    Re: Jagged Array Dimension

    Quote Originally Posted by penagate
    Are you using 1.1 or 2.0?

    Right now i've only got 2.0

  14. #14
    type Woss is new Grumpy; wossname's Avatar
    Join Date
    Aug 2002
    Location
    #!/bin/bash
    Posts
    5,682

    Re: Jagged Array Dimension

    Code:
    if( outerarray[0] is string[])
    //yeah its a string array
    else
    //nope, its not.
    I don't live here any more.

  15. #15

    Thread Starter
    Frenzied Member oceanebelle's Avatar
    Join Date
    Jun 2005
    Location
    my n00k.
    Posts
    1,064

    Re: Jagged Array Dimension

    ok i'll try that wossy.

  16. #16

    Thread Starter
    Frenzied Member oceanebelle's Avatar
    Join Date
    Jun 2005
    Location
    my n00k.
    Posts
    1,064

    Re: Jagged Array Dimension

    Code:
     private void displayElements(Array jagged)
            {
                IEnumerator enumerator = jagged.GetEnumerator();
                while (enumerator.MoveNext())
                {
    
                    if (enumerator.Current is Array)
                    {
                        displayElements(enumerator.Current as Array);
                    } else {
                        Console.Write("{0}, ", enumerator.Current);
                    }
                }
            }
    finally it worked. Thanks woss, and everyone!

  17. #17

    Thread Starter
    Frenzied Member oceanebelle's Avatar
    Join Date
    Jun 2005
    Location
    my n00k.
    Posts
    1,064

    Re: Jagged Array Dimension

    Quote Originally Posted by wossname
    Code:
    if( outerarray[0] is string[])
    //yeah its a string array
    else
    //nope, its not.

    as you can see instead of specifying a type I just used Array, that way whatever type the array is, it'd still work for this function.

  18. #18
    type Woss is new Grumpy; wossname's Avatar
    Join Date
    Aug 2002
    Location
    #!/bin/bash
    Posts
    5,682

    Re: [RESOLVED] Jagged Array Dimension

    Inheritence is FUNdamental
    I don't live here any more.

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  



Click Here to Expand Forum to Full Width