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 6575391
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 25, 20262026-05-25T15:22:53+00:00 2026-05-25T15:22:53+00:00

I have an ArrayList, and we’ll say it can be at most size 5.

  • 0

I have an ArrayList, and we’ll say it can be at most size 5. I want to assign the first element to var1, the second to var2, the third to var3, etc.

However, sometimes the ArrayList will have less than 5 items, in which case I won’t be assigning some variables a value.

So my question is, is there a better way to do this other than:

if (myArrayList.size() > 0)
    var1 = myArrayList.get(0);
if (myArrayList.size() > 1)
    var2 = myArrayList.get(1);
if (myArrayList.size() > 2)
    var3 = myArrayList.get(2);
if (myArrayList.size() > 3)
    var4 = myArrayList.get(3);
if (myArrayList.size() > 4)
    var5 = myArrayList.get(4);
  • 1 1 Answer
  • 0 Views
  • 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-25T15:22:53+00:00Added an answer on May 25, 2026 at 3:22 pm

    The source of this is most certainly a bad code design. Also to what do you initialize the variable if the arraylist doesn’t contain the field (after all you use it later)? A larger example or what exactly you’re trying to do would help here. Usually just using

    But I can think of at least two ways to do this:

        switch(myArrayList.size()) {
        case 5:
            var4 = myArrayList.get(4);
        case 4:
            var3 = myArrayList.get(3);
        case 3:
            var2 = myArrayList.get(2);
                // and so on
        }
    

    or just use a try/catch.

        try {
            var0 = myArrayList.get(0);
            var1 = myArrayList.get(1);
        }
        catch(IndexOutOfBoundsException ex){
        }
    

    But most certainly it’s better to use the arraylist itself and just padd it with the default values you’d otherwise use for your variables.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have an ArrayList l1 of size 10. I assign l1 to new list
I have an ArrayList<String> , and I want to remove repeated strings from it.
I have an ArrayList that I want to use to hold RaceCar objects that
I have an arraylist (eg: h1) contain some urls. eg: http://www.google.com,http://www.yahoo.com,http://www.xyz.com ,..... How can
I have an ArrayList in my Servlet code. Now, I want to show that
I have an Arraylist of HashMap . Each HashMap element contains two columns: column
i have an arraylist and i want to add and remove many items at
I have an Arraylist . If user enter the same number secondly I want
I have an ArrayList and want to show items present in the arraylist inside
I have the arraylist which has some process names like Notepad, mspaint I want

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.