Results 1 to 6 of 6
  1. #1
    lo2
    lo2 is offline Member
    Join Date
    Nov 2012
    Posts
    11
    Rep Power
    0

    Default 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:

    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);
      }
    But I get this error code then:

    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?

  2. #2
    Sierra is offline AN21XX
    Join Date
    Mar 2012
    Location
    Munich
    Posts
    297
    Rep Power
    3

    Default 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!

  3. #3
    lo2
    lo2 is offline Member
    Join Date
    Nov 2012
    Posts
    11
    Rep Power
    0

    Default Re: How to add this to an ArrayList

    Quote Originally Posted by Sierra View Post
    You need to add a object of the type you created the list with: "Node"
    try e.g.: add(new Node(position, expression) )
    Ah yeah ok, it is because I can only add one object at a time right?

    And thanks it seems to work now ;)

  4. #4
    Sierra is offline AN21XX
    Join Date
    Mar 2012
    Location
    Munich
    Posts
    297
    Rep Power
    3

    Default Re: How to add this to an ArrayList

    You may add multiple objects using .addAll(...) from the ArrayList.
    I like likes!

  5. #5
    lo2
    lo2 is offline Member
    Join Date
    Nov 2012
    Posts
    11
    Rep Power
    0

    Default 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:

    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;
          }
        }
    I get this error code, in fact I get an error code for both of the get. methods:

    error:
    cannot find symbol
    return ((column == other.getColumn()) && (row == other.getRow()));
    ^
    symbol: method getRow()
    location: variable other of type Object

  6. #6
    wsaryada is offline Senior Member
    Join Date
    Jun 2007
    Location
    Bali, Indonesia
    Posts
    758
    Rep Power
    8

    Default Re: How to add this to an ArrayList

    Your code should be:

    Java Code:
    return ((column == position.getColumn()) && (row == position.getRow()))
    Because "other" type is java.lang.Object it will never find the getRow() method in there.

Similar Threads

  1. Replies: 12
    Last Post: 07-07-2011, 08:49 PM
  2. copying contents of an ArrayList to another ArrayList
    By ankit1801 in forum New To Java
    Replies: 8
    Last Post: 03-27-2011, 06:07 AM
  3. sorting arraylist based on another arraylist
    By busdude in forum New To Java
    Replies: 4
    Last Post: 02-07-2011, 11:48 AM
  4. Replies: 7
    Last Post: 11-22-2010, 01:26 PM
  5. Replies: 2
    Last Post: 04-21-2008, 11:43 AM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •