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

Thread: How to declare an object of multi-dimension array?

  1. #1
    Junior Member
    Join Date
    Jan 2011
    Posts
    4
    Thanks
    4
    Thanked 0 Times in 0 Posts

    Default How to declare an object of multi-dimension array?

    <the code show below was copied from this forum>
    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 ensureCapcity(int row, int num)
        {
            ensureCapacity(row);
            while (row < getNumRows())
            {
                array.add(new ArrayList<Type>());
            }
            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();
        }
    }

    Can anyone tell me how to declare an object of the class above?
    Kindly thanks for help from you all..>.<
    Last edited by copeg; January 13th, 2011 at 07:59 PM.


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

    Default Re: How to declare an object of multi-dimension array?

    Don't use an ArrayList. Have a 2D array and write your own class using a 2D array to modify.

    Have the get check for row and column.

    Change your contains method to use .equals() instead of .contains() to see if true or false.

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

    FongChengWeng (January 13th, 2011)

  4. #3
    Administrator copeg's Avatar
    Join Date
    Oct 2009
    Location
    US
    Posts
    5,320
    Thanks
    181
    Thanked 833 Times in 772 Posts
    Blog Entries
    5

    Default Re: How to declare an object of multi-dimension array?

    Quote Originally Posted by javapenguin View Post
    Don't use an ArrayList. Have a 2D array and write your own class using a 2D array to modify.
    Why? This doesn't even answer the OP's question...

    FongChengWeng, I suggest reading the Announcements for instructions on how to format code for the forums (I've edited your post for you).
    You create this class as you would any other class
    ArrayList2d list = new ArrayList2d();
    This does not enforce type safety however, and will result in compiler warning and possible runtime casting exceptions. To use generics, you specify what object you wish to have in the class
    ArrayList2d<String> list = new ArrayList2d<String>();
    More suggested reading: Generics

  5. The Following User Says Thank You to copeg For This Useful Post:

    FongChengWeng (January 13th, 2011)

  6. #4
    Junior Member
    Join Date
    Jan 2011
    Posts
    4
    Thanks
    4
    Thanked 0 Times in 0 Posts

    Default Re: How to declare an object of multi-dimension array?

    copeg, javapenguin, thanks for help and suggenstion!!
    Can I ask, can we use 2d ArrayList to create N x N matrix?

  7. #5
    Administrator copeg's Avatar
    Join Date
    Oct 2009
    Location
    US
    Posts
    5,320
    Thanks
    181
    Thanked 833 Times in 772 Posts
    Blog Entries
    5

    Default Re: How to declare an object of multi-dimension array?

    Quote Originally Posted by FongChengWeng View Post
    Can I ask, can we use 2d ArrayList to create N x N matrix?
    Yes. ArrayLists are nice in that they are dynamic (can change size depending upon what you put in). If you have a known fixed size N you might prefer to use an array rather than an ArrayList

  8. The Following User Says Thank You to copeg For This Useful Post:

    FongChengWeng (January 13th, 2011)

  9. #6
    Junior Member
    Join Date
    Jan 2011
    Posts
    4
    Thanks
    4
    Thanked 0 Times in 0 Posts

    Default Re: How to declare an object of multi-dimension array?

    After I created the ArrayList2d (list), why list.Add("String", 0) (function in the ArrayList2d class) cause an error.(Exception in thread "main" java.lang.IndexOutOfBoundsException: Index: 0, Size: 0
    at java.util.ArrayList.RangeCheck(ArrayList.java:547)
    at java.util.ArrayList.get(ArrayList.java:322)
    at ArrayList2d.getNumCols(CreateElement.java:85)
    at CreateElement.main(CreateElement.java:100)

    Process completed.)

    The matrix I want to create is dynamic. Because I need to read 18k line of data from a file>.<

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

    Default Re: How to declare an object of multi-dimension array?

    It does that because when I changed the code last time, I only made sure it compiled, not ran Hopefully it works now.

    I wrote that code as a "sample" of how to create multi-dimensional ArrayLists and various operations you might want to do with them. While you're welcome to use that code for whatever you want, I would recommend directly using an ArrayList of ArrayLists (the concept embodied inside that post).

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

    FongChengWeng (January 14th, 2011)

  12. #8
    Junior Member
    Join Date
    Jan 2011
    Posts
    4
    Thanks
    4
    Thanked 0 Times in 0 Posts

    Default Re: How to declare an object of multi-dimension array?

    YES!!It's works!!
    But there is an error inside the Add function. It should be getNumRows() rather than getRow() if I'm not mistaken and the ensureCapacity() function will cause an error if it's invoke.
    Thank You so much!
    Last edited by FongChengWeng; January 14th, 2011 at 01:25 AM.

Similar Threads

  1. Multi-dimension ArrayList example
    By adeola in forum Collections and Generics
    Replies: 3
    Last Post: March 26th, 2010, 03:23 AM
  2. Multi Dimensional Array help NEEDED URGENT
    By bonjovi4u in forum Loops & Control Statements
    Replies: 5
    Last Post: February 13th, 2010, 12:44 AM
  3. Multi-dimension ArrayList example
    By helloworld922 in forum Java Programming Tutorials
    Replies: 1
    Last Post: February 3rd, 2010, 11:01 AM
  4. How do you set an object in array to null value?
    By Arius in forum What's Wrong With My Code?
    Replies: 5
    Last Post: January 25th, 2010, 03:50 AM
  5. [SOLVED] Method declaration in Java
    By mohsendeveloper in forum Object Oriented Programming
    Replies: 4
    Last Post: June 11th, 2009, 03:18 AM