未定义的参数在返回时使用 INT

huangapple 未分类评论53阅读模式
英文:

Undefined arguments in return with INT

问题

以下是翻译好的代码部分:

  1. public final class PhpArray extends AbstractMap
  2. {
  3. private TreeMap t;
  4. private HashMap m;
  5. public PhpArray() {
  6. this.t = new TreeMap(Request.PHP_ARRAY_KEY_COMPARATOR);
  7. this.m = null;
  8. }
  9. @Override
  10. public Object put(final Object key, final Object value) {
  11. if (this.m != null) {
  12. return this.m.put(key, value);
  13. }
  14. try {
  15. return this.t.put(key, value);
  16. }
  17. catch (ClassCastException e) {
  18. this.m = new HashMap(this.t);
  19. this.t = null;
  20. return this.m.put(key, value);
  21. }
  22. }
  23. @Override
  24. public Set entrySet() {
  25. if (this.t != null) {
  26. return this.t.entrySet();
  27. }
  28. return this.m.entrySet();
  29. }
  30. public int arraySize() {
  31. if (this.t == null) {
  32. throw new IllegalArgumentException("The passed PHP \"array\" is not a sequence but a dictionary");
  33. }
  34. if (this.t.size() == 0) {
  35. return 0;
  36. }
  37. return 1 + this.t.lastKey();
  38. }
  39. }

请注意,为了保持代码的完整性和一致性,我已经将代码中的注释和错误消息翻译成了英文。如果您需要更多帮助或解释,请随时提问。

英文:

i have the code like this when i create it like this

  1. public final class PhpArray extends AbstractMap
  2. {
  3. private TreeMap t;
  4. private HashMap m;
  5. public PhpArray() {
  6. this.t = new TreeMap(Request.PHP_ARRAY_KEY_COMPARATOR);
  7. this.m = null;
  8. }
  9. @Override
  10. public Object put(final Object key, final Object value) {
  11. if (this.m != null) {
  12. return this.m.put(key, value);
  13. }
  14. try {
  15. return this.t.put(key, value);
  16. }
  17. catch (ClassCastException e) {
  18. this.m = new HashMap(this.t);
  19. this.t = null;
  20. return this.m.put(key, value);
  21. }
  22. }
  23. @Override
  24. public Set entrySet() {
  25. if (this.t != null) {
  26. return this.t.entrySet();
  27. }
  28. return this.m.entrySet();
  29. }
  30. public int arraySize() {
  31. if (this.t == null) {
  32. throw new IllegalArgumentException("The passed PHP \"array\" is not a sequence but a dictionary");
  33. }
  34. if (this.t.size() == 0) {
  35. return 0;
  36. }
  37. return 1 + this.t.lastKey();
  38. }
  39. }

but when i update my project i got error in the code

return 1 + this.t.lastKey();

the error is an arguments + is undefined.. why like that ? and how to fix the problem ?

答案1

得分: 1

TreeMap是一个泛型类,但在您问题中的代码中,您使用了没有类型参数的形式。这意味着您代码中的这行:

  1. private TreeMap t;

实际上等同于这行:

  1. private TreeMap<Object, Object> t;

换句话说,t.lastKey()返回一个Object,而运算符+不能用于Object,因为Object不是一个数字。

也许您想要调用size()方法而不是lastKey()方法?

也许这个教程会有帮助?

英文:

TreeMap is a generic class but in the code in your question you have used it without type parameters. This means that this line of your code:

  1. private TreeMap t;

is essentially this:

  1. private TreeMap<Object, Object> t;

In other words t.lastKey() returns an Object and the operator + can't be used with Object because an Object is not a number.

Perhaps you meant to call method size() rather than method lastKey()?

Perhaps this tutorial will help?

huangapple
  • 本文由 发表于 2020年7月27日 17:54:53
  • 转载请务必保留本文链接:https://java.coder-hub.com/63112837.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定