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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 24, 20262026-05-24T00:32:45+00:00 2026-05-24T00:32:45+00:00

So I have an Array with a length of 4. When I increment it

  • 0

So I have an Array with a length of 4. When I increment it by 1 and the number gets bigger than the length of the array, I’d like it to rollover.

For example:

current_index = 3;
current_index++;
//current_index is now 0 again

current_index = 3;
current_index += 2;
//current_index would be 1

current_index = 0;
current_index--;
//current_index would be 3

I’m currently solving it with if-else like this

if (current_index == textviewlist.length + 1)
     current_index = 0;
else if (current_index == textviewlist.length + 2)
     current_index = 1;
else if (current_index == -1)
     current_index = 3;

But I feel like this isn’t an appropriate solution, or “good” code.

Edit: I tried your suggestion, but apparently java behaves strangely with negative numbers.
When I try

current_index = (current_index - 1) % textviewlist.length;

Java takes the index “0”, decreases it by 1 (“-1”) and then

 -1 % 4 = -1

I expected it to be 3, see Wolfram Alpha: -1 mod 4
But apparently the java % operator is not the same as the modulo operator?

Edit 2: I found a solution here: Best way to make Java’s modulus behave like it should with negative numbers? – Stack Overflow

I can just do:

current_index -= 1;
current_index = (current_index % textviewlist.length + textviewlist.length)  % textviewlist.length;
  • 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-24T00:32:46+00:00Added an answer on May 24, 2026 at 12:32 am

    You can use the modulo operator.

    current_index = (current_index + n) % 4;
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have a character array of length 32 and would like to take certain
Now I currently have an array that contains 120 pairs. For example: [[1, 12],
I have an array of objects of variable length n. Defined by the number
I have an array like this. I need to add the total length of
I have an array: [0, 5, 6, 0, 0, 2, 5] I would like
Lets say I have an array of length n and I want to pick
Basically, I have an array like this: val base_length = Array( 0, 1, 2,
I have an array: data.Dealer.car[0] data.Dealer.car[1] data.Dealer.car[2] If I do this: alert(data.Dealer.car.length); delete data.Dealer.car[1];
Say you have an array, data, of unknown length. Is there a shorter method
I have a for loop that loops over one array... for i=1:length(myArray) In this

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.