PC Review


Reply
Thread Tools Rate Thread

ArrayList.ToArray question

 
 
GB
Guest
Posts: n/a
 
      29th Aug 2006
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




 
Reply With Quote
 
 
 
 
Bruce Wood
Guest
Posts: n/a
 
      29th Aug 2006

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
}
}
}

 
Reply With Quote
 
GB
Guest
Posts: n/a
 
      29th Aug 2006
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" <(E-Mail Removed)> wrote in message
news:(E-Mail Removed)...
>
> 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
> }
> }
> }
>



 
Reply With Quote
 
Bruce Wood
Guest
Posts: n/a
 
      30th Aug 2006

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.

 
Reply With Quote
 
Bruce Wood
Guest
Posts: n/a
 
      30th Aug 2006
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" <(E-Mail Removed)> wrote in message
> news:(E-Mail Removed)...
> >
> > 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
> > }
> > }
> > }
> >


 
Reply With Quote
 
 
 
Reply

Thread Tools
Rate This Thread
Rate This Thread:

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
ArrayList.ToArray to string array question Walker Moore Microsoft C# .NET 1 18th Aug 2011 10:01 PM
ArrayList.toArray() --> int[] ChuckD_Duncan Microsoft Dot NET Framework 1 25th Aug 2005 09:04 PM
Problems with ArrayList::ToArray Dirk Microsoft VC .NET 2 1st Aug 2004 07:48 PM
ArrayList.ToArray() question Derrick Microsoft C# .NET 4 2nd Mar 2004 08:38 PM
Re: ArrayList ToArray issues Jay B. Harlow [MVP - Outlook] Microsoft VB .NET 32 27th Aug 2003 10:10 AM


Features
 

Advertising
 

Newsgroups
 


All times are GMT +1. The time now is 10:56 AM.