473,938 Members | 41,097 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 1832
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
1900
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
2868
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
5396
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
2833
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
4776
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
3270
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
3154
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
2641
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
3523
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
10131
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
11516
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, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. Here is my compilation command: g++-12 -std=c++20 -Wnarrowing bit_field.cpp Here is the code in...
1
11284
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
9854
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, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then launch it, all on its own.... Now, this would greatly impact the work of software developers. The idea...
1
8210
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
7377
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert into image. Globals.ThisAddIn.Application.ActiveDocument.Select();...
0
6072
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
6290
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
3
3496
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.