Go Back   Java Programming Forums > Learning Java > Java Code Snippets and Tutorials

Reply
 
LinkBack Thread Tools Display Modes
  #1 (permalink)  
Old 23-07-2009, 01:45 AM
helloworld922's Avatar
Super Moderator
 

Join Date: Jun 2009
Posts: 838
Thanks: 5
Thanked 187 Times in 172 Posts
helloworld922 will become famous soon enoughhelloworld922 will become famous soon enoughhelloworld922 will become famous soon enough
Post Multi-dimension ArrayList example

Here's a way to get multi dimension ArrayLists:

In Java, you unfortunately can't create arrays of generics. However, ArrayList is basically an array, so create an ArrayList of ArrayLists.

Here's a few basic operations on a 2d ArrayList (easily extended to any dimension).
Java Code
public class ArrayList2d<Type>
{
     ArrayList<ArrayList<Type>> array;

     public ArrayList2d()
     {
          array = new ArrayList<ArrayList<type>>();
     }

     public void ensureCapacity(int num)
     {
          array.ensureCapacity(num);
          while (rows >= array.size())
          {
               array.add(new ArrayList<Type>());
          }
     }

     public void ensureCapcity(int row, int num)
     {
          ensureCapacity(row);
          array.get(row).ensureCapacity(num);
     }

     public void Add(Type data, int row)
     {
          ensureCapacity(row);
          array.get(row).add(data);
     }

     public Type get(int row, int col)
     {
          return array.get(row).get(col);
     }

     public void remove(int row, int col)
     {
          array.get(row).remove(col);
     }

     public boolean contains(Type data)
     {
          for (int i = 0; i < array.size(); i++)
          {
               if (array.get(i).contains(data))
               {
                     return true;
               }
          }
          return false;
     }

     public int getNumRows()
     {
          return array.size();
     }

     public int getNumCols(int row)
     {
          return array.get(row).size();
     }
}



Reply With Quote Share this thread on Facebook
The Following 2 Users Say Thank You to helloworld922 For This Useful Post:
JavaPF (23-07-2009), Json (23-07-2009)
Sponsored Links
  #2 (permalink)  
Old 03-02-2010, 04:01 PM
Json's Avatar
Super Moderator
 

Join Date: Jul 2009
Location: Manchester, United Kingdom
Posts: 861
Thanks: 31
Thanked 112 Times in 108 Posts
Json will become famous soon enoughJson will become famous soon enough

I'm feeling Happy
Default Re: Multi-dimension ArrayList example

See also:

google-collections - Project Hosting on Google Code

// Json
Reply With Quote
Reply

Thread Tools
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On



Similar Threads
Thread Thread Starter Forum Replies Last Post
ArrayList Problem Marty Collections and Generics 10 21-02-2010 07:56 PM
convert arraylist to a hash map nadman123 Collections and Generics 1 29-07-2009 09:24 AM
Converting a method from ArrayList so it is capable with an Array BlueJ1 Collections and Generics 2 08-07-2009 10:22 PM
How to use an ArrayList - java.util.ArrayList JavaPF Java Code Snippets and Tutorials 1 17-05-2009 06:12 PM
creating objects from file input and storing in arraylist LabX File I/O & Other I/O Streams 4 14-05-2009 08:52 AM


100 most searched terms
Search Cloud
2 dimensional arraylist java 2d arraylist java actionlistener actionlistener in java actionlistener java actionlistener jbutton addactionlistener addactionlistener java avatar hardware id convert double to integer java double format java double to int java double to integer in java double to integer java eclipse shortcut keys eclipse tutorial for beginners exception in thread "awt-eventqueue-0" java.lang.outofmemoryerror: java heap space exception in thread "main" java.lang.outofmemoryerror: java heap space format double java get mouse position java hardware id avatar java 2 dimensional arraylist java 2d arraylist java actionlistener java addactionlistener java button actionlistener java convert double to int java double format java double to int java double to integer java for beginner eclippse java format double java forum java forums java get mouse position java jbutton java list to map java mouse position java programming forum java programming forums java sendkeys java.lang.reflect.invocationtargetexception java.util.arraylist jbutton actionlistener jbutton java jtextarea font size programming forums string to int java two dimensional arraylist java writing apps for ipod touch

All times are GMT. The time now is 12:33 PM.
Powered by vBulletin® Copyright ©2000-2009, Jelsoft Enterprises Ltd.