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 2 of 2

Thread: Multi-dimension ArrayList example

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

    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).
    import java.util.ArrayList;
     
    public class ArrayList2d<Type>
    {
    	ArrayList<ArrayList<Type>>	array;
     
    	public ArrayList2d()
    	{
    		array = new ArrayList<ArrayList<Type>>();
    	}
     
    	/**
    	 * ensures a minimum capacity of num rows. Note that this does not guarantee
    	 * that there are that many rows.
    	 * 
    	 * @param num
    	 */
    	public void ensureCapacity(int num)
    	{
    		array.ensureCapacity(num);
    	}
     
    	/**
    	 * Ensures that the given row has at least the given capacity. Note that
    	 * this method will also ensure that getNumRows() >= row
    	 * 
    	 * @param row
    	 * @param num
    	 */
    	public void ensureCapacity(int row, int num)
    	{
    		ensureCapacity(row);
    		while (row < getNumRows())
    		{
    			array.add(new ArrayList<Type>());
    		}
    		array.get(row).ensureCapacity(num);
    	}
     
    	/**
    	 * Adds an item at the end of the specified row. This will guarantee that at least row rows exist.
    	 */
    	public void Add(Type data, int row)
    	{
    		ensureCapacity(row);
    		while(row >= getNumRows())
    		{
    			array.add(new ArrayList<Type>());
    		}
    		array.get(row).add(data);
    	}
     
    	public Type get(int row, int col)
    	{
    		return array.get(row).get(col);
    	}
     
    	public void set(int row, int col, Type data)
    	{
    		array.get(row).set(col,data);
    	}
     
    	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();
    	}
    }

    edit: fixed a few bugs
    Last edited by helloworld922; January 14th, 2011 at 04:21 PM.

  2. The Following 5 Users Say Thank You to helloworld922 For This Useful Post:

    javapenguin (April 5th, 2011), JavaPF (July 23rd, 2009), Json (July 23rd, 2009), ptl161 (January 4th, 2011), svesda (October 14th, 2012)


  3. #2
    Super Moderator Json's Avatar
    Join Date
    Jul 2009
    Location
    Warrington, United Kingdom
    Posts
    1,274
    My Mood
    Happy
    Thanks
    70
    Thanked 156 Times in 152 Posts

    Default Re: Multi-dimension ArrayList example

    See also:

    google-collections - Project Hosting on Google Code

    // Json

Similar Threads

  1. How to use an ArrayList and what is its advantage over array?
    By JavaPF in forum Java SE API Tutorials
    Replies: 4
    Last Post: December 21st, 2011, 04:44 AM
  2. ArrayList Problem
    By Marty in forum Collections and Generics
    Replies: 16
    Last Post: August 31st, 2010, 03:47 AM
  3. convert arraylist to a hash map
    By nadman123 in forum Collections and Generics
    Replies: 1
    Last Post: July 29th, 2009, 04:24 AM
  4. Converting a method from ArrayList so it is capable with an Array
    By BlueJ1 in forum Collections and Generics
    Replies: 2
    Last Post: July 8th, 2009, 05:22 PM
  5. Object creation from a input file and storing in an Array list
    By LabX in forum File I/O & Other I/O Streams
    Replies: 4
    Last Post: May 14th, 2009, 03:52 AM