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

标签 java android compiler-errors

我正在制作我的个人 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;
}

歌剧类:

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 来保存这些值。

<小时/>

已添加

在这个类中必须有一个“numeros”和一个“operacoes”类属性。 在提供的代码 fragment 中,没有显示它们是如何创建的。

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

关于java - 删除列表的最后一个元素在JAVA中不起作用,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/29674067/

相关文章:

java - Maven - 错误 StatusLogger 找不到 log4j2 配置文件

java - 如何验证基于 soap 的 Java Web 服务?

android - 登录成功后如何开始新 Activity ?

Android Room 无法创建用于测试迁移的 JSON 模式

objective-c - ARC 禁止合成具有未指定所有权或存储的属性

amazon-s3 - 无法在Raspberry Pi 3上编译Riak

java - 尝试在 Apache Tomcat 上部署 GWT 失败

java - 在任何标准库中是否有帮助程序类实现对 boolean 集合的逻辑操作?

android - 如何在 flutter 中按 List<String> 对 List<Type> 进行排序

javascript - 省略分号时出错 - 有人可以解释为什么吗?