473,394 Members | 1,800 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,394 software developers and data experts.

arraylist - how do I retrieve a single element?

This is driving me crazy.

I have defined an arraylist as follows:

public ArrayList drawnballs;

This is instantiated through

drawnballs = new ArrayList();

I add balls to this just fine with:

drawnballs.Add(myball);

I can access every element (every ball) through a foreach statement, that
works great.

What I can't do is get just the first ball (or any individual ball).

anotherball = drawnballs[0] doesn't work. There is no method like
anotherball = drawnballs.Item[1]; which is what I would expect. I can't get
the drawnballs.getarray method to work.

Please, please, how do I reference a single ball in drawnballs (ie in an
arraylist)?

Nov 5 '07 #1
3 1599
On Nov 5, 11:54 am, "Peter Webb"
<webbfam...@DIESPAMDIEoptusnet.com.auwrote:
This is driving me crazy.
<snip>
What I can't do is get just the first ball (or any individual ball).

anotherball = drawnballs[0] doesn't work.
It does, but being a weakly typed ArrayList you need to cast it:

anotherball = (MyBall) drawnballs[0];

Are you still using C# 1? If you're using C# 2 (and .NET 2.0) then
you'd be better off using List<T>.

Jon

Nov 5 '07 #2
Exactly what I needed to know. The web pages you provided were great; I
browsed some of the other pages and picked up on a lot of other stuff that
helps explains what going on.

Its a long learning curve from 6502 assembler to C#, and I have enjoyed
almost every minute of it so far.

Thanks for your help, and good luck with your book (if you decide to
publish).
Nov 6 '07 #3
Peter Webb <we********@DIESPAMDIEoptusnet.com.auwrote:
Exactly what I needed to know. The web pages you provided were great; I
browsed some of the other pages and picked up on a lot of other stuff that
helps explains what going on.
Excellent. Do ask again if any of it becomes difficult again. I've
found that I sometimes think I understand something, and then when I
try to use it something else goes wrong!
Its a long learning curve from 6502 assembler to C#, and I have enjoyed
almost every minute of it so far.
It's certainly a lot more productive than assembler :)
Thanks for your help, and good luck with your book (if you decide to
publish).
Oh yes, it's due out in March.

--
Jon Skeet - <sk***@pobox.com>
http://www.pobox.com/~skeet Blog: http://www.msmvps.com/jon.skeet
If replying to the group, please do not mail me too
Nov 6 '07 #4

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

Similar topics

3
by: Fred | last post by:
I'm trying to build a hashtable and a arraylist as object value I'm not able to retrieve stored object from the hashtable. Hashtable mp = new Hashtable(); // THE HASHTABLE ArrayList...
18
by: JohnR | last post by:
From reading the documentation, this should be a relatively easy thing. I have an arraylist of custom class instances which I want to search with an"indexof" where I'm passing an instance if the...
14
by: budy_ludy | last post by:
Hi All, I am new to vb .net, I have an ArrayList and i store class objects in it, and later i want to retrieve each ArrayList items and type cast to the class, How can it be done ? I used...
1
by: =?Utf-8?B?SkI=?= | last post by:
Hello My pgm1 (User Interface Level) passes an empty ArrayList to pgm2 (Business Logic Level). pgm2 then calls pgm3 (Data Access Level) to populate the ArrayList. Question1: When pgm2 gets...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
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...
0
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...

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.