473,384 Members | 1,854 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,384 software developers and data experts.

Extracting single dimensional array out of two dimensional array

Hi,
I am using framework 2.0. I am writing a foreach loop that will
extract single dimensional arrays out of double dimensional array. I
am trying writing something like this.

string [,] strDetails
foreach(string[] str in strDetails)
{
//Code comes here
}

I have studied that it is not possible to do like this. The maximum
we can do is extracting individual values in the array.
Any comments? Opinions? Solutions?
Thanks
Chakravarti Mukesh

Oct 24 '07 #1
5 3620
Having a jagged array can help in this.
string[][] strDetails

I think, foreach (string[]... ) should work in such a case.

On Oct 24, 2:33 pm, Mukesh <cmukes...@gmail.comwrote:
Hi,
I am using framework 2.0. I am writing a foreach loop that will
extract single dimensional arrays out of double dimensional array. I
am trying writing something like this.

string [,] strDetails
foreach(string[] str in strDetails)
{
//Code comes here

}

I have studied that it is not possible to do like this. The maximum
we can do is extracting individual values in the array.
Any comments? Opinions? Solutions?
Thanks
Chakravarti Mukesh

Oct 24 '07 #2
Mukesh <cm*******@gmail.comwrote:
I am using framework 2.0. I am writing a foreach loop that will
extract single dimensional arrays out of double dimensional array. I
am trying writing something like this.

string [,] strDetails
foreach(string[] str in strDetails)
{
//Code comes here
}

I have studied that it is not possible to do like this. The maximum
we can do is extracting individual values in the array.
Any comments? Opinions? Solutions?
That's correct.

If you can produce a string[][] (an array of string arrays) instead,
it's a lot easier.

How much of what you want to do is fixed? Do you definitely need a
string[], or just (for example) an IEnumerable<string>?

--
Jon Skeet - <sk***@pobox.com>
http://www.pobox.com/~skeet Blog: http://www.msmvps.com/jon.skeet
If replying to the group, please do not mail me too
Oct 24 '07 #3
Mukesh wrote:
I am using framework 2.0. I am writing a foreach loop that will
extract single dimensional arrays out of double dimensional array. I
am trying writing something like this.

string [,] strDetails
foreach(string[] str in strDetails)
{
//Code comes here
}

I have studied that it is not possible to do like this. The maximum
we can do is extracting individual values in the array.
Any comments? Opinions? Solutions?
For some options:

using System;

namespace E
{
public class Program
{
public static void Main(string[] args)
{
int[,] a = new int[3,3];
for(int i = 0; i < 3; i++)
{
for(int j = 0; j < 3; j++)
{
a[i,j] = 3*i+j+1;
}
}
int[] a1 = new int[3];
Buffer.BlockCopy(a, 3*sizeof(int), a1, 0, 3*sizeof(int));
Console.WriteLine(a1[0] + " " + a1[1] + " " + a1[2]);
int[][] b = new int[3][];
for(int i = 0; i < 3; i++)
{
b[i] = new int[3];
for(int j = 0; j < 3; j++)
{
b[i][j] = 3*i+j+1;
}
}
int[] b1 = b[1];
Console.WriteLine(b1[0] + " " + b1[1] + " " + b1[2]);
Console.ReadKey();
}
}
}

Arne
Oct 24 '07 #4
Mukesh wrote:
[...]
string [,] strDetails
foreach(string[] str in strDetails)
{
//Code comes here
}

I have studied that it is not possible to do like this. The maximum
we can do is extracting individual values in the array.
Well "strDetails" isn't an array of arrays; it's a single
two-dimensional array. So that's why you can't enumerate the "string[]"
instances in the "strDetails" array. There aren't any.

That said, there's nothing to stop you from iterating over a single
dimension of the two-dimensional array, and then within that iterating
over the other dimension for the purpose of building a new
single-dimensional array of strings.

Something like this:

string[,] strDetails;

for (int i = 0; i < strDetails.GetLength(0); i++)
{
string[] str = new string[strDetails.GetLength(1)];

for (int j = 0; j < str.Length; j++)
{
str[j] = strDetails[i, j];
}

// do something with str here
}

Pete
Oct 24 '07 #5
Arne Vajhøj wrote:
Mukesh wrote:
>I am using framework 2.0. I am writing a foreach loop that will
extract single dimensional arrays out of double dimensional array. I
am trying writing something like this.

string [,] strDetails
foreach(string[] str in strDetails)
{
//Code comes here
}

I have studied that it is not possible to do like this. The maximum
we can do is extracting individual values in the array.
Any comments? Opinions? Solutions?

For some options:
...
Note that the block copy does not work for strings and therfore
is not applicable to your problem.

Arne

Oct 24 '07 #6

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

4
by: lecichy | last post by:
Hello Heres the situation: I got a file with lines like: name:second_name:somenumber:otherinfo etc with different values between colons ( just like passwd file) What I want is to extract...
10
by: Sona | last post by:
Please could someone tell me how to use a single-dimensional array as a two-dimensional array? Is the following correct: int ROW_SIZE = 5; int COL_SIZE = 3; int *arr = (int*)...
4
by: Venkat | last post by:
Hi All, I need to copy strings from a single dimensional array to a double dimensional array. Here is my program. #include <stdio.h> #include <stdlib.h>
2
by: ip4ram | last post by:
I used to work with C and have a set of libraries which allocate multi-dimensional arrays(2 and 3) with single malloc call. data_type **myarray =...
3
by: Sim | last post by:
Hi, I come from a C background and I heard that in C# single dimensional arrays are stored in a tree structure internally? Is that true? I know that's true for jagged arrays but I'm not sure...
8
by: per9000 | last post by:
Hi all, I have a two-dimensional array of data, f.x int's. We can imagine that the array is "really large". Now I want the data in it and store this in a one-dimensional array. The obvious...
272
by: Peter Olcott | last post by:
http://groups.google.com/group/comp.lang.c++/msg/a9092f0f6c9bf13a I think that the operator() member function does not work correctly, does anyone else know how to make a template for making two...
1
by: rpjd | last post by:
I am completely new to this so please bear with me here. My project involves a webpage executing php scripts via an xmlhttprequest which queries a database and returns data to the webpage. This code...
5
by: nelly0 | last post by:
developing a program that will manipulate noise levels (measured in decibels) that is collected by car manufacturers. These noise levels are produced at seven different speeds by a maximum of six...
1
by: CloudSolutions | last post by:
Introduction: For many beginners and individual users, requiring a credit card and email registration may pose a barrier when starting to use cloud servers. However, some cloud server providers now...
0
by: Faith0G | last post by:
I am starting a new it consulting business and it's been a while since I setup a new website. Is wordpress still the best web based software for hosting a 5 page website? The webpages will be...
0
by: ryjfgjl | last post by:
In our work, we often need to import Excel data into databases (such as MySQL, SQL Server, Oracle) for data analysis and processing. Usually, we use database tools like Navicat or the Excel import...
0
by: taylorcarr | last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.