Comparing C# Int Arrays

Please support our ASP.NET advertiser: Intel Parallel Studio Home
Thread Solved

Join Date: Jun 2009
Posts: 6
Reputation: IntegrityWebDev is an unknown quantity at this point 
Solved Threads: 0
IntegrityWebDev IntegrityWebDev is offline Offline
Newbie Poster

Comparing C# Int Arrays

 
0
  #1
Jun 25th, 2009
Hello all...my first official post (after the introduction)

I'm relatively new in the area of C# (some PHP experience) and I'm hoping someone can help me.

I have 2 arrays of variable lengths, with int data in them. Something like this:

FreeNumbers = 1,2,3,4,5
UsedNumbers = 1,2

I want to take the first FreeNumber (1) and search through the used number array. If it exists there I want to move on to the next FreeNumber (2) and do the same. Once I find a FreeNumber (3) that is NOT in the UsedNumber array, I want to assign it to another var (TheNextFreeNumber).

Can someone help me with this? I was thinking this should be done through For's, ForEach or Try/Catch but I'm just not quite getting there. Maybe there is an even simpler C# function?

Thanks for any help.

-Chris
Chris Cummings - www.iwdonline.com
Reply With Quote Quick reply to this message  
Join Date: Feb 2009
Posts: 3,187
Reputation: sknake has much to be proud of sknake has much to be proud of sknake has much to be proud of sknake has much to be proud of sknake has much to be proud of sknake has much to be proud of sknake has much to be proud of sknake has much to be proud of sknake has much to be proud of sknake has much to be proud of 
Solved Threads: 571
Sponsor
sknake's Avatar
sknake sknake is offline Offline
.NET Enthusiast

Re: Comparing C# Int Arrays

 
0
  #2
Jun 25th, 2009
Can you use generics? They are a little easier to deal with than arrays:

  1. private void simpleButton2_Click(object sender, EventArgs e)
  2. {
  3. List<int> free = new List<int>();
  4. List<int> used = new List<int>();
  5. free.AddRange(new int[] { 1, 2, 3, 4, 5, 6 });
  6. used.AddRange(new int[] { 2, 3 });
  7.  
  8. int nextNumber = GetNextFreeNumber(free, used);
  9. System.Diagnostics.Debugger.Break();
  10. }
  11. private int GetNextFreeNumber(List<int> free, List<int> used)
  12. {
  13. foreach (int i in free)
  14. {
  15. if (used.Contains(i))
  16. continue;
  17. else
  18. {
  19. used.Add(i);
  20. return i;
  21. }
  22. }
  23. throw new Exception("out of free number");
  24. }
Scott Knake
Custom Software Development
Apex Software, Inc.
Reply With Quote Quick reply to this message  
Join Date: Jun 2009
Posts: 6
Reputation: IntegrityWebDev is an unknown quantity at this point 
Solved Threads: 0
IntegrityWebDev IntegrityWebDev is offline Offline
Newbie Poster

Re: Comparing C# Int Arrays

 
0
  #3
Jun 25th, 2009
I haven't used generics before. The arrays are already built and filled. Is there an easy way to move the aray data to the generics? There are about 300 elements per array.

-Chris
Chris Cummings - www.iwdonline.com
Reply With Quote Quick reply to this message  
Join Date: Feb 2009
Posts: 3,187
Reputation: sknake has much to be proud of sknake has much to be proud of sknake has much to be proud of sknake has much to be proud of sknake has much to be proud of sknake has much to be proud of sknake has much to be proud of sknake has much to be proud of sknake has much to be proud of sknake has much to be proud of 
Solved Threads: 571
Sponsor
sknake's Avatar
sknake sknake is offline Offline
.NET Enthusiast

Re: Comparing C# Int Arrays

 
0
  #4
Jun 25th, 2009
Yes:
  1. private void simpleButton2_Click(object sender, EventArgs e)
  2. {
  3. int[] numbers = new int[] { 1, 2, 3, 4, 5, 6, 7 };
  4. List<int> lst = new List<int>();
  5. lst.AddRange(numbers); //adds the array to the generic list
Last edited by sknake; Jun 25th, 2009 at 4:10 pm.
Scott Knake
Custom Software Development
Apex Software, Inc.
Reply With Quote Quick reply to this message  
Join Date: Jun 2009
Posts: 6
Reputation: IntegrityWebDev is an unknown quantity at this point 
Solved Threads: 0
IntegrityWebDev IntegrityWebDev is offline Offline
Newbie Poster

Re: Comparing C# Int Arrays

 
0
  #5
Jun 25th, 2009
That Got It! Thank you so much! Plus I learned about Generics for the first time. Will study up more on that.

Here's the code I used in the end for those interested:
  1. List<int> free = new List<int>();
  2. List<int> used = new List<int>();
  3. free.AddRange(FreeNumbers); //adds the array to the generic list
  4. used.AddRange(UsedNumbers); //adds the array to the generic list
  5.  
  6. foreach (int i in free)
  7. {
  8. if (used.Contains(i))
  9. continue;
  10. else
  11. {
  12. Response.Write("<hr>");
  13. Response.Write(i);
  14. Response.Write(" is the next available number<hr>");
  15. break;
  16. }
  17. }

Now one more question...why didn't I join Daniweb sooner? :-)

Thanks again
Last edited by IntegrityWebDev; Jun 25th, 2009 at 4:36 pm. Reason: Added Code.
Chris Cummings - www.iwdonline.com
Reply With Quote Quick reply to this message  
Reply

This thread has been marked solved.
Perhaps start a new thread instead?
Message:


Thread Tools Search this Thread



About Us | Contact Us | Advertise | DaniWeb | Acceptable Use Policy | RSS Feed

©2003 - 2009 DaniWeb® LLC