473,770 Members | 4,999 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

Array vs ArrayList

is it true that I cannot dynamically add an item to an array? Eg

public char[] = {"a","b"};
char.Add("newit em");
Do I really have to switch to ArrayList to do this?
Nov 16 '05 #1
9 23704
Arrays are fixed size. An ArrayList takes care of the resizing you would have to manage yourself but does so in a generaic way so isn't typesafe.

You can of course allocate an array larger than you know you need at the point of allocation and tehn fill the extra elements with the new stuff you discover. Or even better if you know the fina size of hte population allocate an array of this size upfront. But as to the original question? Yes its true, no you don't need to switch to an ArrayList if you're happy to manage the reallocation yourself.

Incidently, why are you so worried about switching to an ArrayList?

Regards

Richard Blewett - DevelopMentor
http://www.dotnetconsult.co.uk/weblog
http://www.dotnetconsult.co.uk

is it true that I cannot dynamically add an item to an array? Eg

public char[] = {"a","b"};
char.Add("newit em");
Do I really have to switch to ArrayList to do this?

Nov 16 '05 #2
thx Richard
well I'm doing a hangman application and add upp the guessed letters to a
string Array. In one instance I have to search that particular array using
IndexOf() method but it is more complicated on ArrayList since there the
items are of object type...

"Richard Blewett [DevelopMentor]" <ri******@NOSPA Mdevelop.com> wrote in
message news:OL******** ******@TK2MSFTN GP10.phx.gbl...
Arrays are fixed size. An ArrayList takes care of the resizing you would
have to manage yourself but does so in a generaic way so isn't typesafe.

You can of course allocate an array larger than you know you need at the
point of allocation and tehn fill the extra elements with the new stuff
you discover. Or even better if you know the fina size of hte population
allocate an array of this size upfront. But as to the original question?
Yes its true, no you don't need to switch to an ArrayList if you're happy
to manage the reallocation yourself.

Incidently, why are you so worried about switching to an ArrayList?

Regards

Richard Blewett - DevelopMentor
http://www.dotnetconsult.co.uk/weblog
http://www.dotnetconsult.co.uk

is it true that I cannot dynamically add an item to an array? Eg

public char[] = {"a","b"};
char.Add("newit em");
Do I really have to switch to ArrayList to do this?

Nov 16 '05 #3
Well I guess you know the maximum size of the array then (the number of letters in your alphabet) so just allocate an array of that size and just keep a track of the last index you inserted at

Regards

Richard Blewett - DevelopMentor
http://www.dotnetconsult.co.uk/weblog
http://www.dotnetconsult.co.uk

thx Richard
well I'm doing a hangman application and add upp the guessed letters to a
string Array. In one instance I have to search that particular array using
IndexOf() method but it is more complicated on ArrayList since there the
items are of object type...

Nov 16 '05 #4
Davids <db****@simnet. is> wrote:
well I'm doing a hangman application and add upp the guessed letters
to a string Array. In one instance I have to search that particular
array using IndexOf() method but it is more complicated on ArrayList
since there the items are of object type...


I don't see why that makes it any more complicated, so long as the
items implement Equals appropriately (as strings do).

I'm not sure I'd implement it that way myself though - are you just
trying to find out whether or not a particular letter has been guessed?
If so, I'd suggest an array of 26 bools (assuming a case-insensitive
Western alphabet with no accents).

--
Jon Skeet - <sk***@pobox.co m>
http://www.pobox.com/~skeet
If replying to the group, please do not mail me too
Nov 16 '05 #5
just being obsessive and trying to have the code as simple as possible, I
see both Array and ArrayList are possible ways. This is how on learns c#,
this occurence forced me on finally reading about c# collections, thx for
your help!
"Jon Skeet [C# MVP]" <sk***@pobox.co m> wrote in message
news:MP******** *************** *@msnews.micros oft.com...
Davids <db****@simnet. is> wrote:
well I'm doing a hangman application and add upp the guessed letters
to a string Array. In one instance I have to search that particular
array using IndexOf() method but it is more complicated on ArrayList
since there the items are of object type...


I don't see why that makes it any more complicated, so long as the
items implement Equals appropriately (as strings do).

I'm not sure I'd implement it that way myself though - are you just
trying to find out whether or not a particular letter has been guessed?
If so, I'd suggest an array of 26 bools (assuming a case-insensitive
Western alphabet with no accents).

--
Jon Skeet - <sk***@pobox.co m>
http://www.pobox.com/~skeet
If replying to the group, please do not mail me too

Nov 16 '05 #6
Davids <db****@simnet. is> wrote:
just being obsessive and trying to have the code as simple as possible, I
see both Array and ArrayList are possible ways. This is how on learns c#,
this occurence forced me on finally reading about c# collections, thx for
your help!


To have the code as simple as possible, I'd seriously look at the array
of 26 bools approach - that's as simple as I can imagine...

--
Jon Skeet - <sk***@pobox.co m>
http://www.pobox.com/~skeet
If replying to the group, please do not mail me too
Nov 16 '05 #7
yep agree with you and have done that. Bools... sounds similiar to booleans
but I know that isn't the case?!?
"Jon Skeet [C# MVP]" <sk***@pobox.co m> wrote in message
news:MP******** *************** *@msnews.micros oft.com...
Davids <db****@simnet. is> wrote:
just being obsessive and trying to have the code as simple as possible, I
see both Array and ArrayList are possible ways. This is how on learns c#,
this occurence forced me on finally reading about c# collections, thx for
your help!


To have the code as simple as possible, I'd seriously look at the array
of 26 bools approach - that's as simple as I can imagine...

--
Jon Skeet - <sk***@pobox.co m>
http://www.pobox.com/~skeet
If replying to the group, please do not mail me too

Nov 16 '05 #8
Davids <db****@simnet. is> wrote:
yep agree with you and have done that. Bools... sounds similiar to booleans
but I know that isn't the case?!?


No, they're the same. bool is shorthand for System.Boolean in C#, just
as int is shorthand for System.Int32.

--
Jon Skeet - <sk***@pobox.co m>
http://www.pobox.com/~skeet
If replying to the group, please do not mail me too
Nov 16 '05 #9
Yes, arrays are fixed size and Arraylists are not.

with regards,
J.V.Ravichandra n
- http://www.geocities.com/
jvravichandran
- http://www.411asp.net/func/search?
qry=Ravichandra n+J.V.&cob=aspn etpro
- http://www.southasianoutlook.com
- http://www.MSDNAA.Net
- http://www.csharphelp.com
- http://www.poetry.com/Publications/
display.asp?ID= P3966388&BN=999 &PN=2
- Or, just search on "J.V.Ravichandr an"
at http://www.Google.com

*** Sent via Developersdex http://www.developersdex.com ***
Don't just participate in USENET...get rewarded for it!
Nov 16 '05 #10

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

Similar topics

1
21870
by: davehunt | last post by:
Hi folks, New C# programmer here. I am reading some CSV data from a file into an ArrayList. I want to get the data from the ArrayList into a 2-dimensional array. I see a few references to ..Split, but I'm not sure that's what I need. So, basically, what I have loaded into the ArrayList is:
13
11315
by: Hrvoje Voda | last post by:
How to put a specified dataset table into an array list ? Hrcko
9
1592
by: Steve | last post by:
Hello, I created a structure ABC and an array of type ABC Public Structure ABC Dim str1 As String Dim int1 As Integer End Structure Public ABC1 As New ABC, ABC2 As New ABC
4
2823
by: Peter | last post by:
I run into this situation all the time and I'm wondering what is the most efficient way to handle this issue: I'll be pulling data out of a data source and want to load the data into an array so that I can preform complicated operations against this data. The returned record count in these operations is always variable. 1. I have been using an arraylist.add function to handle non-multidemional returns but was wondering if I'm better...
5
19600
by: Stacey Levine | last post by:
I have a webservice that I wanted to return an ArrayList..Well the service compiles and runs when I have the output defined as ArrayList, but the WSDL defines the output as an Object so I was having a problem in the calling program. I searched online and found suggestions that I return an Array instead so I modified my code (below) to return an Array instead of an ArrayList. Now I get the message when I try to run just my webservice...
18
3254
by: Sam | last post by:
Hi All I'm planing to write an application which allows users dynamically add their points (say you can add upto 30,000) and then draw xy graph. Should I use an array for my coordinate point storage and dynamically resize it when there is a new point or should I use ArrayList? Is speed noticable between the two? Regards,
24
4399
by: RyanTaylor | last post by:
I have a final coming up later this week in my beginning Java class and my prof has decided to give us possible Javascript code we may have to write. Problem is, we didn't really cover JS and what we covered was within the last week of the class and all self taught. Our prof gave us an example of a Java method used to remove elements from an array: public void searchProcess() { int outIt=0;
5
28393
by: Paulers | last post by:
Hello all, I have a string array with duplicate elements. I need to create a new string array containing only the unique elements. Is there an easy way to do this? I have tried looping through each element but I am having issues using redim to adjust the new array. Any help or example code would be greatly appreciated. thanks!
12
14451
by: Maxwell2006 | last post by:
Hi, I declared an array like this: string scriptArgs = new string; Can I resize the array later in the code? Thank you, Max
9
5684
by: Brian Tkatch | last post by:
I'm looking for a simple way to unique an array of strings. I came up with this. Does it make sense? Am i missing anything? (Testing seems to show it to work.) Public Function Unique(ByVal List() As String) As String() ' Returns the unique values of in array, in an array. Dim Temp As New System.Collections.Specialized.StringCollection()
0
9425
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
0
10059
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that captivates audiences and drives business growth. The Art of Business Website Design Your website is...
1
10005
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For most users, this new feature is actually very convenient. If you want to control the update process,...
0
9871
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
1
7416
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes instead of User Defined Types (UDT). For example, to manage the data in unbound forms. Adolph will...
0
6679
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert into image. Globals.ThisAddIn.Application.ActiveDocument.Select();...
0
5313
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in the same network. But I'm wondering if it's possible to do the same thing, with 2 Pfsense firewalls...
0
5452
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
3
2817
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating effective websites that not only look great but also perform exceptionally well. In this comprehensive...

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.