Morpheus Posted January 26, 2003 Posted January 26, 2003 I have a 2d array and I want to print it using foreach. How do I do that? Quote
*Experts* Volte Posted January 26, 2003 *Experts* Posted January 26, 2003 A standardDim i As Integer For Each i In multiDimArray Nextwill do it. I'm not totally sure what order it traverses it (which dimension increments first in the loop), but I'm sure you can figure that out. Quote
Morpheus Posted January 26, 2003 Author Posted January 26, 2003 Well that is VB code isn't it? The array looks something like this. {{0,0}, {0,1}, {0,2}, {0,3}, {0,4}, {1,0}...........{4,4} and it should look like this on the consolewindow: 0,0 0,1 0,2 0,3 0,4 1,0 1,1 1,2 1,3 1,4 and so on Quote
*Experts* Volte Posted January 26, 2003 *Experts* Posted January 26, 2003 Heh, sorry, I didn't realize this is the C# forum. In C# it would be like this:foreach(int i in theArray) { } Quote
Morpheus Posted January 26, 2003 Author Posted January 26, 2003 Yes that's how far I got, but I can't get it to look the way I want it to. Someone told med that I had to use GetLength(). Quote
*Experts* Volte Posted January 26, 2003 *Experts* Posted January 26, 2003 You know, you can always not use foreach; it may be more appropriate, in this case, to use a standard for loop.int i,j; for (i=0;i<=myArray.GetLength();i++) { for (j=0;j<=myArray.GetLength();j++) { // use myArray[i,j] to output the data } } Quote
Morpheus Posted January 26, 2003 Author Posted January 26, 2003 Yes that would be really nice :) but I have to use foreach, school assignement ya know. Quote
*Experts* Volte Posted January 26, 2003 *Experts* Posted January 26, 2003 Oh. Well, I really don't know what to tell you. foreach can easily get the data, but I have no idea how to get it out in the order that you want it. Sorry. :-\ Quote
*Experts* Nerseus Posted January 26, 2003 *Experts* Posted January 26, 2003 If it's a school assignment shouldn't you be figuring this out on your own? :p -Nerseus Quote "I want to stand as close to the edge as I can without going over. Out on the edge you see all the kinds of things you can't see from the center." - Kurt Vonnegut
*Gurus* divil Posted January 26, 2003 *Gurus* Posted January 26, 2003 Why would you use foreach to traverse a 2-dimensional array? Just curious. I assume your assignment gives some kind of reason. Perhaps I'm assuming too much :) Quote MVP, Visual Developer - .NET Now you see why evil will always triumph - because good is dumb. My free .NET Windows Forms Controls and Articles
*Experts* Volte Posted January 26, 2003 *Experts* Posted January 26, 2003 Perhaps it is a trick question, and your instructor wants you to say "why?" :p Quote
Morpheus Posted January 27, 2003 Author Posted January 27, 2003 Nope no reason why and some friends have made it. Well assignement was a bad word to use, it's more like an excersice and we can use help from other people. There is a big difference between ripping code and getting help. Quote
Morpheus Posted January 28, 2003 Author Posted January 28, 2003 Done! int[][] mul; mul=new int[4][]; mul[0] = new int[5] {0,1,2,3,4}; mul[1] = new int[5] {0,1,2,3,4}; mul[2] = new int[5] {0,1,2,3,4}; mul[3] = new int[5] {0,1,2,3,4}; int counter=0; foreach(int[] j in mul) { foreach(int i in j) { Console.Write("{0},{1} ",counter,i); } Console.WriteLine(); counter++; } Quote
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.