473,472 Members | 2,168 Online
Bytes | Software Development & Data Engineering Community
Create Post

Home Posts Topics Members FAQ

arraylist of integer array

1 New Member
Hi
I'm wondering if anyone can help me about this problem:
I want to make two dimension integer array but the number of rows isn't determined,so I tried arraylist.In this case I don't know how to declare and what to do when I want to retrieve the inserted data like which was in 2d arrays (like integerarray{i][j]).
Thanks...
Mar 24 '10 #1
2 2161
Christian Binder
218 Recognized Expert New Member
Hi!
You could create your own class and take usage of the this[]-Property/Indexer.

I've made a little default implementation ...

Expand|Select|Wrap|Line Numbers
  1. public class MyArray<T> {
  2.     public Dictionary<int, T> _list { get; set; }
  3.     private T _default;
  4.  
  5.     public T this[int index] {
  6.       get{
  7.         if (_list == null || !_list.ContainsKey(index))
  8.           return _default;
  9.         return _list[index];
  10.       }
  11.  
  12.       set{
  13.         if (_list == null)
  14.           _list = new Dictionary<int, T>();
  15.  
  16.         if (_list.ContainsKey(index))
  17.           _list[index] = value;
  18.         else
  19.           _list.Add(index, value);
  20.       }
  21.     }
  22.   }
  23.  
And you can use this class that way:

Expand|Select|Wrap|Line Numbers
  1.  
  2. MyArray<MyArray<int>> integerarray = new MyArray<MyArray<int>>();
  3. integerarray[1][1] = 10;
  4.  
I hope this works, I haven't tested it yet :-)
Mar 24 '10 #2
jkmyoung
2,057 Recognized Expert Top Contributor
Not sure that helps. Thought the point was to be able to add and resize the array.
Expand|Select|Wrap|Line Numbers
  1.             ArrayList al = new ArrayList();
  2.  
  3.  
  4.             for (int i = 0; i < 10; i++)
  5.             {
  6.                 al.Add(new int[10]);
  7.             }
  8.  
  9.             int av = ((int[])(al[3]))[2];
  10.  
The accessing with your current method is somewhat ugly, since you have to cast the temporary result to an int[].

I think you probably want a List<int[]> instead: eg
Expand|Select|Wrap|Line Numbers
  1.             List<int[]> intList = new List<int[]>();
  2.             for (int i = 0; i < 10; i++)
  3.             {
  4.                 intList.Add(new int[10]);
  5.             }
  6.             int av = intList[3][2];
  7.  
You have to know the number of columns beforehand, unless you want to use an List of List<int>.
Mar 24 '10 #3

Sign in to post your reply or Sign up for a free account.

Similar topics

4
by: tma | last post by:
I'm trying to save code to a session object like the following: dim oAppList as arraylist dim oApp as someclass Code to manipulate oApp... .... .... oApplist.Add(oApp)...
9
by: Leon | last post by:
I have a webform in which when the user press generate button the form generate six unique ramdon numbers, the user can also type these six numbers in manually in any order. however, the user can...
4
by: Mike | last post by:
I have declared two classes. The first class has 4 private variables. Each has a property defined. I'm calling a readfile sub from a second class. The second class also has an Arraylist...
6
by: rdi | last post by:
I have 3 classes: mailBox, mailFilter & mailFilterSet I then have an arraylist. Each element in the array is of type mailBox (variable name is mbox). One item of the mailbox class is an...
2
by: rdi | last post by:
I posted this previously--but don't know if it actually posted. So I'm trying again. Sorry if it IS a double post. TIA =================================== I have 3 classes: mailBox,...
4
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...
20
by: Dennis | last post by:
I use the following code for a strongly typed arraylist and it works great. However, I was wondering if this is the proper way to do it. I realize that if I want to implement sorting of the...
18
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...
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...
1
by: nondos | last post by:
I have problem with arraylist the object i get from the array is always the last i'll give example: Public Class Form1 Private Sub Form1_Load(ByVal sender As Object, ByVal e As...
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
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,...
1
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...
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
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...
0
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...
0
muto222
php
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.

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.