0

I have an ArrayList and I want to give index as big integer value (Customer ID)

ex:

limitAmountTotal[CUSTOMERID] = amount;

where CUSTOMERID = 1000000

But I am getting an error

arraylist is out of range

Is there any way I can handle this in C#?

in PHP we can do this like this

$limitAmountTotal[$CUSTOMERID] = $amount;

Thanks

5
  • 1
    I don't get it why would you want to use ArrayList in this case? Isn't dictionary a better solution for this... ? Commented May 8, 2014 at 6:48
  • arraylist inital capability is zero (from 3.5 and above). see this: stackoverflow.com/questions/1565000/… Commented May 8, 2014 at 6:48
  • 5
    Don't use ArrayList anymore. It belongs old days that C# doesn't have Generics. Use List<T> instead. Commented May 8, 2014 at 6:50
  • I suggest to use List<T> as @SonerGönül said before or simple Array... but Array may be inapropriate for this if it should dynamically change its size. Commented May 8, 2014 at 6:57
  • @SonerGönül just for my understanding, what's wrong with a Dictionary ? Commented May 8, 2014 at 7:01

2 Answers 2

4

That's because ArrayList behaves like an array. You want Dictionary instead:

Dictionary<int, int> limitAmountTotal  = new Dictionary<int, int>();
limitAmountTotal[10000] = 30;
Sign up to request clarification or add additional context in comments.

9 Comments

Because you want to dereference using a key, not an index.
Why don't use single list/array for this? He want some big integer (long) as index not some special key or I am missing something here.
Wouldn't an array consume way to much memory?
Are you serious...? - dotnetperls.com/array-dictionary-test how did you get 13.7k reputation points I don't know...
@rosko: no need to be insulting. First, your link does not address memory usage, only speed. Second I reckon the asker might not have 1,000,000 items in his array. If you would use an array and want to dereference the 1,000,000th object, you will have allocated a million unused objects. In a Dictionary only used objects are allocated.
|
-2

Use List<T> or simple Array (array is optional but not recommended for this!).

If your amount is some monetary value then you can try using:

decimal[] arr = new decimal[arraySize];

or:

List<decimal> list = new List<decimal>();

If not change decimal to whatever data type you like: int, long and etc.

Edit:

Some performance and memory tests:

All collections are type of `int`
Maximum collection size: 1000000
Random generated index used for value lookup: 354497

Array populating time elapsed: 00:00:00.0063499
Array lookup time elapsed: 00:00:00.0000004
Size: 4000028 bytes
==============
List populating time elapsed: 00:00:00.0184629
List lookup time elapsed: 00:00:00.0000004
Size: 4194509 bytes
==============
Dictionary populating time elapsed: 00:00:00.0593676
Dictionary lookup time elapsed: 00:00:00.0000293
Size: 17001335 bytes

Code: https://gist.github.com/rekosko/999e58dc95ba3e4fc678

4 Comments

To access an array in the way he wants he would first need to initialize an array with length of 1000000 just so he could use it in a way limitAmountTotal[1000000] = amount. Same goes for List<T>. Your answer is wrong.
Yes you are right but you don't really know if he needs an array that is so large anyway it's just an option to use an array i didn't say it's the way, right?
It could be even worse, he would maybe need an even larger array ;)
As I said - the usage of array is just an option.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.