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

  • Home
  • SEARCH
  • 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 8495515
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 10, 20262026-06-10T23:35:43+00:00 2026-06-10T23:35:43+00:00

How to check the sizeof array list in java without get error exception from

  • 0

How to check the sizeof array list in java without get error exception from the compiler if the arraylist is not been initialised.
I have tried the following but it does not work. and I got this message:
Exception in thread “main” java.lang.IndexOutOfBoundsException: Index: 1, Size: 1

ArrayList<String> str = new ArrayList<>();
if (str.size()>0)
  • 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-06-10T23:35:44+00:00Added an answer on June 10, 2026 at 11:35 pm

    There is nothing in the code you posted that would throw an IndexOutOfBoundsException. I assume you were calling str.get(0) without the size check before?

    Anyway, your code is fine, i.e. you should be able to extend it to do something like the following:

    List<String> someStrings = new ArrayList<String>();
    ...
    if (someStrings.size() > 0) {
        System.out.println("First element of list is: " + someStrings.get(0));
    } else {
        System.out.println("List is empty");
    }
    

    However, for cleanliness I would suggest using if (!someStrings.isEmpty()) instead, which checks the same thing as if (someStrings.size() > 0).

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

Sidebar

Related Questions

How do I check for null in a array list and remove them? It
Anytime I try to serialize a file I get the error: FileNotFound. Not sure
I notice that when using sys.getsizeof() to check the size of list and dictionary,
**Have it working now. I forgot to populate the Array List. How embarrassing. I'm
I have a series of exception messages: enum myEnum { BASIC(1, An error occurred),
So I have this array list that is populated using a csv file. Once
I have a JSP page where I load an arraylist which has the list
List<String> listStr = new ArrayList<String>(); if(listStr.size == 0){ } versus if(listStr.isEmpty()){ } In my
Which is the most reliable way to check if a character array is empty?
I pull a list of address from a DB. Where the size of the

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.