2D Collections. How to make them and is it a good idea to do so?
Code java:
public interface My2DCollection<T>
{
public T get(int row, int column);
public int Size();
public T front();
public T back();
public void add(T data, int row, int column);
public void remove(int row, int column);
public void removeFirst();
public void removeLast();
public void addFirst(T data);
public void addLast(T data);
public T middle();
public T get(int row, int column);
public void swap(int row1, int col1, int row2, int col2);
}
Code java:
public My2DArrayList<T> implements My2DCollection<T>
{
private Object[][] obj;
private int size;
public My2DArrayList(int initialRows, int initalColumnsPerRow)
{
}
}
Code java:
public My2DDoublyLinkedList<T> implements My2DCollection<T>
{
}
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?
Quote:
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.
For more info, see: Paging - Wikipedia, the free encyclopedia