Reputation: 25
I'm new to coding java and would love some help. I'm trying to add an object of the class Fish to an arraylist call fishList
in my main I have for example
public static void main(String[] args) {
Fish f1 = new Fish("Nemo");}
and in my class and constructor I have
public class Fish {
protected static String name;
protected static int number;
protected static List<Fish> fistList = new ArrayList<Fish>();
public Fish(String in){
name = in;
number = 15;
fishList.add(name, number);
}
but I get an error "no suitable method found for add(string, int) method List.add(int, Fish) is not applicable (actual argument String cannot be converted to int by method invocation conversion) method List.add(Fish) is not applicable (actual an formal argument list differ in length)
How do I add objects to an arraylist properly?
Upvotes: 2
Views: 14065
Reputation: 810
The given name and number is in String format, but you specify Fish class in your generics, so you have to add only Fish objects in your List like
fishList.add(this);
Upvotes: 0
Reputation: 779
because you used generics and declared fishList of with . So you are only allowed to add the list items of type Fish. no other types are allowed. This is the compile time type safety provided by generics. add() of List is overloaded as follows. add(T) and add(int,T) where first argument is the index and second is of any type. but you are saying add(String,int) which is not there in java.util.List.
static fields are never recomanded unless and until they are required.because they shows huge impact in performance. so remove static in properties declaration.
If the property 'number' may takes different values then dont assign default value 15 in its constructor rather do like this.
private String name;
private int number;
public Fish(String name,int number){
this.name = name;
this.number = number;
}
replace the above statement firstList.add(name,number);
with
firstList.add(new Fish("abc",10));
Upvotes: 0
Reputation: 26094
you need to change fishList.add(name, number);
to fishList.add(this);
Dont create object property with static modifier unless needed. static modifier means that property belongs to class. if any one of the object of Fish modifying these properties will get changed to last modified value.
protected static String name;
protected static int number;
Please modify ypur pojo like this.
class Fish {
private String name;
private int number;
public Fish(String name, int number) {
super();
this.name = name;
this.number = number;
}
@Override
public String toString() {
return "Fish [name=" + name + ", number=" + number + "]";
}
}
public static void main(String[] args) {
List<Fish> fistList = new ArrayList<Fish>();
fistList.add(new Fish("name1",1));
fistList.add(new Fish("name1",2));
fistList.add(new Fish("name1",3));
fistList.add(new Fish("name1",4));
fistList.add(new Fish("name1",5));
fistList.add(new Fish("name1",6));
for (Fish fish : fistList) {
System.out.println(fish);
}
System.out.println(fistList.get(0)); // getting the first Fish
System.out.println(fistList.get(1)); // getting the second Fish
}
Upvotes: 3
Reputation: 28513
you cannot pass two arguments to add method of list.
pass this like below
fishList.add(this);
also correct typo error of fistList
to fishList
in declaration of list
Upvotes: 0
Reputation: 69
The ArrayList you defined is parameterized to accept Fish objects, placing an object that is not a Fish in there will result in an error. Furthermore, As the other answers have mentioned, you are using the wrong form of the ArrayList.add method.
fishList.add(name, number);
Should be,
fishList.add(this);
Upvotes: 1
Reputation: 15141
First of all name
and number
shouldn't be static
(unless you want all the Fish to have the same name/number but then creating more than 1 instance of that class would be a waste of resources)!
Secondly, change :
fishList.add(name, number);
To:
fishList.add(this);
fishList can hold references to objects of type Fish. If you try to add "name, number" Java doesn't know you mean a Fish :-)
this
points to the objects that is currently being created in the constructor.
Upvotes: 3