简体   繁体   English

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

[英]removing last element of list not working JAVA

I'm doing my personal android calculator just for fun to learn Java and Android. 我正在做我的个人android计算器,只是为了好玩学习Java和Android。

But the function is not working... the String list operacoes is where I have, in sequence the operators i will need (+,-,x and /). 但是该功能不起作用...字符串列表操作operacoes是我所需要的,依序需要我使用运算符(+,-,x和/)。 And the String list numeros is where I have the numbers to do the operations like for example: 32 45 232 12. 字符串列表numeros是我执行操作的数字的位置,例如:32 45 232 12。

Maybe what I want to do in the code is hard to explain but I'll give it a try: I'm trying to do the x or / operations first and then reduce 1 element in numeros and operacoes list, and only after all of them are done I will go to + and - operations. 也许我想在代码中做的事情很难解释,但我将尝试一下:我先尝试执行x或/运算,然后在numerosoperacoes列表中减少1个元素,仅在所有它们完成后,我将转到+和-操作。

But it gives errors and I don't know what is wrong. 但这会带来错误,我不知道哪里出了问题。 Does someone has a clue? 有人有线索吗?

The android compiler says: 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)

And code is: 代码是:

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

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

}

ADDED 添加

I had to do this on the declaration of the String lists: 我必须在字符串列表的声明中执行此操作:

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

It works now! 现在可以使用!

You are probably working with an immutable array . 您可能正在使用不可变数组

The java.util.Arrays.ArrayList version is immutable and its remove() method is not overridden. java.util.Arrays.ArrayList版本是不可变的,并且其remove()方法不会被覆盖。 As such, it defers to the AbstractList implementation of remove(), which throws an UnsupportedOperationException. 因此,它遵循remove()的AbstractList实现,该实现会引发UnsupportedOperationException。

Just see where you are creating the "numeros" or the "operacoes" array . 只需查看在哪里创建“ numeros”或“ operacoes”数组即可 If you want to work with a mutable array, instantiate a java.util.ArrayList instead to hold those values. 如果要使用可变数组,请实例化一个java.util.ArrayList来保存这些值。


ADDED 添加

In this class there must be a "numeros" and a "operacoes" class properties. 在该类中,必须有一个“ numeros”和一个“ operacoes”类属性。 In the code snippet provided, it's not shown how they are created. 在提供的代码段中,未显示它们的创建方式。

Find where they are instantiated, and use a new Arraylist() . 找到它们的实例化位置,然后使用new Arraylist()


ADDED 添加

Here is your constructor 这是您的构造函数

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

So you are receiving the numeros and operacoes as parameters. 因此,您将收到numerosoperacoes作为参数。

The best way would be to see who is sending them and why they are immutable. 最好的方法是查看谁发送了它们,以及为什么它们不可变。

The easy way, just do this: 简单的方法就是这样做:

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM