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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 23, 20262026-05-23T15:40:17+00:00 2026-05-23T15:40:17+00:00

I have an array list containing 5 bulbs. I can iterate throuh them like

  • 0

I have an array list containing 5 bulbs. I can iterate throuh them like this

for(Bulb bul : list){
    System.out.println(bul.id);
}

No a bulb is switched off/on. The effect is that its neighbourg bulbs a switch too.

My problem is that when the last or 4th bulb are switched I need to determine its neighbours. Since I have 5 bulbs this would work.

int bulbIdClicked = 3;

if(bul.id == (bulbIdClicked + 1)%5)

if(bul.id == (bulbIdClicked - 1)%5)

For 3 it would give me 2 and 4 as neighbours. But when 4 is switched it gives me 3 and 0 ans neighbours where 0 should be 5.

How can I solve that problem?

  • 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-23T15:40:17+00:00Added an answer on May 23, 2026 at 3:40 pm

    If you have a bulb ID ranging from 0 to 4, the best way to get the next and previous IDs is to use:

    next = (id + 1) % 5
    prev = (id + 4) % 5
    

    This is language-agnostic since not all languages treat modulus operators on negative numbers the same. You can see that stepping forward 4 from 4 (for example) gives you: 0, 1, 2, 3 which is the same as a step backwards.

    However. modulus really only works on zero-based values. Since you have one-based values, you can subtract one first, do the relevant addition/modulo, then add one again.

    next = ((id - 1) + 1) % 5 + 1
    prev = ((id - 1) + 4) % 5 + 1
    

    These simplify down to:

    next = id % 5 + 1
    prev = (id + 3) % 5 + 1
    

    Using those formula, you get:

    id  next  prev
    --  ----  ----
     1    2     5
     2    3     1
     3    4     2
     4    5     3
     5    1     4
    

    as expected.

    That’s about as optimised as you’re likely to get without a lookup table. You can use the same approach for any roll-over size (not just 5), you just have to change the modulo and what you add.

    If the indexes range from 1 to N, its:

    next = id % [N] + 1
    prev = (id + [N-2]) % [N] + 1
    

    where the figures inside [] are constant based on the number of indexes.

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

Sidebar

Related Questions

Closed as exact duplicate of this question . I have an array/list of elements.
I have an array containing a list of files. I want to sort it
I have an array containing a list of file paths that I want to
I have an arraylist called 'pri1', I want this array list to be available
I have an array which is a list of domains, I want to print
I have an array that contains a list of vertices which I copy to
Suppose I have a collection (be it an array, generic List, or whatever is
I have a List containing a lot of paths. I have a specific path
So, I have a list containing a custom class, MyClass MyClass has properties, which
I have two files, one containing an array in PHP that is echo ed

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.