2

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?

0

6 Answers 6

3

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.

Sign up to request clarification or add additional context in comments.

1 Comment

Thank you so much for helping me with the static thing if im honest I was getting an error and thought this might help, I've posted a new question about it! Gave you the tick for spotting the static thing.
3

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

}

2 Comments

@SerDavos you need to add 17 Fish to fishList. Then only you can access 16 element by index 16. fishList.get(16); Note: list is 0 index based.
@SerDavos be warned that your code will still most probably not work as you think it will - see my answer why
1

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);

Comments

0

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

Comments

0

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));

Comments

0

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);

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.