【问题标题】:The type of the expression must be an array type but it resolved to表达式的类型必须是数组类型,但它解析为
【发布时间】:2013-10-23 14:17:52
【问题描述】:

我是 Java 新手。我不明白为什么会发生这些错误。试图制作一个数组列表,以便保存每个对象。我得到的错误是表达式的类型必须是数组类型,但它解析为'newbug1 [i].setspecies();'行上的ArrayList

提前谢谢你

import javax.swing.JOptionPane;
import java.util.ArrayList;

public class Abug2 {
    private String species;
    private String name;
    private char symbol = '\0'; 
    private int horposition = 0, verposition = 0, energy = 0, uniqueID = 1, counter; 


    public Abug2(String species, String name, char symbol)
    {
        uniqueID = counter;
        counter++;
    }


    public void setspecies(){
    species = JOptionPane.showInputDialog(null, "Enter the species: ");
    }
    public String getspecies(){
        return species;
    }

    public void setname(){
    name = JOptionPane.showInputDialog(null, "Enter the name: ");
    }
    public String getname(){
    return name;
    }

    public void setsymbol(){
    symbol = name.charAt(0);
    }
    public char getsymbol(){
        return symbol;
    }

    public int getid(){
        return uniqueID;
    }

    public int gethorizontal(){
    return horposition;
    }

    public int getvertical(){ 
        return verposition;
    }

    public int getenergy(){
        return energy;
    }

    //The class ABug has a set of methods: two or more constructors, toString, toText, and getters and setters for the attributes 

    public String toString(){
        String tostring = "\nName: " + name + "\nHorizontal Position: " + horposition + "\nVertical Position: " + verposition + "\n";
                return tostring;
    }

    public String toText(){
        String totext = getspecies() + getname() + getsymbol() + getid() + gethorizontal() + getvertical() + getenergy();
                return totext;
    }


    public static void main (String [] args){       

        ArrayList<Abug2> newbug1 = new ArrayList<Abug2>();

        String choice = JOptionPane.showInputDialog(null, "Would you like to add another bug?: ");
            do{for (int i = 0; i < 3; i++) {

                newbug1.add(new Abug2("Bug", "Spider", 's'));

                newbug1[i].setspecies();
                newbug1[i].setname();
                newbug1[i].setsymbol();

                System.out.println(newbug1[i].toString());

            }       }while(choice != "yes");
            }

}

【问题讨论】:

  • 您不能使用下标访问ArrayList 元素。 newbug1[i] 用于数组。

标签: java arrays arraylist


【解决方案1】:

对于数组列表,请改用get()

newbug1.get(i).setspecies();
newbug1.get(i).setname();
newbug1.get(i).setsymbol();

因为它存储对象引用,任何setFoo 调用都会影响arraylist 中引用的原始对象。

【讨论】:

    【解决方案2】:

    为了访问 ArrayList 中的元素,您必须使用称为 get 的方法。

    在您的代码中,将 newbug1[i] 替换为 newbug1.get(i)

    此外,您应该将该引用存储在一个变量中,而不是一次又一次地调用它:

    Abug2 currentBug = newbug1.get(i);
    currentBug.setSpecies();   
    

    您的代码将变得更加清晰。

    【讨论】:

      猜你喜欢
      • 2021-07-24
      • 2019-10-25
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2016-02-20
      • 1970-01-01
      • 2012-04-20
      • 2018-08-14
      相关资源
      最近更新 更多