Java Add Object to ArrayList error

13,622

Solution 1

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.

Solution 2

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

}

Solution 3

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);
Share:
13,622
Admin
Author by

Admin

Updated on June 28, 2022

Comments

  • Admin
    Admin almost 2 years

    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?

  • Admin
    Admin over 10 years
    Thanks for the help friend!
  • Prabhakaran Ramaswamy
    Prabhakaran Ramaswamy over 10 years
    @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.
  • Mateusz Dymczyk
    Mateusz Dymczyk over 10 years
    @SerDavos be warned that your code will still most probably not work as you think it will - see my answer why
  • Admin
    Admin over 10 years
    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.