我正在做我的个人android计算器,只是为了好玩学习Java和Android。

但是该功能无法正常工作...字符串列表operacoes是我所需要的位置,依序需要我使用运算符(+,-,x和/)。字符串列表numeros是我执行数字操作的地方,例如:32 45 232 12。

也许我想在代码中执行的操作很难解释,但我将尝试一下:我尝试先执行x或/操作,然后在numerosoperacoes列表中减少1个元素,并且仅在所有他们完成了,我将去+和-操作。

但这会带来错误,我不知道哪里出了问题。有人有线索吗?

android编译器说:

 .UnsupportedOperationException
        at java.util.AbstractList.remove(AbstractList.java:638)
        at nunoprograms.com.nunoprogram.Operacoes.conta(Operacoes.java:54)
        at nunoprograms.com.nunoprogram.App2Activity.onClick(App2Activity.java:222)
        at android.view.View.performClick(View.java:4756)
        at android.view.View$PerformClick.run(View.java:19749)
        at android.os.Handler.handleCallback(Handler.java:739)
        at android.os.Handler.dispatchMessage(Handler.java:95)
        at android.os.Looper.loop(Looper.java:135)
        at android.app.ActivityThread.main(ActivityThread.java:5221)
        at java.lang.reflect.Method.invoke(Native Method)
        at java.lang.reflect.Method.invoke(Method.java:372)
        at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:899)
        at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:694)

代码是:
public int conta(){

    int j,num1,num2,total=0;

    num1=Integer.parseInt(this.numeros.get(0));
       int x=this.operacoes.size();
       int y=0;

       for(int i=0;i<x-y;i++){//Objective: priority to x and / operations

        if(this.operacoes.get(i).equals("x")||this.operacoes.get(i).equals("/")) {
            if (this.operacoes.get(i).equals("x"))
                total = Integer.parseInt(this.numeros.get(i)) * Integer.parseInt(this.numeros.get(i+1));
            if (this.operacoes.get(i).equals("/"))
                total = Integer.parseInt(this.numeros.get(i)) / Integer.parseInt(this.numeros.get(i+1));

            this.numeros.set(i,String.valueOf(total));//atribui o valor da operaçao

            for (j=i;j<this.operacoes.size()-1;j++)this.numeros.set(j+1,this.numeros.get(j+2));
            for (j=i;j<this.operacoes.size()-1;j++)this.operacoes.set(j,this.operacoes.get(j+1));
            this.numeros.remove(this.numeros.size()-1);//remove last element
            this.operacoes.remove(this.operacoes.size()-1);
            y++;
        }
        }
    //now the + and - operations
    total=num1;
    for(int i=0;i<this.operacoes.size();i++) {

      num2=Integer.parseInt(this.numeros.get(i+1));
        System.out.println("conteudo da string de operacoes: "+ this.operacoes.get(i));

        if(this.operacoes.get(i).equals("+"))total+=num2;
        else if(this.operacoes.get(i).equals("-"))total-=num2;

    }
return total;
}

歌剧类:
public class Operacoes {

    private int numeros_size,operacoes_size;
    private List<String> numeros;
    private List<String> operacoes;

    Operacoes(List<String> x,int x_size, List<String> y, int y_size){
        this.numeros=x;
        this.numeros_size=x_size;
        this.operacoes=y;
        this.operacoes_size=y_size;
    }

    public int conta(){
    ...
    }

}

添加

我必须在String列表的声明中执行此操作:
Operacoes(List<String> x,int x_size, List<String> y, int y_size){
    this.numeros=new ArrayList(x);
    this.numeros_size=x_size;
    this.operacoes=new ArrayList(y);
    this.operacoes_size=y_size;
}

现在可以使用!

最佳答案

您可能正在使用不变数组



只需查看要在哪里创建“numeros”或“operacoes”数组即可。如果要使用可变数组,请实例化java.util.ArrayList来保存这些值。

已添加

在该类中,必须有一个“numeros”和一个“operacoes”类属性。
在提供的代码段中,未显示它们的创建方式。

找到它们的实例化位置,然后使用new Arraylist()

已添加

这是您的构造函数

Operacoes(List<String> x,int x_size, List<String> y, int y_size){
    this.numeros=x;
    this.numeros_size=x_size;
    this.operacoes=y;
    this.operacoes_size=y_size;
}

因此,您将收到numerosoperacoes作为参数。

最好的方法是查看谁发送了它们,以及为什么它们不可变。

简单的方法就是这样做:
Operacoes(List<String> x,int x_size, List<String> y, int y_size){
    this.numeros= new ArrayList(x);
    this.numeros_size=x_size;
    this.operacoes= new ArrayList(y);
    this.operacoes_size=y_size;
}

10-08 02:52