简体   繁体   English

赋值表达式的编译错误

[英]Compilation error with assignment expression

How do I switch every two items in an array list? 如何切换数组列表中的每两个项目?

For example: "hi", "how", "are", "you" becomes: 例如:“ hi”,“ how”,“ are”,“ you”将变为:

"how", "hi", "you", "are" “如何”,“嗨”,“您”,“是”

This is my error (in practice-it): 这是我的错误(在实践中):

The compiler found a data type it was not expecting here. 编译器找到了此处未期望的数据类型。 Sometimes this error occurs when you mistake = for == when comparing values 有时在比较值时将=误认为= =时会发生此错误

unexpected type 意外类型
required: variable 必需:变量
found : value 发现:价值
list.get(i) = list.get(i+1); list.get(i)= list.get(i + 1);

unexpected type 意外类型
required: variable 必需:变量
found : value 发现:价值
list.get(i+1) = temp; list.get(i + 1)=温度

This is what I have: 这就是我所拥有的:

    public void switchPairs(ArrayList<String> list){
        String temp = "";
        for(int i = 0; i<= list.size(); i+2){
            temp = list.get(i);
            list.get(i) = list.get(i+1);
            list.get(i+1) = temp;
        }
    }

You are attempting to use the return of the get method as a variable. 您试图将get方法的返回值用作变量。 Unlike array access expressions, eg arr[i] = value , which are legal, the results of method calls cannot be used this way. 与合法的数组访问表达式(例如arr[i] = value ,不能以这种方式使用方法调用的结果。 You must use the set method instead. 您必须改为使用set方法

temp = list.get(i);
list.set(i, list.get(i + 1));
list.set(i + 1, temp);

This will fix the compiler error, but running this will guarantee an IndexOutOfBoundsException . 这将解决编译器错误,但是运行此命令将保证IndexOutOfBoundsException

If the list size is even, then list.get(i) will throw an IndexOutOfBoundsException when i reaches list.size() . 如果列表大小是偶数,那么当i到达list.size()时, list.get(i)会抛出IndexOutOfBoundsException Remember that valid indexes are from 0 through size() - 1 . 请记住,有效索引从0size() - 1

If the list size is odd, then list.get(i + 1) will throw an IndexOutOfBoundsException . 如果列表大小为奇数,则list.get(i + 1)将抛出IndexOutOfBoundsException

You must alter your for loop condition to stop before i and i + 1 go out of bounds. 您必须更改for循环条件才能停止,直到i i + 1超出范围。 (The increment needs += to have an effect here.) (增量需要+=才能在此处生效。)

for(int i = 0; i < list.size() - 1; i+=2){

That will leave the last item in a list of odd size untouched. 这将使原始大小列表中的最后一项保持不变。

Because every method return a value , not a variable . 因为每个方法都返回一个value ,而不是一个variable

and the left operand of an assignment operator must be a variable, or a compile-time error occurs. 并且赋值运算符的左操作数必须是变量,否则会发生编译时错误。

You can think like this way: If the we can modify a variable via it's getter, that encapsulation makes nonsense, Since usually there is a getter for a private attribute. 您可以这样想:如果我们可以通过它的getter修改变量,那么封装就变得毫无意义了,因为通常私有属性都有一个getter。

In a word, in this case(and many other cases), you should use a setter to achieve your gold. 简而言之,在这种情况下(以及许多其他情况下),您应该使用二传手来取得成功。 Like this: 像这样:

list.set(i, "something you want");

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

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