[SOLVED] 2D Collections. How to make them and is it a good idea to do so?

Welcome to the Java Programming Forums

The professional, friendly Java community. 21,500 members and growing!

The Java Programming Forums are a community of Java programmers from all around the World. Our members have a wide range of skills and they all have one thing in common: A passion to learn and code Java. We invite beginner Java programmers right through to Java professionals to post here and share your knowledge. Become a part of the community, help others, expand your knowledge of Java and enjoy talking with like minded people. Registration is quick and best of all free. We look forward to meeting you.

Is this idea even practical? It seems like it'll run out of memory sooner than a 1D one. What uses could a 2D Collection have? Is it possible to make a 2D DoublyLinkedList(a generic version of a Linked Linked)?

Re: 2D Collections. How to make them and is it a good idea to do so?

I've used 2D ArrayLists to store a graph before. I'm sure you could find a use for 2D LinkedLists, but I've never personally had to do that. Instead, I usually prefer to use some kind of a tree (BSP, quadtree/octtree, etc.) or a graph.

Yeah a 2D ArrayList could take up more memory (depending on how it's implemented), but unless you're trying to do something extreme you'll probably be ok either way.

Usually I choose not to implement a custom Collection for a specific dimension (without a specific application at least), though. Instead, I would just take advantage of generics and create something like an ArrayList of ArrayLists.

Re: 2D Collections. How to make them and is it a good idea to do so?

Originally Posted by helloworld922

I've used 2D ArrayLists to store a graph before. I'm sure you could find a use for 2D LinkedLists, but I've never personally had to do that. Instead, I usually prefer to use some kind of a tree (BSP, quadtree/octtree, etc.) or a graph.

Yeah a 2D ArrayList could take up more memory (depending on how it's implemented), but unless you're trying to do something extreme you'll probably be ok either way.

Usually I choose not to implement a custom Collection for a specific dimension (without a specific application at least), though. Instead, I would just take advantage of generics and create something like an ArrayList of ArrayLists.

I suppose you could. Never thought of that. I've actually made a LinkedList that holds a class that holds a LinkedList of another class that holds a LinkedList of yet another class.

Would one like that run out of memory? It's storing every verse in the KJV. It has a main class that has a DoublyLinkedList of Books which in turn has a DoublyLinkedList of Chapters which in turn has a DoublyLinkedList of Verses. What do you mean by extreme? The array index can hold up to 4 Billion elements, at least in theory. Can it hold a few thousand at least? I used a DoublyLinkedList as my old professor, who has a Phd. says that ArrayLists will run out of memory faster because they have to be stored in contiguous memory.

I'm hoping contiguous means all in one spot. If not, that's what I meant.

Re: 2D Collections. How to make them and is it a good idea to do so?

It doesn't need to be contiguous. Modern computers use something called "paging". So while the physical address of the memory isn't contiguous, the virtual addresses given to the program is contiguous and can be used as if it was contiguous, thus adverting the problem with trying to keep large chunks of memory free associated with contiguous allocation methods. I've been able to allocate arrays (and other forms of "contiguous memory") up to 3GB on my system without having any problems (the last GB was taken up by my OS/other programs).

There is one case I can think of that would cause a memory error in an ArrayList before a LinkedList:

Since an ArrayList grows by "doubling" it's size (I say doubling, though I'm not exactly what's the multiple they use. I know in Visual C++ they use a 1.4x growth), you need to temporarily have space for your original ArrayList and space for an ArrayList twice the size as the original. However, you can create an ArrayList which initially has that double capacity and you'd be fine.

A LinkedList required several pointers over an ArrayList per element (depending on what kind of linked list). For example, say you had a doubly linked list. Every node would contain 3 references: one to the previous node, one to the next node, and one to the actual data. An ArrayList only requires the reference to the data. Additionally you need the overhead for each node object. This could be about 8 bytes per node (don't quote me, I haven't looked too deep into this, it could be more or less).

edit: Your computer can easily hold even the largest books in the world in memory. At 1 doubly linked list node per character in the bible, that'd take up ~40 MB of memory, and with the "worst case" arraylist which has twice the capacity as the size, that's ~20 MB of memory for the ArrayList.

edit2:

did a little more reading on paging, and it turns out that when you're using the RAM, memory addresses are contiguous. Paging takes place on an auxiliary location (for example the hard disk) and doesn't necessarily hold contiguous blocks of memory. When memory needs to be swapped in, it can be re-arranged to be contiguously in the RAM. Additionally, paging is quite slow compared to operating only on RAM.