I am receiving a compiler warning that says local variable 'newElement' is redundant
. How can I write the following code more efficiently to rid my code of this warning?
Element newElement = new Element(left,elements.next);
elements.next = newElement;
Say you have a list a -> b -> c
and now you want to add d
to it. What you have written tries to make d
point to d
.
This block
Element newElement = new Element(left,elements.next);
elements.next = newElement;
is equivalent to
elements.next = new Element(left,elements.next);
which really doesn't make a bunch of sense.
If you are trying to make something like a linked list,
Element newElement = new Element(left,null);
elements.next = newElement;
Sometimes having a redundant variable is desirable (perhaps for code clarity). In these cases the warning can be suppressed.
@SuppressWarnings("UnnecessaryLocalVariable")
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.