473,395 Members | 1,458 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,395 software developers and data experts.

Two-Dimensional Array

I am trying to return a two-dimensional array with data but am having some problems retreving the data. Can someone guide me what I should change in this code?

Thanks

Expand|Select|Wrap|Line Numbers
  1. public ArrayList getData()
  2. {
  3.     ArrayList data = new ArrayList();
  4.     ArrayList myList1 = new ArrayList();
  5.     ArrayList myList2 = new ArrayList();
  6.     int i;
  7.  
  8.     for(i=0; i<5; i++)
  9.     {
  10.         i++;
  11.         myList1.Add("San");
  12.     }
  13.  
  14.     for(i=0; i<5; i++)
  15.     {
  16.         i++;
  17.         myList2.Add("Diego");
  18.     }
  19.  
  20.     data.Add(myList1);
  21.     data.Add(myList2);
  22.  
  23. return data;
method for retreving the data

Expand|Select|Wrap|Line Numbers
  1. ArrayList Data = new ArrayList();
  2. DatabaseAccessLayer DAL = new DatabaseAccessLayer();
  3. data = DAL.getData;
  4.  
  5. for(int i=0; i<data.Count; i++)
  6. {
  7.     Data = ((ArrayList)(data[i]));
  8.     Console.Write((string)(Data[0]));
  9.     Console.Write((string)(Data[1]));
  10. }
Aug 7 '07 #1
4 1168
Plater
7,872 Expert 4TB
You are itterating your counter (i++) two times in each loop in your populating thing.

Otherwise I don't see why it wouldn't work
Aug 7 '07 #2
r035198x
13,262 8TB
I am trying to return a two-dimensional array with data but am having some problems retreving the data. Can someone guide me what I should change in this code?

Thanks

Expand|Select|Wrap|Line Numbers
  1. public ArrayList getData()
  2. {
  3.     ArrayList data = new ArrayList();
  4.     ArrayList myList1 = new ArrayList();
  5.     ArrayList myList2 = new ArrayList();
  6.     int i;
  7.  
  8.     for(i=0; i<5; i++)
  9.     {
  10.         i++;
  11.         myList1.Add("San");
  12.     }
  13.  
  14.     for(i=0; i<5; i++)
  15.     {
  16.         i++;
  17.         myList2.Add("Diego");
  18.     }
  19.  
  20.     data.Add(myList1);
  21.     data.Add(myList2);
  22.  
  23. return data;
method for retreving the data

Expand|Select|Wrap|Line Numbers
  1. ArrayList Data = new ArrayList();
  2. DatabaseAccessLayer DAL = new DatabaseAccessLayer();
  3. data = DAL.getData;
  4.  
  5. for(int i=0; i<data.Count; i++)
  6. {
  7.     Data = ((ArrayList)(data[i]));
  8.     Console.Write((string)(Data[0]));
  9.     Console.Write((string)(Data[1]));
  10. }
Note that a two-dim array is not the same as an arraylist of arraylists.
You should also consider using a typed collection.
Aug 7 '07 #3
Thanks. I've rearranged the code and it now returns data. This is an arraylist made up of several arraylists.

How could I organize the data to return in this manner:

A1x
B2x
C3x
and so on.

Expand|Select|Wrap|Line Numbers
  1. ArrayList data;
  2. ArrayList Data = new ArrayList();
  3. DatabaseAccessLayer DAL = new DatabaseAccessLayer();
  4. data = DAL.getData();
  5.  
  6. for(int i=0; i<data.Count; i++)
  7. {
  8.     Data = ((ArrayList)(data[i]));
  9.     Console.WriteLine((string)(Data[0]));
  10.     Console.WriteLine((string)(Data[1]));
  11.     Console.WriteLine((string)(Data[2]));
  12.     Console.WriteLine((string)(Data[3]));
  13.     Console.WriteLine((string)(Data[4]));
  14.     Console.WriteLine((string)(Data[5]));
  15.     Console.WriteLine((string)(Data[6]));
  16.     Console.WriteLine((string)(Data[7]));
  17. }
  18.  
Sample data

Expand|Select|Wrap|Line Numbers
  1. public ArrayList getData()
  2. {
  3.     ArrayList data = new ArrayList();
  4.  
  5.     ArrayList myList1 = new ArrayList();
  6.     ArrayList myList2 = new ArrayList();
  7.     ArrayList myList3 = new ArrayList();
  8.  
  9.     myList1.Add("A");
  10.     myList1.Add("B");
  11.     myList1.Add("C");
  12.     myList1.Add("D");
  13.     myList1.Add("E");
  14.     myList1.Add("F");
  15.     myList1.Add("G");
  16.     myList1.Add("H");
  17.  
  18.     myList2.Add("1");
  19.     myList2.Add("2");
  20.     myList2.Add("3");
  21.     myList2.Add("4");
  22.     myList2.Add("5");
  23.     myList2.Add("6");
  24.     myList2.Add("7");
  25.     myList2.Add("8");
  26.  
  27.     myList3.Add("x");
  28.     myList3.Add("x");
  29.     myList3.Add("x");
  30.     myList3.Add("x");
  31.     myList3.Add("x");
  32.     myList3.Add("x");
  33.     myList3.Add("x");
  34.     myList3.Add("x");
  35.  
  36.     data.Add(myList1);
  37.     data.Add(myList2);
  38.     data.Add(myList3);
  39.  
  40.     return data;
  41. }
  42.  
Aug 7 '07 #4
r035198x
13,262 8TB
Expand|Select|Wrap|Line Numbers
  1. for(int j = 0; j < ((ArrayList)list[0]).Count; j ++) {
  2.                 String val = "";
  3.                 for(int i =0; i < list.Count;i++) {
  4.                     val += (String)((ArrayList)list[i])[j]; 
  5.  
  6.                 }
  7.                 Console.WriteLine(val);    
  8.  
  9.             } 
Generic list doesn't require the ugly casts
Aug 7 '07 #5

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

Similar topics

0
by: SimonC | last post by:
I'm looking to do something similar to a feature found on Ticketmaster.com, where you select your seats at a venue, and then you have two minutes in which to take or leave them. QUESTION 1a....
8
by: John Grenier | last post by:
Hi, I have to determine the "standing" (WIN - TIE - LOSS) from confrontations between two teams on a contest. The table matchResults has fields cont_id, team_id and contest_result (int). ...
6
by: Willem | last post by:
Hi, I have a newbie question: is it possible to make a search form in asp that searches in two different databases (access)? Willem
10
by: Hank1234 | last post by:
Can I use one Data Adapter and one Command Builder to update amny tables? Currently in my data adapter I query two tables and fill them into two tables in a data set. When I make a change to a...
6
by: Matt K. | last post by:
Hi there, I have a form in an Access project that contains a subform which displays the results of a query of the style "select * from where = #a certain date#". In the main part of the form...
7
by: Prabhudhas Peter | last post by:
I have two object instances of a same class... and i assigned values in both object instances (or the values can be taken from databse and assigned to the members of the objects)... Now i want to...
0
by: clintonG | last post by:
I applied aspnet_regsql to SQL2K which was working fine throughout Beta 2 development. After installing Visual Studio and SQL Express RTM my application has blown up. Logging in to the application...
9
by: Steven | last post by:
Hello, I have a question about strcmp(). I have four words, who need to be compared if it were two strings. I tried adding the comparison values like '(strcmp(w1, w2) + strcmp(w3, w4))', where...
9
by: dhable | last post by:
I just started working with Python and ran into an annoyance. Is there a way to avoid having to use the "from xxx import yyy" syntax from files in the same directory? I'm sure it's been asked a...
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
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,...
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,...
0
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...

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.