|
-
Oct 25th, 2006, 11:08 PM
#1
Thread Starter
Frenzied Member
[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.
-
Oct 25th, 2006, 11:10 PM
#2
Thread Starter
Frenzied Member
Re: Jagged Array Dimension
how do I do this without using foreach?
-
Oct 25th, 2006, 11:20 PM
#3
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
-
Oct 25th, 2006, 11:21 PM
#4
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.
-
Oct 26th, 2006, 12:50 AM
#5
Thread Starter
Frenzied Member
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?
-
Oct 26th, 2006, 01:02 AM
#6
Re: Jagged Array Dimension
If its typed as string[][] then each element can only be of type string[].
-
Oct 26th, 2006, 01:09 AM
#7
Thread Starter
Frenzied Member
Re: Jagged Array Dimension
 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...
-
Oct 26th, 2006, 01:20 AM
#8
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?
-
Oct 26th, 2006, 01:23 AM
#9
Thread Starter
Frenzied Member
Re: Jagged Array Dimension
 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
-
Oct 26th, 2006, 01:25 AM
#10
Thread Starter
Frenzied Member
Re: Jagged Array Dimension
 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.
-
Oct 26th, 2006, 01:33 AM
#11
Re: Jagged Array Dimension
Hold on, I'll see what I can do.
-
Oct 26th, 2006, 01:41 AM
#12
Re: Jagged Array Dimension
Are you using 1.1 or 2.0?
-
Oct 26th, 2006, 01:47 AM
#13
Thread Starter
Frenzied Member
Re: Jagged Array Dimension
 Originally Posted by penagate
Are you using 1.1 or 2.0?
Right now i've only got 2.0
-
Oct 26th, 2006, 06:21 AM
#14
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.
-
Oct 26th, 2006, 06:39 AM
#15
Thread Starter
Frenzied Member
Re: Jagged Array Dimension
ok i'll try that wossy.
-
Oct 26th, 2006, 06:47 AM
#16
Thread Starter
Frenzied Member
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!
-
Oct 26th, 2006, 06:50 AM
#17
Thread Starter
Frenzied Member
Re: Jagged Array Dimension
 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.
-
Oct 27th, 2006, 03:12 PM
#18
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
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|