lots of ArrayLists vs 1 big ArrayList

Hi,

i was wondering should i have 1 big array list that stores 600 varriables

or have say about 200 that store 3 each.

which one is the more cost effective

it’s an extreme example but just wanted to know

more cost effective

As a programmer you’ll always be balancing speed and space… Data structures typically have overhead. An ArrayList probably has at least several bytes it needs to manage itself, in addition to the space it uses to story values. Naturally 200 lists would take more “management” space than 1 list.

It’s not enough memory, even in your “extreme” example, to be a big deal.

But is it quick? Is it easy to code? Do the simplest thing first. And optimize later.

You should tell in which way you expect one to be better than the other. There are three major operations you can do a data structure: add, remove and access.

Bigger is better when:

  • If you need to add or remove elements quickly only to/from the last position.
  • If you’re obsessively concerned about memory consumption. (Read flavius’ answer.)
  • You have an immutable, sorted list.

Smaller Is Better:

  • You need to do frequent searches through your unsorted list. *
  • You need to add/remove elements somewhere else than the end. *

* You could consider using a more suitable data structure to your need, e.g. a dictionary or a linked list.

Searching a sorted list can be done rather quickly. However, sorting it or keeping it sorted after additions will have overhead

When I come across a situation whereby I feel the need to create several lists (or other data structures) within a class, I ask myself if there is a domain concept that I’ve missed. Usually the answer is yes, and I can encapsulate the relevant data into a class of its own.