[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. :wave:
Re: Jagged Array Dimension
how do I do this without using foreach?
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:
Dim outerArr As String()()
'...
For Each innerArr As String() In outerArr
For each str As String In innerArr
MessageBox.Show(str)
Next str
Next innerArr
or:
VB Code:
Dim outerArr As String()()
'...
Dim innerArr As String()
Dim str As String
For i As Integer = 0 To outerArr.GetUpperBound(0) Step 1
innerArr = outerArr(i)
For j As Integer = 0 To innerArr.GetUpperBound(0) Step 1
str = innerArr(j)
MessageBox.Show(str)
Next j
Next i
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.
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?
Re: Jagged Array Dimension
If its typed as string[][] then each element can only be of type string[].
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... :cry:
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...
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?
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
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.
Re: Jagged Array Dimension
Hold on, I'll see what I can do.
Re: Jagged Array Dimension
Are you using 1.1 or 2.0?
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
Re: Jagged Array Dimension
Code:
if( outerarray[0] is string[])
//yeah its a string array
else
//nope, its not.
Re: Jagged Array Dimension
ok i'll try that wossy. ;)
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!
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. :wave:
Re: [RESOLVED] Jagged Array Dimension
Inheritence is FUNdamental :)