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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 17, 20262026-06-17T17:42:44+00:00 2026-06-17T17:42:44+00:00

If I search for the 5th item in my arrayList, I also want to

  • 0

If I search for the 5th item in my arrayList, I also want to get the 4th and 6th. The code for this is provided in the final if statement and is define as (i – 1) and (i + 1). Here is my code so far:

import java.util.ArrayList;

public class PlanetsList {
public static void main(String args[]) {

    ArrayList<String> planets = new ArrayList<String>();
    String names[] = { "Mercury", "Venus", "Earth", "Mars", "Jupiter", "Saturn", "Uranis", "Neptune", "Pluto"};

    for (int i = 0, n = names.length; i < n; i++) {
        planets.add(names[i]);
        String value = (String) planets.get(i);

        if(value.contains("Mars")) {
            String newNum = value.replace(value, "Red planet ");
            planets.set(i,newNum);     
        }
        if(value.contains("Uranis")) {
            String wordBefore = (String) planets.get(i-1);
            String wordAfter = (String) planets.get(i+1);
            String newNum = value.replace(value, "Uranus ");
            planets.set(i,newNum);
            System.out.println("This is the word before " + wordBefore);
            System.out.println("This is the word after " + wordAfter);
            planets.remove(i-1);  
        }
    }
    System.out.println(planets);
}
}

With this I get an indexoutofbounds exception, which is obviously caused by the wordAfter line in the final if statement, and because the for loop hasn’t looped through the entire arrayList. The final if statement doesn’t need to be in the same for loop as the other if statement, but if it is in a different loop, the replace method must be able to put the replaced word back in the correct position.

Regards

  • 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-17T17:42:45+00:00Added an answer on June 17, 2026 at 5:42 pm

    Complete adding the strings into your list and then try iterating over list again for your logic. You’re trying get(i+1) when i is the max added to your list.

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

Sidebar

Related Questions

Search for good JAVA lib for playing with POST-GET requests - Is there any
A search API is returning date in String format,I want to compare that date
I search for a lot of question here and in google also but still
I search a simple/clean way to get every element with the same classes than
What search do when they found different status code? 302 301 404 etc... I
I search and get 10 records as result.I have two drop down boxes.one will
I have a simple search text box. This text box acts as a filter.
The Javascript timer event has this basic syntax: var t=setTimeout(javascript statement,milliseconds); I have this
The 5th rule down on this page is triggered by the URL: /vehicles/Ford/Escape/2005/ BUT
I found some jQuery code as a result of a Google search that allowed

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.