Want to win a PS4? Go Premium and enter to win our High-Tech Treats giveaway. Enter to Win

x
Solved

# How to Assign ArrayList element to Integer array

Posted on 2003-10-31
Medium Priority
1,112 Views
Hi,
The code is given below

ArrayList aSequenceList= new ArrayList();

(The above arraylist contain list like 23,45,67,89) I wants to assign these elements to integer array. For this the code which i written.

int iArrSeq;

for (int i=0; i< aSequenceList.Count; i++)
{
iArrSeq[i]=aSequenceList[i];    Not working
iArrSeq[i]=int.parse(aSequenceList[i]); Not Working
}
gives error like "Cannot implicitly convert type 'object' to 'int'"

Please give me any suggestion or solution.

Thanks,

0
Question by:manjiri_j
[X]
###### Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

• Help others & share knowledge
• Earn cash & points
• 4
• 3
• 2
• +2

LVL 5

Expert Comment

ID: 9657051
Try the following line

iArrSeq = int.Parse(aSequenceList[i].ToString());

Tom
0

LVL 10

Expert Comment

ID: 9657601

Otherwise, a tidier solution, which prevents you from having to go through the whole array is:-

iArrSeq=(int[])aSequenceList.ToArray(typeof(Int32));

0

LVL 20

Expert Comment

ID: 9661354
Here is the code fixed:

ArrayList aSequenceList= new ArrayList();

int[] iArrSeq = new int[aSequenceList.Count];

for (int i=0; i< aSequenceList.Count; i++)
{
iArrSeq[i]=(int)aSequenceList[i];
}
0

LVL 3

Expert Comment

ID: 9664239
int[] iArr = (int[]) myList.ToArray(typeof(int))
0

LVL 10

Expert Comment

ID: 9665382
infinity88... how is that different to my suggestion ???
0

LVL 3

Expert Comment

ID: 9666943
You used Int32 -- why refer directly to the framework class for a primative type?

I dont expect points if thats what you're worried about.
0

LVL 10

Expert Comment

ID: 9670768
Ok, fair enough, but int & Int32 are the same thing. 'int' is just an alias for the 'framework class' System.Int32

for some reason, when I'm casting something, I always use 'int' - maybe it's just from my c++ days.
0

LVL 3

Accepted Solution

Infinity88 earned 375 total points
ID: 9672522
Agreed that it doesnt matter -- but for some reason it bugs me when I see ppl referring directly to the primative classes. Even when they use "String" instead of "string". For one, the syntax highlighter doesnt catch it -- and secondly, it makes the code look more like VB.
0

LVL 10

Expert Comment

ID: 9677366
wow, very diplomatic manjiri_j.

1. You gave a grade 'b' for a solution to your problem.. why not an 'a' ?? If there is a problem with the solution provided, please state what it is, the experts here put their time and effort into trying to help you, the least you can do is post a brief comment explaining the reason for the low grade.

2. You have only graded 1 out of your 10 open questions.

3. Did you actually read the last few posts where Infinity88 agreed that his solution was the same as mine ?? Usually you accept the first correct answer.

Smg.
0

Author Comment

ID: 9678256
Hi,

I very sorry to say about grading. That should be grade "A" only. I really appreciate your efforts and thanks for that. And i hope you co-operate me, same in the future.

Thanks,
0

Author Comment

ID: 9678259
Hi,

I >am< very sorry to say about grading. That should be grade "A" only. I really appreciate your efforts and thanks for that. And i hope you co-operate me, same in the future.

Thanks,
0

## Featured Post

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

This article is for Object-Oriented Programming (OOP) beginners. An Interface contains declarations of events, indexers, methods and/or properties. Any class which implements the Interface should provide the concrete implementation for each Interâ€¦
Performance in games development is paramount: every microsecond counts to be able to do everything in less than 33ms (aiming at 16ms). C# foreach statement is one of the worst performance killers, and here I explain why.
How to fix incompatible JVM issue while installing Eclipse While installing Eclipse in windows, got one error like above and unable to proceed with the installation. This video describes how to successfully install Eclipse. How to solve incompaâ€¦
Please read the paragraph below before following the instructions in the video â€” there are important caveats in the paragraph that I did not mention in the video. If your PaperPort 12 or PaperPort 14 is failing to start, or crashing, or hanging, â€¦
###### Suggested Courses
Course of the Month9 days, 14 hours left to enroll