| | |
Comparing C# Int Arrays
Please support our ASP.NET advertiser: $4.95 a Month - ASP.NET Web Hosting – Click Here!
Thread Solved
![]() |
•
•
Join Date: Jun 2009
Posts: 6
Reputation:
Solved Threads: 0
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
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
Can you use generics? They are a little easier to deal with than arrays:
c# Syntax (Toggle Plain Text)
private void simpleButton2_Click(object sender, EventArgs e) { List<int> free = new List<int>(); List<int> used = new List<int>(); free.AddRange(new int[] { 1, 2, 3, 4, 5, 6 }); used.AddRange(new int[] { 2, 3 }); int nextNumber = GetNextFreeNumber(free, used); System.Diagnostics.Debugger.Break(); } private int GetNextFreeNumber(List<int> free, List<int> used) { foreach (int i in free) { if (used.Contains(i)) continue; else { used.Add(i); return i; } } throw new Exception("out of free number"); }
•
•
Join Date: Jun 2009
Posts: 6
Reputation:
Solved Threads: 0
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
Chris Cummings - www.iwdonline.com
Yes:
c# Syntax (Toggle Plain Text)
private void simpleButton2_Click(object sender, EventArgs e) { int[] numbers = new int[] { 1, 2, 3, 4, 5, 6, 7 }; List<int> lst = new List<int>(); lst.AddRange(numbers); //adds the array to the generic list
Last edited by sknake; Jun 25th, 2009 at 4:10 pm.
•
•
Join Date: Jun 2009
Posts: 6
Reputation:
Solved Threads: 0
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:
Now one more question...why didn't I join Daniweb sooner? :-)
Thanks again
Here's the code I used in the end for those interested:
ASP.NET Syntax (Toggle Plain Text)
List<int> free = new List<int>(); List<int> used = new List<int>(); free.AddRange(FreeNumbers); //adds the array to the generic list used.AddRange(UsedNumbers); //adds the array to the generic list foreach (int i in free) { if (used.Contains(i)) continue; else { Response.Write("<hr>"); Response.Write(i); Response.Write(" is the next available number<hr>"); break; } }
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
![]() |
Similar Threads
- how to add a selection sort to this code? (C++)
- Comparing items in arrays (Perl)
- passing arrays (C++)
- Confused about use * in declaring char arrays (C++)
- Hey Just started new topic with arrays, and having some trouble please help (C++)
- help with sorting arrays from hightest to lowest (C++)
- Having trouble with comparing strings and arrays (Java)
- compare (C++)
Other Threads in the ASP.NET Forum
- Previous Thread: w3wp.exe Error
- Next Thread: How to Dynamically generate web.sitemap?
| Thread Tools | Search this Thread |
.net 2.0 3.5 activexcontrol advice ajax alltypeofvideos appliances asp asp.net bc30451 beginner bottomasp.net browser businesslogiclayer c# c#gridviewcolumn cac checkbox commonfunctions compatible confirmationcodegeneration content contenttype courier css dataaccesslayer database datagrid datagridview datagridviewcheckbox datalist development dgv dropdownlist dropdownmenu dynamically edit fileuploader fill flash flv formatdecimal forms formview gridview gudi homeedition iframe iis javascript jquery listbox menu microsoft mono mouse mssql multistepregistration nameisnotdeclared news objects opera panelmasterpagebuttoncontrols redirect registration relationaldatabases reportemail rotatepage schoolproject security serializesmo.table sessionvariables silverlight smartcard smoobjects software sql-server sqlserver2005 suse textbox tracking treeview unauthorized validatedate validation vb.net video videos virtualdirectory vista visual-studio visualstudio web webapplications webarchitecture webdevelopemnt webprogramming webservice youareanotmemberofthedebuggerusers






