Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 5981269
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 22, 20262026-05-22T21:55:14+00:00 2026-05-22T21:55:14+00:00

In Java, I find the following code much cleaner and easier to maintain than

  • 0

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?

  • 1 1 Answer
  • 1 View
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-22T21:55:15+00:00Added an answer on May 22, 2026 at 9:55 pm

    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];
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I've got the following Java code but List.indexOf() seems to do pretty much the
I have the following java code fragment while (condition1){ switch (someinteger){ case 1: if(condition2)
I find the following code remarkable in Java: ArrayList<String> l1 = new ArrayList<String>(); ArrayList<Integer>
I'm trying to find a Java library to highlight code. I don't want to
I have following code in Java: import java.util.*; public class longest{ public static void
I found the following code example for Java on RosettaCode : public static boolean
I understand that the following code can (perhaps not very efficiently) find out a
The following code matches the two expressions and prints success. import java.util.regex.Matcher; import java.util.regex.Pattern;
I downloaded the selenium-java-2.0a5.zip http://code.google.com/p/selenium/downloads/list and ran the following code: package org.openqa.selenium.example; import org.openqa.selenium.By;
I'm trying to mimic the following Java code: int[][] multi; // DIMENSIONS ARE UNKNOWN

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.