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


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

Join Date: Jun 2009
Posts: 1,329
Thanks: 5
Thanked 282 Times in 254 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();
     }
}



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
Java Training from DevelopIntelligence
  #2 (permalink)  
Old 03-02-2010, 03:01 PM
Json's Avatar
Super Moderator
 

Join Date: Jul 2009
Location: Manchester, United Kingdom
Posts: 1,181
Thanks: 55
Thanked 137 Times in 133 Posts
Json will become famous soon enoughJson 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
Closed Thread

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 16 31-08-2010 08:47 AM
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
2d arraylist java actionlistener actionlistener in java actionlistener java addactionlistener addactionlistener in java addactionlistener java applications of oops could not create java virtual machine xp double format java double to int 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.nullpointerexception exception in thread "main" java.lang.outofmemoryerror: java heap space format double java get mouse position java java 2d arraylist java actionlistener java addactionlistener java convert list to map java double format java double formatting java double to int java double to integer java format double java forum java forums java get mouse position java list to map java mouse position java programming forum java programming forums java programming help java sendkeys java two dimensional arraylist java.lang.classformaterror: truncated class file java.lang.outofmemoryerror: java heap space java.util.arraylist jbutton actionlistener jtextarea font jtextfield font size jxl.read.biff.biffexception: unable to recognize ole stream programming mutators and generics two dimensional arraylist java writing ipod apps

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