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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 22, 20262026-05-22T19:07:10+00:00 2026-05-22T19:07:10+00:00

I have a for loop looping through an ArrayList . If a condition is

  • 0

I have a for loop looping through an ArrayList.

If a condition is met in the for loop:

  • I remove the current element from the ArrayList
  • reduce the size of the ArrayList local variable
  • reduce the for loop’s index like so, checking to ensure that they never go below zero.

A case where we have just removed the last element of the ArrayList:

i = (i > 0) ? i-- : i;

My problem is that the above does not reduce i by 1 when i > 0. I’ve used ternary operators countless times, but never seen this behavior. I’ve tested that i is indeed > 0 and that the i– section is being called. It simply isn’t reducing the value of i. Removing the 0 value check and simply running i--; does indeed reduce i as expected.

EDIT2: Ok, well someone edited out my last edit where I mentioned that I am specifically NOT using a ListIterator in this case due to the performance sensitive nature of the loop itself, being in a critical portion of Android code.

  • 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-22T19:07:11+00:00Added an answer on May 22, 2026 at 7:07 pm

    i-- decrements i, but returns the original value.

    i = i-- will decrement i, then assign it to its original value.

    You should use i - 1.

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

Sidebar

Related Questions

i have a foreach loop when looping through that I am creating a $data
I have a loop that is looping through a document library like in the
I have a variable that is built in loop. Something like: $str = ;
i have two mutable arrays that i am looping through when the objects are
I'm looping through all the songs from an iPhone's music library using the following
I have a Customer table with 200.000+ records which i have to loop through
I have a loop that reads each line in a file using getline() :
I have a loop on page to update an access database that takes 15-20
In my WPF client, I have a loop that calls a WCF service to
I have following foreach-loop: using System.IO; //... if (Directory.Exists(path)) { foreach(string strFile in Directory.GetFiles(path,

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.