somewhere I heard don't use ararys but they never said what alternative to use. Is using collections a better alternative? And is so why. Why not use arrays?
IMO there is no "one is better than the other" when it comes to arrays vs. collections.
Both have their pros and cons and depending on the situation is how you should decide which to use.
Rule of thumb:
Arrays perform better for fixed data, that is data which will not be manipulated once inside the array.
Collections perform better for dynamic data, that is data which will be manipulated, added to, or delete from while inside the collection.
@ damon2012 TIMTOWTDI =. there is more than one way to do it
the best answer is likely it depends.
two of the criteria are nature of the problem being solved and performance; i.e., the type of problem one is trying to solve may influence the data structure that one chooses.
for example:
let's say one wishes to keep track in memory of what end user non unuque ip addresses are associated with visits to one's website; depending on the popularity of the website, one could average from a few visits per day to thousands
of visits per day; in cases like this, it's a good idea to use a list which can grow as required ... a not so good alternative might be to create a very large array an hope that it's big enough for your worst case scenario.
OTOH, let's say one is running a survey asking end users what month is their favourite month ... although the answers are coming in randomly, there are only 12 months in a year ... for that reason, an array of 12 elements would
be appropriate.
g.
B-) Please help me by completing my school survey about computer programmers on my website. Thank you!!! Gerry Lowry +1 705-429-7550 wasaga beach, ontario, canada
Everything has two sides, it has advantage and disadvantage, so it depends on what are you using array for and how do you using the array.
One of the disadvantages of an array is the fixed size on creation. An advantage is in effect linking data together, especially with multiple dimensions. With lists, you can use multiple delimiters and handle it that way, where each item is really multiple
bits of related data. Alternately, you use multiple lists, and have to track them all.
Let's say you wanted a simple address book. There's a name, address line or two, phone number or two or three, email addresses these days, and more. You can handle each one in a separate list, since it would be really awkward to try to put it all in one
mega-list, or you can use one array with 2 dimensions. X number of people by Y number of items to store should be a lot easier to work with.
One other nice little advantage is you no longer have to watch that your delimiters aren't in your items, although it was only rarely a difficulty with tabs. Debugging may be a bit easier, though there will be problems adjusting to the zero-based arrays.
Thanks,
Amy Peng
Please mark the replies as answers if they help or unmark if not.
Feedback to us
damon2012
Member
21 Points
107 Posts
Is using arrays a bad idea?
Dec 13, 2012 08:44 PM|LINK
Hi,
somewhere I heard don't use ararys but they never said what alternative to use. Is using collections a better alternative? And is so why. Why not use arrays?
Thanks.
jprochazka
Contributor
4896 Points
740 Posts
Re: Is using arrays a bad idea?
Dec 13, 2012 09:01 PM|LINK
IMO there is no "one is better than the other" when it comes to arrays vs. collections.
Both have their pros and cons and depending on the situation is how you should decide which to use.
Rule of thumb:
Arrays perform better for fixed data, that is data which will not be manipulated once inside the array.
Collections perform better for dynamic data, that is data which will be manipulated, added to, or delete from while inside the collection.
krutovdl
Member
49 Points
38 Posts
Re: Is using arrays a bad idea?
Dec 13, 2012 09:13 PM|LINK
Consider Generics. They give you the freedom to choose any data type, class, etc. Arrays are good when you know the total count of rows such as
int[] a = { 1, 2, 3, 4, 5 };
, but generics allow to add a row as needed
List<int> b =new List<int>(); for(int i = 0; i < 100; i++) b.add(i);
Depends on what you want to do.
gerrylowry
All-Star
20515 Points
5713 Posts
Re: Is using arrays a bad idea?
Dec 14, 2012 04:23 AM|LINK
@ damon2012 TIMTOWTDI =. there is more than one way to do it
the best answer is likely it depends.
two of the criteria are nature of the problem being solved and performance; i.e., the type of problem one is trying to solve may influence the data structure that one chooses.
for example:
let's say one wishes to keep track in memory of what end user non unuque ip addresses are associated with visits to one's website; depending on the popularity of the website, one could average from a few visits per day to thousands of visits per day; in cases like this, it's a good idea to use a list which can grow as required ... a not so good alternative might be to create a very large array an hope that it's big enough for your worst case scenario.
OTOH, let's say one is running a survey asking end users what month is their favourite month ... although the answers are coming in randomly, there are only 12 months in a year ... for that reason, an array of 12 elements would be appropriate.
g.
Amy Peng - M...
Star
10199 Points
964 Posts
Microsoft
Re: Is using arrays a bad idea?
Dec 17, 2012 05:02 AM|LINK
Hi damon2012,
Everything has two sides, it has advantage and disadvantage, so it depends on what are you using array for and how do you using the array.
One of the disadvantages of an array is the fixed size on creation. An advantage is in effect linking data together, especially with multiple dimensions. With lists, you can use multiple delimiters and handle it that way, where each item is really multiple bits of related data. Alternately, you use multiple lists, and have to track them all.
Let's say you wanted a simple address book. There's a name, address line or two, phone number or two or three, email addresses these days, and more. You can handle each one in a separate list, since it would be really awkward to try to put it all in one mega-list, or you can use one array with 2 dimensions. X number of people by Y number of items to store should be a lot easier to work with.
One other nice little advantage is you no longer have to watch that your delimiters aren't in your items, although it was only rarely a difficulty with tabs. Debugging may be a bit easier, though there will be problems adjusting to the zero-based arrays.
Thanks,
Amy Peng
Feedback to us
Develop and promote your apps in Windows Store