【问题标题】:Beginner Java arraylist cannot find symbol method(int)初学者Java arraylist找不到符号方法(int)
【发布时间】:2020-07-02 23:10:28
【问题描述】:

我在使用下面的 displayIndex 方法时遇到问题,我在 Cat cat = cat.get(index);saying cannot find symbol - method get(int) 时遇到错误,我认为这是使用 arrayList 中的 .get(index) 时的正确语法?

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

       }
    }
}

【问题讨论】:

  • 您似乎试图将名称 cat 用于多个冲突变量。不如打电话给你的数组列表cats
  • 我建议您将列表命名为“cats”或“catList”,这样单只猫的变量就不会影响您的列表。
  • 是的,我意识到重命名确实有帮助。谢谢大家!

标签: java oop arraylist methods


【解决方案1】:

你在这里声明猫

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

       }
    }
}

【讨论】:

    猜你喜欢
    • 2013-04-18
    • 2013-09-05
    • 2020-07-09
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2010-10-10
    相关资源
    最近更新 更多