Начинающий Java arraylist не может найти символ метода (int) - PullRequest
0 голосов
/ 22 марта 2020

У меня проблема с моим методом displayIndex ниже, я получаю сообщение об ошибке Cat cat = cat.get(index);, говоря cannot find symbol - method get(int), я думал, что это был правильный синтаксис при использовании .get (index) из arrayList?

import java.util.ArrayList;
public class Cattery
{
    // instance variables 
    private ArrayList<Cat> cat;
    private String businessName;
    /**
     * 
     */
    public Cattery(String businessName){
        cat = new ArrayList<Cat>();
        this.businessName = businessName;
     }
    /**
     * 
     */
    public void addCat(Cat newCat){
     cat.add(newCat);
     }
    /**
     * 
     */
    public void displayIndex(int index) {
    if((index >= 0) && (index <= cat.size()-1)) {
        Cat cat = cat.get(index);                
        System.out.println(index);
    }
    else{
        System.out.println("Invalid index position");
    }
    }
    /**
     * to remove cat
     */
     public void removeCat(int indexRemove){
       if((indexRemove >= 0) && (indexRemove <= cat.size()-1)) {
         cat.remove(indexRemove);
        }
     else{
         System.out.println("Invalid index position");
       }
       }

    public void displayNames(){
    System.out.println("The current guests in Puss in Boots Cattery:");
    for(Cat catNames : cat ){
       System.out.println(catNames.getName());

       }
    }
}

1 Ответ

3 голосов
/ 22 марта 2020

Вы объявляете cat здесь

  private ArrayList<Cat> cat;

, а затем снова здесь (отсюда и конфликт)

public void displayIndex(int index) {
if((index >= 0) && (index <= cat.size()-1)) {
    Cat cat = cat.get(index);    

Вы можете изменить его на

 public void displayIndex(int index) {
 if((index >= 0) && (index <= cat.size()-1)) {
 Cat cat = this.cat.get(index);     

В противном случае переименовать переменная.

public class Cattery
{
    // instance variables 
    private ArrayList<Cat> cats;
    private String businessName;
    /**
     * 
     */
    public Cattery(String businessName){
        cats = new ArrayList<Cat>();
        this.businessName = businessName;
     }
    /**
     * 
     */
    public void addCat(Cat newCat){
     cats.add(newCat);
     }
    /**
     * 
     */
    public void displayIndex(int index) {
    if((index >= 0) && (index <= cats.size()-1)) {
        Cat cat = cats.get(index);                
        System.out.println(index);
    }
    else{
        System.out.println("Invalid index position");
    }
    }
    /**
     * to remove cat
     */
     public void removeCat(int indexRemove){
       if((indexRemove >= 0) && (indexRemove <= cats.size()-1)) {
         cats.remove(indexRemove);
        }
     else{
         System.out.println("Invalid index position");
       }
       }

    public void displayNames(){
    System.out.println("The current guests in Puss in Boots Cattery:");
    for(Cat catNames : cats ){
       System.out.println(catNames.getName());

       }
    }
}
Добро пожаловать на сайт PullRequest, где вы можете задавать вопросы и получать ответы от других членов сообщества.
...