删除列表的最后一个元素不起作用 JAVA

removing last element of list not working JAVA

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

但是该函数不起作用...字符串列表 operacoes 是我所拥有的,按顺序是我需要的运算符(+、-、x 和 /)。字符串列表 numeros 是我有数字来执行操作的地方,例如:32 45 232 12.

也许我想在代码中做的事情很难解释,但我会试一试:我试图先做 x 或 / 操作,然后在 numeros 中减少 1 个元素和 operacoes 列表,只有在所有这些都完成后,我才会去 + 和 - 操作。

但是它给出了错误,我不知道哪里出了问题。有人知道吗?

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;
}

歌剧Class:

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(){
    ...
    }

}

已添加

我必须在字符串列表的声明中这样做:

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;
}

现在可以使用了!

您可能正在使用 不可变数组

The java.util.Arrays.ArrayList version is immutable and its remove() method is not overridden. As such, it defers to the AbstractList implementation of remove(), which throws an UnsupportedOperationException.

只需查看您在何处创建 "numeros" 或 "operacoes" 数组。如果您想使用可变数组,请实例化一个 java.util.ArrayList 而不是保存这些值。


已添加

在这个 class 中必须有一个 "numeros" 和一个 "operacoes" class 属性。 在提供的代码片段中,没有显示它们是如何创建的。

找到它们被实例化的地方,并使用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;
}