匿名数组索引而不是 switch 语句?

发布于 2024-11-14 01:09:16 字数 684 浏览 9 评论 0原文

在Java中,我发现下面的代码比相应的笨重的switch语句更干净、更容易维护:

try {
  selectedObj = new Object[] {
    objA,
    objB,
    objC,
    objD,
  }[unvalidatedIndex];
} catch (ArrayIndexOutOfBoundsException e) {
  selectedObj = objA;
}

switch (unvalidatedIndex) {
  case 0:
    selectedObj = objA;
    break;

  case 1:
    selectedObj = objB;
    break;

  case 2:
    selectedObj = objC;
    break;

  case 3:
    selectedObj = objD;
    break;

  default:
    selectedObj = objA;
}

Is the前者被认为是可接受的做法相反吗?我知道这不是最有效的,因为它涉及分配数组和捕获异常。当 unvalidatedIndex 超出范围时(尽管异常已处理),是否会导致不良情况?

如果可能的话,你会推荐一些更干净的东西吗?

In Java, I find the following code much cleaner and easier to maintain than the corresponding bulky switch statement:

try {
  selectedObj = new Object[] {
    objA,
    objB,
    objC,
    objD,
  }[unvalidatedIndex];
} catch (ArrayIndexOutOfBoundsException e) {
  selectedObj = objA;
}

opposed to

switch (unvalidatedIndex) {
  case 0:
    selectedObj = objA;
    break;

  case 1:
    selectedObj = objB;
    break;

  case 2:
    selectedObj = objC;
    break;

  case 3:
    selectedObj = objD;
    break;

  default:
    selectedObj = objA;
}

Is the former considered an acceptable practice? I am aware that it's not the most efficient one as it involves allocating an array and catching an exception. Would it cause something undesirable when unvalidatedIndex is out of range (although the exception is handled)?

If possible, would you suggest something cleaner?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(6

遮云壑 2024-11-21 01:09:16

你的第一个方法很好。

不过,最好先检查一下索引:

Object[] arr = new Object[] { ... };

if (i < 0 || i >= arr.length)
    i = 0;
selectedObj = arr[i];

Your first approach is fine.

However, it is better to check the index first:

Object[] arr = new Object[] { ... };

if (i < 0 || i >= arr.length)
    i = 0;
selectedObj = arr[i];
想念有你 2024-11-21 01:09:16

这不是一种可接受的做法。异常用于错误处理,而不是程序流程。
异常也非常慢。

It is not an acceptable practice. Exceptions are for errors handling, not for program flow.
Also exceptions are VERY SLOW.

酒浓于脸红 2024-11-21 01:09:16

两者都是反模式。只需亲自测试范围成员资格的索引即可。在许多实际情况下可能有一种使用enum的方法。

Both are antipatterns. Just test the index for range membership yourself. There might be a way to use an enum in many actual cases.

零崎曲识 2024-11-21 01:09:16

就我个人而言,尽管我毫不怀疑有些人会不同意,但我会这样做:

switch (unvalidatedIndex) {
    case 0 : selectedObj = objA; break;
    case 1 : selectedObj = objB; break;
    case 2 : selectedObj = objC; break;
    case 3 : selectedObj = objD; break;
    default: selectedObj = objA; break;
    }

它干净、紧凑、高效,而且非常容易理解。

我会犹豫是否包含 case 0,即 default 情况。

Personally, though I have no doubt some will disagree, I would do:

switch (unvalidatedIndex) {
    case 0 : selectedObj = objA; break;
    case 1 : selectedObj = objB; break;
    case 2 : selectedObj = objC; break;
    case 3 : selectedObj = objD; break;
    default: selectedObj = objA; break;
    }

It's clean, compact, efficient, and really easy to understand.

I would hesitate to include the case 0, that being the default case.

以酷 2024-11-21 01:09:16

怎么样

if(index < arr.length && index >= 0){
    obj = arr[index];
}else{
    obj = defaultValue;
}

How about

if(index < arr.length && index >= 0){
    obj = arr[index];
}else{
    obj = defaultValue;
}
四叶草在未来唯美盛开 2024-11-21 01:09:16
    int index = 4;

    ArrayList<String> myObjects = Lists.newArrayList("a", "b", "c", "d");
    Object o = index  < myObjects.size() && index >= 0 ? myObjects.get(index) : null;
    System.out.println(o);

列表来自 Guava。

    int index = 4;

    ArrayList<String> myObjects = Lists.newArrayList("a", "b", "c", "d");
    Object o = index  < myObjects.size() && index >= 0 ? myObjects.get(index) : null;
    System.out.println(o);

Lists comes from Guava.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文