473,706 Members | 2,640 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

Arraylist of Arraylist

Hello,

I've created an arraylist which consist arraylist ..like

ArrayList myAL = new ArrayList(); // Main Array List
ArrayList tmpAL = new ArrayList(); //Temp Array List
tmpAL.Add(2); //Add int to Temp AL
tmpAL.Add(3); //Add int to Temp AL
myAL.Add(tmpAL) ; //Add Temp AL to Main AL
//(and so on)
..
int[] data = (int[])myAL[0].ToArray(typeof (int); // something like this
...i have to access the element in Main arraylist whicj are itself an
arraylist and then convert them into int[].

now i have to convert myAL to int[] .. problem is in converting object
to Array, any hints?

Jan 25 '06 #1
6 1817
Iapain,
now i have to convert myAL to int[] .. problem is in converting object
to Array, any hints?


you have to cast the object you get from the main arraylist, which is an
arraylist, as an arraylist which can be converted to an array

it sounds hard but is is as simple as this:

(int[])((ArrayList)ma inAL[i]).ToArray(typeo f(int);

(ArrayList)main AL[i] means: get the i-th element from main array and
cast it to an ArrayList, the result is the i-th arraylist in your mainAL.

hope this will be clare enough
Jan 25 '06 #2
ArrayList temp = (ArrayList)myAL[0];
int[] data = new int[temp.Count]
temp.CopyTo(dat a);

Ths last statement throws an InvalidCastExce ption in case the elements in
the source ArrayList cannot be cast to the type of the destination array.

--
Kai Brinkmann [MSFT]

Please do not send e-mail directly to this alias. This alias is for
newsgroup purposes only.
This posting is provided "AS IS" with no warranties, and confers no rights.
"Iapain" <ia****@gmail.c om> wrote in message
news:11******** **************@ g47g2000cwa.goo glegroups.com.. .
Hello,

I've created an arraylist which consist arraylist ..like

ArrayList myAL = new ArrayList(); // Main Array List
ArrayList tmpAL = new ArrayList(); //Temp Array List
tmpAL.Add(2); //Add int to Temp AL
tmpAL.Add(3); //Add int to Temp AL
myAL.Add(tmpAL) ; //Add Temp AL to Main AL
//(and so on)
.
int[] data = (int[])myAL[0].ToArray(typeof (int); // something like this
..i have to access the element in Main arraylist whicj are itself an
arraylist and then convert them into int[].

now i have to convert myAL to int[] .. problem is in converting object
to Array, any hints?

Jan 26 '06 #3

"Iapain" <ia****@gmail.c om> wrote in message
news:11******** **************@ g47g2000cwa.goo glegroups.com.. .
Hello,

I've created an arraylist which consist arraylist ..like

ArrayList myAL = new ArrayList(); // Main Array List
ArrayList tmpAL = new ArrayList(); //Temp Array List
tmpAL.Add(2); //Add int to Temp AL
tmpAL.Add(3); //Add int to Temp AL
myAL.Add(tmpAL) ; //Add Temp AL to Main AL
//(and so on)
.
int[] data = (int[])myAL[0].ToArray(typeof (int); // something like this
..i have to access the element in Main arraylist whicj are itself an
arraylist and then convert them into int[].

now i have to convert myAL to int[] .. problem is in converting object
to Array, any hints?


Apart from all the other stuff that people have responded you should never
right ugly stuff like this.

The way to go is to write your own matrix class.
The underlying implementation can use ArrayList but by using your own class
you will acheive 3 benefits:

1) It's easier for other to use and understand.
2) It can be typesafe
3) It helps you to think more clearly about the types involved than trying
to stuff it all into a single complicated expression with a lot of casting.
[For example the casting to ArrayList would have been obvious]
Jan 26 '06 #4
Both Rudderius and Kai suggested an excellent way to use Arraylist of
Arraylist, Nick i your suggestion is excellent too ..Thanks it
certainly helped me a lot. I've one more question may be its stupid but
its happening with me .. If i clear tempAL like below

ArrayList myAL = new ArrayList(); // Main Array List
ArrayList tmpAL = new ArrayList(); //Temp Array List
tmpAL.Add(2); //Add int to Temp AL
tmpAL.Add(3); //Add int to Temp AL
myAL.Add(tmpAL) ; //Add Temp AL to Main AL
tmpAL.Clear() // clear temp AL

now i am clearing the tmpAL after adding it to MainAL ..would the
content of MainAL have tmpAL? I've tried and their is no content in
MainAL ?? any solution?

Jan 26 '06 #5
Iapain wrote:
Both Rudderius and Kai suggested an excellent way to use Arraylist of
Arraylist, Nick i your suggestion is excellent too ..Thanks it
certainly helped me a lot. I've one more question may be its stupid but
its happening with me .. If i clear tempAL like below

ArrayList myAL = new ArrayList(); // Main Array List
ArrayList tmpAL = new ArrayList(); //Temp Array List
tmpAL.Add(2); //Add int to Temp AL
tmpAL.Add(3); //Add int to Temp AL
myAL.Add(tmpAL) ; //Add Temp AL to Main AL
tmpAL.Clear() // clear temp AL

now i am clearing the tmpAL after adding it to MainAL ..would the
content of MainAL have tmpAL? I've tried and their is no content in
MainAL ?? any solution?


You need to be aware of reference type semantics. ArrayLists store
references to objects, so whatever you do to tmpAL (while it's still
referring to the same object) is visible via myAL[0], because they're
both references to the same object. To put this in a slightly simpler
context (with only one ArrayList involved) if you do:

ArrayList list1 = new ArrayList();
ArrayList list2 = list1;

list2.Add("Hell o");
Console.WriteLi ne (list1.Count);

the output will be 1, because the values of list1 and list2 are
references to the same object.

Jon

Jan 26 '06 #6
Absolutly correct Jon, thank you guys! i've solved my problem.

Jan 26 '06 #7

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

Similar topics

7
455
by: Alex Ting | last post by:
Hi Everybody, I have an issue about deleting an object from an arrayList. I've bounded a datagrid using this code where it will first run through all of the code in loadQuestions() and bind a arrayList to the datagrid. private void BindQuestions()
3
1891
by: george r smith | last post by:
I am trying to create an arrayList that contains multiple arrayLists. My code attempt is below. The question I have is how can I get away from creating another pAttribute list than can be added to pItem. As I understand it (and I coded it to test) if I try to use pAttribute again by clearing with pAttribute.Clear because it is a shallow copy the pItem is also cleared. I guess I need a "deep copy" but can't find any documentation on it. ...
3
2855
by: Stephen | last post by:
I was wondering if someone can help me with an web application design problem. I have a aspx page which builds up an arraylist called addresses and outputs the values in the arraylist items to a datagrid. I am using the viewstate object to store the Arraylist items on the page on postback. My PROBLEM is that I need to redirect the user to a new aspx page and on this new page i need to be able to access the items in my arraylist. Is this...
6
5383
by: gane kol | last post by:
Hi, I have a code that creates a datatable from an arraylist, but i am getting an error in casting in for (int intRow = 0; intRow < alLCPlist.Count; intRow++) { DataRow drow = dtLCPack.NewRow(); int intColCount = dtLCPack.Columns.Count; ArrayList arrlRow = (ArrayList)alLCPlist; <== Specified cast
4
2814
by: Peter | last post by:
I run into this situation all the time and I'm wondering what is the most efficient way to handle this issue: I'll be pulling data out of a data source and want to load the data into an array so that I can preform complicated operations against this data. The returned record count in these operations is always variable. 1. I have been using an arraylist.add function to handle non-multidemional returns but was wondering if I'm better...
18
4740
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 class where only the "searched" property has a value. I expected to get the index into the arraylist where I could then get the entire class instance. However, the 'indexof' is never calling my overloaded, overrides Equals method. Here is the...
18
3250
by: Sam | last post by:
Hi All I'm planing to write an application which allows users dynamically add their points (say you can add upto 30,000) and then draw xy graph. Should I use an array for my coordinate point storage and dynamically resize it when there is a new point or should I use ArrayList? Is speed noticable between the two? Regards,
6
3138
by: fniles | last post by:
I am using VB.NET 2003 and a socket control to receive and sending data to clients. As I receive data in 1 thread, I put it into an arraylist, and then I remove the data from arraylist and send it to the client. Before adding data to the arraylist, I check if the depth of the arraylist is longer than iMaxQueueDepth, and if it is, I clear the arraylist. Is it possible that while I am clearing the arraylist, the ThreadMain at the same time...
3
2631
by: Christopher H | last post by:
I've been reading about how C# passes ArrayLists as reference and Structs as value, but I still can't get my program to work like I want it to. Simple example: code:------------------------------------------------------------------------------ class Program { static public ArrayList MyArrayList = new ArrayList();
1
3504
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 the ArrayList back from pgm3 how to extract and separate the fields out fo the ArrayLists? Question2: When pgm3 gets ArrayList back from pgm2 how to separate the
0
8772
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, people are often confused as to whether an ONU can Work As a Router. In this blog post, weíll explore What is ONU, What Is Router, ONU & Routerís main usage, and What is the difference between ONU and Router. Letís take a closer look ! Part I. Meaning of...
0
8692
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 effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
1
9038
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 Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For most users, this new feature is actually very convenient. If you want to control the update process,...
0
8982
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 protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
1
6607
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 presenter, Adolph Duprť who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes instead of User Defined Types (UDT). For example, to manage the data in unbound forms. Adolph will...
0
4441
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in the same network. But I'm wondering if it's possible to do the same thing, with 2 Pfsense firewalls...
0
4705
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
2
2493
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
3
2084
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating effective websites that not only look great but also perform exceptionally well. In this comprehensive...

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.