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.


>> REGISTER NOW TO START POSTING


Members have full access to the forums. Advertisements are removed for registered users.

Results 1 to 4 of 4

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

  1. #1
    Banned
    Join Date
    May 2010
    Location
    North Central Illinois
    Posts
    1,631
    My Mood
    Sleepy
    Thanks
    390
    Thanked 112 Times in 110 Posts

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

    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);
    }

    public My2DArrayList<T> implements My2DCollection<T>
    {
     
    private Object[][] obj;
    private int size;
     
    public My2DArrayList(int initialRows, int initalColumnsPerRow)
    {
     
    }
     
    }
    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)?


  2. #2
    Super Moderator helloworld922's Avatar
    Join Date
    Jun 2009
    Posts
    2,895
    Thanks
    23
    Thanked 619 Times in 561 Posts
    Blog Entries
    18

    Default 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.

  3. The Following User Says Thank You to helloworld922 For This Useful Post:

    javapenguin (January 14th, 2011)

  4. #3
    Banned
    Join Date
    May 2010
    Location
    North Central Illinois
    Posts
    1,631
    My Mood
    Sleepy
    Thanks
    390
    Thanked 112 Times in 110 Posts

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

    Quote Originally Posted by helloworld922 View Post
    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.

  5. #4
    Super Moderator helloworld922's Avatar
    Join Date
    Jun 2009
    Posts
    2,895
    Thanks
    23
    Thanked 619 Times in 561 Posts
    Blog Entries
    18

    Default 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
    Last edited by helloworld922; January 15th, 2011 at 02:28 AM.

Similar Threads

  1. Good morning!
    By gnome in forum Member Introductions
    Replies: 0
    Last Post: January 1st, 2011, 04:18 PM
  2. Help me to Choose Collections
    By kathir0301 in forum Collections and Generics
    Replies: 1
    Last Post: December 3rd, 2010, 10:14 AM
  3. Generics collections and reflection
    By juza in forum Collections and Generics
    Replies: 2
    Last Post: November 30th, 2010, 03:14 AM
  4. how to sort objects with collections???
    By kyros in forum What's Wrong With My Code?
    Replies: 1
    Last Post: October 31st, 2010, 02:21 PM
  5. Replies: 10
    Last Post: June 22nd, 2009, 07:45 AM