473,401 Members | 2,068 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,401 software developers and data experts.

ArrayList.ToArray question

GB
Hello:
I have ArrayList strat_list of integers like this:

index value

[0] 3
[1] 4
[2] 2
[3] 1
[4] 6
[5] 4
[6] 3
[7] 2
[8] 5
[9] 8
[10] 6
[11] 2

I want to put the content of the list in 2 dimensional array to get a
matrix like this:
3421
6432
5862
This is what I am doing:
int k = 4;
int[,] myArr = new int[strat_list.Count,k];
myArr = (int[,])strat_list.ToArray(typeof(int[,]));

But it gives me InvalidCastException error.
Could you give me a hint how to fix the problem?

Thanks,
GB


Aug 29 '06 #1
4 1612

GB wrote:
Hello:
I have ArrayList strat_list of integers like this:

index value

[0] 3
[1] 4
[2] 2
[3] 1
[4] 6
[5] 4
[6] 3
[7] 2
[8] 5
[9] 8
[10] 6
[11] 2

I want to put the content of the list in 2 dimensional array to get a
matrix like this:
3421
6432
5862
This is what I am doing:
int k = 4;
int[,] myArr = new int[strat_list.Count,k];
myArr = (int[,])strat_list.ToArray(typeof(int[,]));

But it gives me InvalidCastException error.
Could you give me a hint how to fix the problem?
You have to write the loops yourself; this is not such a common problem
that the Framework methods will do it for you.

for (int i = 0; i < (strat_list.Count + 3) / 4; i++)
{
for (int j = 0; j < 3; j++)
{
int k = i * 4 + j;
if (k < strat_list.Count)
{
myArr[i, j] = strat_list[k];
}
else
{
myArr[i, j] = 0; // or whatever the default should be
}
}
}

Aug 29 '06 #2
GB
Thank you,
It gives me the 2 dimensional array of 12 by 4.
Can I get the array of 3 by 4 ( to eleminate rows with 0's)?

GB

"Bruce Wood" <br*******@canada.comwrote in message
news:11**********************@h48g2000cwc.googlegr oups.com...
>
GB wrote:
Hello:
I have ArrayList strat_list of integers like this:

index value

[0] 3
[1] 4
[2] 2
[3] 1
[4] 6
[5] 4
[6] 3
[7] 2
[8] 5
[9] 8
[10] 6
[11] 2

I want to put the content of the list in 2 dimensional array to get a
matrix like this:
3421
6432
5862
This is what I am doing:
int k = 4;
int[,] myArr = new int[strat_list.Count,k];
myArr = (int[,])strat_list.ToArray(typeof(int[,]));

But it gives me InvalidCastException error.
Could you give me a hint how to fix the problem?

You have to write the loops yourself; this is not such a common problem
that the Framework methods will do it for you.

for (int i = 0; i < (strat_list.Count + 3) / 4; i++)
{
for (int j = 0; j < 3; j++)
{
int k = i * 4 + j;
if (k < strat_list.Count)
{
myArr[i, j] = strat_list[k];
}
else
{
myArr[i, j] = 0; // or whatever the default should be
}
}
}

Aug 29 '06 #3

Bruce Wood wrote:
GB wrote:
Hello:
I have ArrayList strat_list of integers like this:

index value

[0] 3
[1] 4
[2] 2
[3] 1
[4] 6
[5] 4
[6] 3
[7] 2
[8] 5
[9] 8
[10] 6
[11] 2

I want to put the content of the list in 2 dimensional array to get a
matrix like this:
3421
6432
5862
This is what I am doing:
int k = 4;
int[,] myArr = new int[strat_list.Count,k];
myArr = (int[,])strat_list.ToArray(typeof(int[,]));

But it gives me InvalidCastException error.
Could you give me a hint how to fix the problem?

You have to write the loops yourself; this is not such a common problem
that the Framework methods will do it for you.

for (int i = 0; i < (strat_list.Count + 3) / 4; i++)
{
for (int j = 0; j < 3; j++)
{
int k = i * 4 + j;
if (k < strat_list.Count)
{
myArr[i, j] = strat_list[k];
}
else
{
myArr[i, j] = 0; // or whatever the default should be
}
}
}
Sorry... should have been "for (int j = 0; j < 4; j++)" not j < 3.

Aug 29 '06 #4
The problem here is most likely in your array allocation, at the start.
Try this:

int numRows = (strat_list.Count + 3) / 4;
int[,] myArr = new int[numRows,4];
for (int i = 0; i < numRows; i++)
....

(Instead of "for (int i = 0; i < (strat_list.Count + 3) / 4; i++)".
Also, note my other post that the "j" loop should have been "for (int j
= 0; j < 4; j++)", not j < 3.)

GB wrote:
Thank you,
It gives me the 2 dimensional array of 12 by 4.
Can I get the array of 3 by 4 ( to eleminate rows with 0's)?

GB

"Bruce Wood" <br*******@canada.comwrote in message
news:11**********************@h48g2000cwc.googlegr oups.com...

GB wrote:
Hello:
I have ArrayList strat_list of integers like this:
>
index value
>
[0] 3
[1] 4
[2] 2
[3] 1
[4] 6
[5] 4
[6] 3
[7] 2
[8] 5
[9] 8
[10] 6
[11] 2
>
I want to put the content of the list in 2 dimensional array to get a
matrix like this:
3421
6432
5862
This is what I am doing:
int k = 4;
int[,] myArr = new int[strat_list.Count,k];
myArr = (int[,])strat_list.ToArray(typeof(int[,]));
>
But it gives me InvalidCastException error.
Could you give me a hint how to fix the problem?
You have to write the loops yourself; this is not such a common problem
that the Framework methods will do it for you.

for (int i = 0; i < (strat_list.Count + 3) / 4; i++)
{
for (int j = 0; j < 3; j++)
{
int k = i * 4 + j;
if (k < strat_list.Count)
{
myArr[i, j] = strat_list[k];
}
else
{
myArr[i, j] = 0; // or whatever the default should be
}
}
}
Aug 29 '06 #5

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

Similar topics

2
by: Mark Oueis | last post by:
ArrayList arrayList = new ArrayList(); foreach (Blabla bla in Blablas) { arrayList.Add(Convert.ToInt32(blabla.something)); } so far this works great.. but now
2
by: Sunny | last post by:
Hi, I have a small function, that returns a string, inside the function, there is an arraylist, being processed. At the end of the function all the elements of the ArrayList are copied to my...
4
by: Derrick | last post by:
If i have an ArrayList, add a bunch of MyClass classes to it, how do I get a MyClass out of it? I've tried: MyClass clses = ArrayList.ToArray(typeof(MyClass)); but get errors about not being...
4
by: Hans De Schrijver | last post by:
I have a private ArrayList variable that holds objects of various types, though they're all derived from a common base class (User). What I would like to do is provide public accessor properties...
1
by: Jack Addington | last post by:
I have a method that takes an array of nodes (UltraNode) as a parameter. I've got an arrayList of objects that I am trying to convert to UltraNodes and pass to the method but I cannot seem to get...
2
by: Dirk | last post by:
Hello The following code does not work ArrayList __gc* allControls = new ArrayList(); ..... // put some Controls in allControls .... Control __gc* arr = dynamic_cast<Control...
2
by: Russ Green | last post by:
I am currently working on a small VB.NET utility that accesses the Microstation VBA object model via COM. I have this code which I am trying to get to work... The key bits of information are ...
48
by: Alex Chudnovsky | last post by:
I have come across with what appears to be a significant performance bug in ..NET 2.0 ArrayList.Sort method when compared with Array.Sort on the same data. Same data on the same CPU gets sorted a...
3
by: Michael Howes | last post by:
I have many double that each have a few thousand numbers in them. I need to concatenate groups of these double arrays into a new set of double. I don't know the total # of points. I thought it...
3
by: wolima | last post by:
I have to create a arraylist of arrays. So, at the end, I have to convert this arraylist in a two dimmensional array. I wrote something like this. ArrayList myAL = new ArrayList(); double...
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
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:
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...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
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...
0
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...
0
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...

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.