How to declare an object of multi-dimension array?
<the code show below was copied from this forum>
Code java:
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..>.<
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.
Re: How to declare an object of multi-dimension array?
Quote:
Originally Posted by
javapenguin
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
Code :
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
Code :
ArrayList2d<String> list = new ArrayList2d<String>();
More suggested reading: Generics
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?
Re: How to declare an object of multi-dimension array?
Quote:
Originally Posted by
FongChengWeng
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
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.)
:confused:
The matrix I want to create is dynamic. Because I need to read 18k line of data from a file>.<
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 :P 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).
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!