473,503 Members | 2,159 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

Adding a 2D array to the list. Found a bug and solution, need your explanation

EARNEST
128 New Member
Working fine:

Expand|Select|Wrap|Line Numbers
  1.         internal double[] AggregationMethod(List<double[]> listOf_D)
  2.         {
  3.             double[] maxD;
  4.             double[] aggregatedD;
  5.             List<double[]> tempList;
  6.  
  7.             maxD = new double[listOf_D[0].Length];
  8.  
  9.             tempList = new List<double[]>();
  10.  
  11.             for (int i = 0; i < maxD.Length; i++) 
  12.             {
  13.  
  14.                 aggregatedD = new double[listOf_D.Count]; 
  15.  
  16.                 for (int j = 0; j < listOf_D.Count; j++) 
  17.                 {
  18.                     aggregatedD[j] = listOf_D[j][i];
  19.                 }
  20.                 tempList.Add(aggregatedD);
  21.  
  22.                 maxD[i] = aggregatedD.Max();
  23.             }
  24.  
  25.             return maxD;
  26.         }
Bug, while debugging, I see that after certain point, .ElementAt(0) starts changing while I'm trying to add new elements. What is the reason behind it?

Expand|Select|Wrap|Line Numbers
  1.         internal double[] AggregationMethod(List<double[]> listOf_D)
  2.         {
  3.             double[] maxD;
  4.             double[] aggregatedD;
  5.             List<double[]> tempList;
  6.  
  7.             maxD = new double[listOf_D[0].Length];
  8.  
  9.             tempList = new List<double[]>();
  10.  
  11.             aggregatedD = new double[listOf_D.Count]; 
  12.  
  13.             for (int i = 0; i < maxD.Length; i++) 
  14.             {
  15.  
  16.  
  17.                 for (int j = 0; j < listOf_D.Count; j++) 
  18.                 {
  19.                     aggregatedD[j] = listOf_D[j][i];
  20.                 }
  21.                 tempList.Add(aggregatedD);
  22.  
  23.                 maxD[i] = aggregatedD.Max();
  24.             }
  25.  
  26.             return maxD;
  27.         }
Call by ref? or something like that? What are the ways to fix it, apart from re-initializing it?
Thanks in advance
Jan 10 '11 #1
1 2011
Rabbit
12,516 Recognized Expert Moderator MVP
I don't know C# but it seems that when you add the array to the list, it is not adding the values in the list, i.e. it is not instantiating a new array, copying the values, and then adding that new array to the list. Rather, it just adds the pointer to the array to the list. Therefore, unless you instantiate a new array, you are doing all your operations on the same array over and over.

You may not have to instantiate a new array each time if the list object had a method that allowed you to add a copy of the array. In which case, you are merely moving the instantiation out of your function into another function.
Jan 10 '11 #2

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

Similar topics

4
4448
by: jonny | last post by:
Hey I need to search an array list to remove duplicate Data The list is populated by a search function that enters in a name if the it is told too This causes duplicate names to be entered...
1
6044
by: Phenix Smith | last post by:
I am looking at coverting an application over to C#. Currently I have a global list varible that needs to be converted. I have tested different ideas to accomplish the same idea. Ths best way I...
13
11277
by: Hrvoje Voda | last post by:
How to put a specified dataset table into an array list ? Hrcko
11
8532
by: Zorpiedoman | last post by:
The problem is this: I have a list box. I set an array list as the datasource. I remove an item from the array list. I set the listbox datasource to nothing. I set the listbox datasource to...
18
1744
by: Doug Bell | last post by:
Hi, I have a function that loads Rows from a field in a DataTable into an ArrayList. See below: It also adds a Row (eg "All Areas"). I wanted this new row to be the first record so I added a...
3
3562
by: Ant | last post by:
Hi, I'm using an array list as the collection for my indexer (_alPeople). When i try to set _alpeople to a value at a certain index, I get an error. I can't simply add the value as it needs to be...
29
2758
needhelp123
by: needhelp123 | last post by:
Hi all, any body could help in reading integer values from .txt to array list and after transferring the dates from array list to another .txt file there should be two files....
0
1107
by: Csharprookie | last post by:
I am trying to create an array list (like follows) public static ArrayList GetVehicleInfo(int vehicleId, int quoteId) { ArrayList AllVehicles = new ArrayList(); I have the...
3
8739
dlite922
by: dlite922 | last post by:
I have a list of objects, the object has a String and an Int in it. I need to check to see if an item is in the list, if so increment the integer and save it back to the list. I believe I've done...
6
2234
by: chirayu143 | last post by:
adding array into array of strings in c.....
0
7205
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
7093
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
7287
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
7008
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
7467
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
5594
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,...
1
5022
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...
0
3177
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
1521
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated ...

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.