C# School Assignment? Project Due Tomorrow? Chat LIVE With A Programming Expert!

Welcome to Dream.In.Code
Become a C# Expert!

Join 307,104 C# Programmers for FREE! Get instant access to thousands of C# experts, tutorials, code snippets, and more! There are 2,053 people online right now. Registration is fast and FREE... Join Now!




Add multiple array's data to one big array

 

Add multiple array's data to one big array, How ya do dat!

Mastergeek666

29 Jun, 2009 - 10:53 PM
Post #1

D.I.C Head
Group Icon

Joined: 10 Aug, 2007
Posts: 145



Thanked: 1 times
Dream Kudos: 75
My Contributions
Hi! Ok so the question is: if I have say 3 string arrays how would i go about putting the contents of all three of those arrays into one big array? Here is some example code:
CODE

string[] class1 = new string[] {"Susy", "Bobby", "Steve"};
string[] class2 = new string[] {"Jimmy", "Layla", "Jim"};
string[] class3 = new string[] {"Joey", "Jeff", "Ted"};
string[] students;
//and then i was thinking nested for loops to
//add the contents of class1, class2, and class3 to
//the students array, but I'm hitting a snag...
//here is the nested for loop i was talking about
for(int a = 0; a < class1.length; a++)
{
    students[a] = class1[a];
    for(int b = class1.length; b < class2.length; b++)
    {
          students[b] = class2[b];
          for(int c = class2.length; c < class3.length; c++)
          {
                students[c] = class3[c];
          }
    }
}

Thats the general idea but as I mentioned i've hit a snag and it says, "Object reference not set to an instance of an object." I've had this problem before with but i just can't seem to keep the solution in my head. tongue.gif Much kudos on the help

This post has been edited by Mastergeek666: 29 Jun, 2009 - 10:53 PM

User is offlineProfile CardPM
+Quote Post


Martyr2

RE: Add Multiple Array's Data To One Big Array

29 Jun, 2009 - 11:10 PM
Post #2

Programming Theoretician
Group Icon

Joined: 18 Apr, 2007
Posts: 7,307



Thanked: 837 times
Expert In: C/C++, Java, VB, VB.NET, C#, PHP, Web Development, HTML & CSS, Javascript

My Contributions
Just curious but have you tried looking at the array.copyto method? It can be used to merge arrays together by copying the contents of one array to another.

But if you insist on doing it with the loop, keep in mind that your destination array has to have its size defined and it should be the size of your all your other arrays together.

Plus how is class1 array ever going to be less than class2 array's length when they are the same size?

So in short, look at the length of each array and the size of your destination array and make sure you define the size and that it is big enough to hold all your others, then iterate through each and copy it to the destination.

smile.gif
User is offlineProfile CardPM
+Quote Post

Mastergeek666

RE: Add Multiple Array's Data To One Big Array

29 Jun, 2009 - 11:46 PM
Post #3

D.I.C Head
Group Icon

Joined: 10 Aug, 2007
Posts: 145



Thanked: 1 times
Dream Kudos: 75
My Contributions
QUOTE(Martyr2 @ 29 Jun, 2009 - 11:10 PM) *

Just curious but have you tried looking at the array.copyto method? It can be used to merge arrays together by copying the contents of one array to another.

But if you insist on doing it with the loop, keep in mind that your destination array has to have its size defined and it should be the size of your all your other arrays together.

Plus how is class1 array ever going to be less than class2 array's length when they are the same size?

So in short, look at the length of each array and the size of your destination array and make sure you define the size and that it is big enough to hold all your others, then iterate through each and copy it to the destination.

smile.gif


Well I wish I would of known about the array.copyto method before that would have made my life so much easier countless times. Well thanks for that tidbit of information now I have a new problem. As my example was just that, an example, i'll fill you in on my actual program. I'm using an object array(thanks to your method) and within it lies a crap load of structures. Within those structures I have specific 'ints' that I wan't to pull out. What i've done is created an int array then a for loop to neatly take and place all the ints and place then within the int array. Here is an example:
CODE

total = new object[First.length + second.length];
point = new int[total.length]
for(int i = 0; i < point.length; i++)
{
//Myprogram is the name of my project and this is a structure cast thing
//so that i can pull the numerous amounts of data from the structure
//that where created in form1
     point[i] = ((Myprogram.Form1.Unit)total[i]).unitCost;
}


With this loop I pull all the unitCosts from all the structures i have within the object array. Now the problem is I keep getting the error, 'Object reference not set to an instance of an object.' I can't seem to figure out why this problem is occurring. Much thanks for your expertise.

User is offlineProfile CardPM
+Quote Post

masteryee

RE: Add Multiple Array's Data To One Big Array

30 Jun, 2009 - 12:36 AM
Post #4

D.I.C Regular
***

Joined: 16 May, 2009
Posts: 269



Thanked: 38 times
My Contributions
You need to actually store something in your "total" array before you can access any of the underlying objects. Right now, your array contains null data at each index. When you write new object[First.length + second.length], you're only creating an array that can hold up to so many objects, but you still need to populate the array.
User is offlineProfile CardPM
+Quote Post

Mastergeek666

RE: Add Multiple Array's Data To One Big Array

30 Jun, 2009 - 01:46 AM
Post #5

D.I.C Head
Group Icon

Joined: 10 Aug, 2007
Posts: 145



Thanked: 1 times
Dream Kudos: 75
My Contributions
QUOTE(masteryee @ 30 Jun, 2009 - 12:36 AM) *

You need to actually store something in your "total" array before you can access any of the underlying objects. Right now, your array contains null data at each index. When you write new object[First.length + second.length], you're only creating an array that can hold up to so many objects, but you still need to populate the array.


Oh yeah no sorry I forgot to put that in the code snippet. I have a bunch of stuff in the total array.
User is offlineProfile CardPM
+Quote Post

baavgai

RE: Add Multiple Array's Data To One Big Array

30 Jun, 2009 - 02:47 AM
Post #6

Dreaming Coder
Group Icon

Joined: 16 Oct, 2007
Posts: 4,350



Thanked: 411 times
Dream Kudos: 550
Expert In: C, C++, Java, C#, ASP.NET, PHP, Perl, Python, Oracle, SQL Server, MySql, HTML, JavaScript, Lua, Cheese

My Contributions
For the way you seem to be thinking, this would work:

csharp

string[] students = new string[class1.Length + class2.Length + class3.Length];
int i = 0;
foreach (string s in class1) { students[i++] = s; }
foreach (string s in class2) { students[i++] = s; }
foreach (string s in class3) { students[i++] = s; }


However, what if you have to do that more than three times? Or over the course of other process and don't know the size ahead of time?

I'd prefer to do it like this:
csharp

List<string> list = new List<string>();
list.AddRange(class1);
list.AddRange(class2);
list.AddRange(class3);
string[] students = list.ToArray();


Hope this helps.

User is offlineProfile CardPM
+Quote Post

Fast ReplyReply to this topicStart new topic

Time is now: 11/21/09 12:37PM

Live C# Help!

Be Social

Dream.In.Code RSS Feed Dream.In.Code LinkedIn Group Follow Us On Twitter Fan Us On Facebook

C# Tutorials

Reference Sheets

C# Snippets

DIC Chatroom

Bye Bye Ads

Monthly Drawing

Thumb Drive

Top Contributors

Top 10 Kudos This Month