-3

I have a class of Line and a fit constructor.

I defined:

Line l1 = new Line("A", "B");

I have a class ts, that has a member: Vector<Line> allLines = new Vector<Line>();

I want to add the line l1 into this vector..

I tried three options but it doesn't work:

ts.allLines.addElement(l1);

but I got the error:

The method addElement(Line) in the type Vector<Line> is not applicable for the arguments (Line)

ts.allLines.add(l1);

but I got:

The method add(Line) in the type Vector<Line> is not applicable for the arguments (Line)

but it doesn't work.

Woot4Moo
  • 23,987
  • 16
  • 94
  • 151
Alon Shmiel
  • 6,753
  • 23
  • 90
  • 138

3 Answers3

4

Make sure your import for Line class are correct. You may have imported a wrong Line class.

Juned Ahsan
  • 67,789
  • 12
  • 98
  • 136
2

Your class should look like this:

package com.example;  
import java.util.Vector;  
import com.example.Line;

public class Foo  
{  
    Vector<Line> lines = new Vector<Line>();  

    public void add(Line line)  
    {
         this.lines.add(line);
    }  
}  

make sure you are importing both the correct Vector class and the correct Line class.

Woot4Moo
  • 23,987
  • 16
  • 94
  • 151
0

You should probably use one of the List implementations, for example ArrayList, instead of Vector. Althrough it is not marked as deprecated, it is in the library only for legacy code support and should be avoided. This question highlights several problems with the Vector class.

Community
  • 1
  • 1
Marcin Jedynak
  • 3,697
  • 2
  • 20
  • 19