473,657 Members | 2,392 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 2015
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
4468
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 into the array, the search function is not changable e. the array list has 5 values "jonny", I need to remove all the values but the 1st. The array can have several differnet copies of one name
1
6068
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 have found for me is to create a configuration class with a static array list that is accessed through static methods. Is there a better way to handle this. This class is in a DLL if that makes a difference. public class Configuration {...
13
11304
by: Hrvoje Voda | last post by:
How to put a specified dataset table into an array list ? Hrcko
11
8548
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 the array list again to refresh. Click on an item in the list, and an Indexing error comes up. (in non-user code, it is trying to get an element from the array that is greater than the number of items in the array.) To reproduce:
18
1756
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 preceeding space (eg " All Areas") and sorted the ArrayList. I have just found that there are some valid records that have one or more
3
3570
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 at an index set by the user. the Set statement is this: _people = value; or even _people.Insert(index,value); but these throw 'ArgumentOutOfRange' exception stating: Must be non-negative and less than the size of the collection.
29
2785
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
1117
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 ArrayList delcared as follows for use in a repeater. protected ArrayList AllVehicles = new ArrayList();
3
8781
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 everything OK up to the point of checking to see if an object is in the list by overloading the equals() and hashCode() functions. I don't have any errors and compiles except my list contains two entries of the same name (String) instead of...
6
2239
by: chirayu143 | last post by:
adding array into array of strings in c.....
0
8411
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
8323
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,...
0
8838
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...
0
7351
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
6176
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
5638
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
4173
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...
2
1969
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
2
1732
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.