Adding custom element to ArrayList
I created a class called matrix to perform some work on a 2D array,
which I implemented with:
int array[][];
Now I'm trying to modify it to handle sparse arrays. I'm using an
array of ArrayLists like so:
private ArrayList<Element>[] rows;
//and in the matrix constructor...
rows = new ArrayList [nRows];
for (int i=0; i<nRows; i++){
rows[i] = new ArrayList<Element>();
and the Element class looks like this:
class Element {
private int column;
private int value;
//Element constructor
public Element(int column, int value){
this.column = column;
this.value = value;
}//close Rows constructor
public int getColumn(){
return column;
}
public int getValue(){
return value;
}
I have a setElement method that used to take (int row, int column, int
value) when it was a 2D array (array[row][col]=value;) but I've
changed it to:
public void setElement(int row, Element e) {
rows[row].add(e); }
Now I'm trying to add (column,value) Elements to my rows ArrayList but
I must be using the wrong syntax.
I'm trying:
setElement(r2, c2, getElement(r1, c1));
where r2 is an int representing row, c2 is an int representing column
and getElement(r1,c1) returns an int value.
I'm getting an error that says "the setElement method cannot be
applied cannot be applied to given types. required: int,
Matrix.Element
found: int, int, int
Any suggestions are greatly appreciated.