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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 3, 20262026-06-03T00:40:19+00:00 2026-06-03T00:40:19+00:00

i was solving problem about the prime numbers, i found solution using Arrays, but

  • 0

i was solving problem about the prime numbers, i found solution using Arrays, but i wanted to use the array list for some issues, one of them is to understand the Arraylist well. but i found that the results are not the same when i use the arraylist, here are the 2 Codes:

//using Arrays

import java.util.Arrays;
public class Prime {
    public static void main(String[] args) {
        primeNumber(100);
    }
    public static void primeNumber(int end) {
        boolean primeNumbers[] = new boolean[end]; 
        for (int i = 0; i < end; i++) {
            primeNumbers[i] = true;
        }
        for (int i = 2; i < primeNumbers.length; i++) {
            if (primeNumbers[i] ) {
                for (int j = i+i; j < primeNumbers.length; j += i) {
                    primeNumbers[j]= false;
                }
            }
        }
        for (int j = 2; j < primeNumbers.length; j++) {
            if (primeNumbers[j]) {
                System.out.println(j);

            }
        }   
    }
}

//Using ArrayList

import java.util.ArrayList;
public class Prime {
    public static void main(String[] args) {
        primeNumber(100);
    }

    public static void primeNumber(int end) {
        ArrayList<Boolean> primeNumbers = new ArrayList<Boolean>(); 
        for (int i = 0; i < end; i++) {
            primeNumbers.add(i,true);
        }
        for (int i = 2; i < primeNumbers.size(); i++) {
            if (primeNumbers.get(i) ) {
                for (int j = i+i; j < primeNumbers.size(); j += i) {
                    primeNumbers.add(j, false);
                }
            }
        }
        for (int j = 2; j < primeNumbers.size(); j++) {
            if (primeNumbers.get(j)) {
                System.out.println(j);

            }
        }

    }
}

Can i know what is the wrong i did with using the Arraylist, and if i wanted to use the arraylist for the same result what should i do.

Great thanks for the help.

  • 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-03T00:40:21+00:00Added an answer on June 3, 2026 at 12:40 am

    Instead of

    primeNumbers.add(j, false);
    

    you should use

    primeNumbers.set(j, false);
    

    since add() adds a new element at the specified position (meaning the ArrayList grows by one element), but set() only sets the value of the element at specified position.

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

Sidebar

Related Questions

I am solving the problem in the interview question about LinkedList. question is... Write
This question is more about guidance than actually solving my problem: I need to
I am solving problem 9 on the Project Euler . In my solution I
I wrote the following code for solving a problem but it doesn't work. The
I have the following problem in my Data Structures and Problem Solving using Java
I need some help in solving this problem. We have a large amount of
I am about to spend a lot of time solving a problem, and I
In problems like this, If I were solving a problem in C using recursion
I have previous experience with MATLAB, but the problem that I face is some
Summary: How would I go about solving this problem? Hi there, I'm working on

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.