Results 1 to 6 of 6
Thread: How to add this to an ArrayList
- 12-07-2012, 06:07 PM #1
Member
- Join Date
- Nov 2012
- Posts
- 11
- Rep Power
- 0
How to add this to an ArrayList
Hey there I trying to make a kind of spreadsheet in java, we are implementing it step by step of course. But we have something called a node which contains a position and an expression, each node should represent a cell in our spreadsheet.
We then would like to add these to the ArrayList, and for this I have written the following:
But I get this error code then:Java Code:package spreadsheet; import java.util.ArrayList; public class Spreadsheet { private ArrayList<Node> sheet; public Spreadsheet() { sheet = new ArrayList<Node>(); } public void set(final Position position, final Expression expression) { sheet.add(position, expression); }
error: no suitable method found for add(Position,Expression)
sheet.add(position, expression);
^
method ArrayList.add(int,Node) is not applicable
(actual argument Position cannot be converted to int by method invocation
conversion)
method ArrayList.add(Node) is not applicable
(actual and formal argument lists differ in length)
So well I guess I am adding these elements in the wrong way, so I how should I do it diffrently?
- 12-07-2012, 06:12 PM #2
AN21XX
- Join Date
- Mar 2012
- Location
- Munich
- Posts
- 297
- Rep Power
- 2
Re: How to add this to an ArrayList
You need to add a object of the type you created the list with: "Node"
try e.g.: add(new Node(position, expression) )I like likes!.gif)
- 12-07-2012, 06:28 PM #3
Member
- Join Date
- Nov 2012
- Posts
- 11
- Rep Power
- 0
- 12-07-2012, 06:48 PM #4
AN21XX
- Join Date
- Mar 2012
- Location
- Munich
- Posts
- 297
- Rep Power
- 2
Re: How to add this to an ArrayList
You may add multiple objects using .addAll(...) from the ArrayList.
I like likes!.gif)
- 12-07-2012, 07:17 PM #5
Member
- Join Date
- Nov 2012
- Posts
- 11
- Rep Power
- 0
Re: How to add this to an ArrayList
Ok well I have another question:
I have this method where I want to be able to determine if two cells are equal, this means to determine if they have the same position. I have then written this piece of code where I both use instanceof and casting to make sure that my object is of the type position and then make it of the type position, but it does not seem to work for some reason. Here is my code:
I get this error code, in fact I get an error code for both of the get. methods:Java Code:public class Position { private int column; private int row; public Position (final int column, final int row) { this.column = column; this.row = row; } public int getColumn() { return column; } public int getRow() { return row; } public boolean equals(final Object other) { if(other instanceof Position) { Position position = (Position) other; return ((column == other.getColumn()) && (row == other.getRow())); } else { return false; } }
error:
cannot find symbol
return ((column == other.getColumn()) && (row == other.getRow()));
^
symbol: method getRow()
location: variable other of type Object
- 12-08-2012, 01:36 AM #6
Senior Member
- Join Date
- Jun 2007
- Location
- Bali, Indonesia
- Posts
- 696
- Rep Power
- 6
Re: How to add this to an ArrayList
Your code should be:
Because "other" type is java.lang.Object it will never find the getRow() method in there.Java Code:return ((column == position.getColumn()) && (row == position.getRow()))
Website: Learn Java by Examples
Similar Threads
-
ArrayList copy some of the element from one arraylist tnto another arraylist
By ralf in forum New To JavaReplies: 12Last Post: 07-07-2011, 08:49 PM -
copying contents of an ArrayList to another ArrayList
By ankit1801 in forum New To JavaReplies: 8Last Post: 03-27-2011, 06:07 AM -
sorting arraylist based on another arraylist
By busdude in forum New To JavaReplies: 4Last Post: 02-07-2011, 11:48 AM -
how to add Arraylist filter for a jsp page showing results from a servlet-Arraylist
By alok_sharma in forum Java ServletReplies: 7Last Post: 11-22-2010, 01:26 PM -
Java Project Trouble: Searching one ArrayList with another ArrayList
By BC2210 in forum New To JavaReplies: 2Last Post: 04-21-2008, 11:43 AM


LinkBack URL
About LinkBacks
Reply With Quote

Bookmarks