Home | Contact Us | FAQ | Search & Site Map | Link to Us
Sign In | Join | Other 45 Sites in Network
HomeAnnouncementsFree MagazinesWhite PapersSubmit Content
Discussion GroupsASP.NETWindows FormsLanguages.NET FrameworkVisual Studio.NET
Articles.NET FrameworkASP.NETToolsWindows Forms
.NET DirectoryOpen Source ProjectsUser GroupsWeb Resources
Related Topics
Visual Basic 6SQL ServerMS AccessOther DB ProductsMS Server ProductsMore Topics ...

.NET Forum / Languages / C# / August 2006

Tip: Looking for answers? Try searching our database.

ArrayList.ToArray question

Thread view: 
Enable EMail Alerts  Start New Thread
Thread rating: 
GB - 29 Aug 2006 22:06 GMT
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
Bruce Wood - 29 Aug 2006 22:27 GMT
> Hello:
> I have ArrayList strat_list of integers like this:
[quoted text clipped - 26 lines]
> 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
       }
   }
}
GB - 29 Aug 2006 23:15 GMT
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

> > Hello:
> > I have ArrayList strat_list of integers like this:
[quoted text clipped - 45 lines]
>     }
> }
Bruce Wood - 30 Aug 2006 00:43 GMT
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.)

> Thank you,
> It gives me the 2 dimensional array of 12 by 4.
[quoted text clipped - 51 lines]
> >     }
> > }
Bruce Wood - 30 Aug 2006 00:25 GMT
> > Hello:
> > I have ArrayList strat_list of integers like this:
[quoted text clipped - 45 lines]
>     }
> }

Sorry... should have been "for (int j = 0; j < 4; j++)" not j < 3.

Free Magazines

Get these publications absolutely FREE for up to 12 months. There are no hidden fees and no obligation. Simply choose a title, complete the application form and submit it. Read more ...

Oracle MagazineNetwork ComputingComputer WorldBio-IT WorldeWeekInformation WeekInfosecurity
 
Sign In
Join
My Latest Posts
My Monitored Threads
My Blog
My Photo Gallery
My Profile
My Homepage

Start New Thread
Enable EMail Alerts
Rate this Thread



©2008 Advenet LLC   Privacy Policy - Terms of Use
This website includes both content owned or controlled by Advenet as well as content owned or controlled by third parties.