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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 25, 20262026-05-25T12:26:31+00:00 2026-05-25T12:26:31+00:00

Why does the following java code result in an infinite loop? import java.util.LinkedList; import

  • 0

Why does the following java code result in an infinite loop?

import java.util.LinkedList;
import java.util.Random;

public class Main {

public static void main(String[] args) {
    int n = 3;

    Random rand = new Random();

    LinkedList<Integer> fields = new LinkedList<Integer>();

    for (int i = 0; i < n*n; i++) {
        fields.add(i);
    }

    while (fields.size() > 0) {

        // Choose Field
        int f = rand.nextInt(fields.size());

        fields.remove((Integer) f);
        System.out.println(fields.size());
    }
}
   }
  • 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-25T12:26:32+00:00Added an answer on May 25, 2026 at 12:26 pm

    The way you use remove, you remove objects by value, not by position.

    Say your list consists of values [0, 1, 2, 3], and you remove 0 and 1 the first two times. Now you have [2, 3], whose size is 2, so you will never remove 3 now!

    To remove by position rather than value, say fields.remove(f). (Notice that f is an integer, while (Integer)f is an object of the type contained in the list container.)

    (Alternatively, for different behaviour, you could continue removing by value, but you should now draw the random number from the range [min, max], where you have to determine the extremal values of the list elements separately. This may take a lot longer, of course, since you will have a lot of “misses” where you don’t remove anything.)

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

Sidebar

Related Questions

I have the following java code: UrlValidator urlValidator = new UrlValidator(); boolean validUrl =
Why does the following code raise the exception shown below? BigDecimal a = new
I am trying the following code on Java: String test = http://asda.aasd.sd.google.com/asdasdawrqwfqwfqwfqwf; String regex
i'm trying to convert the following code from Java to C#. // Replace 0
I found an unusual bug in my code recently through random ad-hoc testing. So,
I have the following two dimensional array in my java program. Integer[][] arrayOfSets =
According to this documentation ( http://java.sun.com/docs/books/jls/third_edition/html/lexical.html , 3.10.6) an OctalEscape will be converted to
I ran into an unexpected result in round-tripping Int32.MaxValue into a System.Single : Int32
In our *EntryPoint.gwt.xml we include the following to get Cross-Site support: <add-linker name=xs />
I have encountered a somewhat baffling problem with the simple task of filling an

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.