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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 26, 20262026-05-26T10:08:17+00:00 2026-05-26T10:08:17+00:00

do{ System.out.println(inside do); for (int i = 0; i < i2; i++) { String

  • 0
        do{

        System.out.println("inside do");
        for (int i = 0; i < i2; i++) {

             String s2 = m_area.m_items.get(i).returnName();
             if (s2.contains(s)) {

                itemexist_check = true;
                player.addItem(m_area.m_items.get(i));
                m_area.m_items.remove(i);
                } else {

                 //do nothing
             }
         }
         itemexist_check = true;
        }while(itemexist_check == false);

When this do statement is run, it will happily perform the “player.addItem(m_area.m_items.get(i))”, but when it gets to “m_area.m_items.remove(i)” (m_items is a LinkedList) , it throws an “OutOfBoundsException: Index 1, Size 1”. Via printing the size of the LinkedList, as well as printing the value of “i”, I have determined that prior to the “m_area.m_items.remove(i)”, the size of the LinkedList is 2, and the value of “i” is 0. I understood why it threw the exception. There is obviously no Element 0 in the LinkedList. What I don’t know is, why didn’t it throw the exception on the “player.addItem(m_area.m_items.get(i))”? Something isn’t right here, please help.

Thank you

  • 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-26T10:08:17+00:00Added an answer on May 26, 2026 at 10:08 am

    The savest way to deal with list traversal + removal is to separate the two. Hence, iterate through your list using a standard forearch loop (also much easier and faster than get calls). Instead of removing the item, add it to a separate list and after your loop use m_area.m_items.removeAll(removedElementsList).

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

Sidebar

Related Questions

Could someone please explain why this happening: class Apple { String type; setType(){ System.out.println(inside
This code: System.out.println(String.format(%f, Math.PI)); System.out.println(Math.PI); produces that result on my computer: 3,141593 3.141592653589793 Why
System.out.println( Arrays.deepToString( abc<def>ghi.split((?:<)|(?:>)) ) ); This prints [abc, def, ghi] , as if I
System.out.println(matcher.group(1)); System.out.println(matcher.group()); I like to know what's the difference between the above two codes.
class A { void F() { System.out.println(a); }} class B extends A { void
Object o = new Long[0] System.out.println( o.getClass().isArray() ) System.out.println( o.getClass().getName() ) Class ofArray =
public abstract class Master { public void printForAllMethodsInSubClass() { System.out.println (Printing before subclass method
public class Animal { public void eat() { System.out.println("I eat like a generic Animal.");
The output of the fist System.out.println() is not same as the second System.out.println() What
Many times I saw logging of errors like these: System.out.println(Method aMethod with parameters a:+a+

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.