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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 11, 20262026-05-11T01:44:39+00:00 2026-05-11T01:44:39+00:00

I am writing this java program to find all the prime numbers up to

  • 0

I am writing this java program to find all the prime numbers up to num using the Sieve of Eratosthenes, but when I try to compile, it says I can’t use a long var as an array index, and it expects an int var in its place. But I’ll be working with large numbers, so I can’t use int. What can I do?

import java.util.*; import java.lang.*;  public class t3{     public static void main(String[] args){         long num = 100;          //declaring list and filling it with numbers         ArrayList<Long> numlist = new ArrayList<Long>();         for(long x=2 ; x<num ; x++){             numlist.add(new Long(x));         }          //sieve or eratosthenes         for(long x=0 ; x<Math.sqrt(num) ; x++){             for(long y=x+1 ; y<numlist.size() ; y++){                 if(numlist[y]%numlist[x] == 0){                     numlist.remove(y);                 }             }         }          //print list         for(Object item : numlist){             System.out.println((Long)item);         }     } } 
  • 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. 2026-05-11T01:44:39+00:00Added an answer on May 11, 2026 at 1:44 am

    I’m not sure why your code would compile to begin with.

    You’re not supposed to use [] in an array list to access members. An arraylist is merely a list that is internally stored in an array. You have to use the list get operation (which would still be O(1)). Writing numlist[index] means that you have an array of objects in numlist. You cannot override the [] operation as in C++.

    In addition, an int is 32 bits in Java. Having an array of length greater than 2^32 (so you would need long indices) is unlikely and I’m not even sure the specification allows it.

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

Sidebar

Ask A Question

Stats

  • Questions 73k
  • Answers 73k
  • Best Answers 0
  • User 1
  • Popular
  • Answers
  • Editorial Team

    How to approach applying for a job at a company ...

    • 7 Answers
  • Editorial Team

    How to handle personal stress caused by utterly incompetent and ...

    • 5 Answers
  • Editorial Team

    What is a programmer’s life like?

    • 5 Answers
  • added an answer You just need to pick out the property you want… May 11, 2026 at 2:10 pm
  • added an answer #include <iterator> #include <iostream> #include <sstream> #include <vector> #include <algorithm>… May 11, 2026 at 2:10 pm
  • added an answer You should be reading responses from the server. From nosklo's… May 11, 2026 at 2:10 pm

Related Questions

I am writing a windows service. This service runs another process I've developed through
I am writing a query in SQL server2005. This is returning me a duplicate
I am writing a script for MS PowerShell. This script uses the Copy-Item command.
I come across this problem when i am writing an event handler in SharePoint.
Is there a destructor for Java? I don't seem to be able to find
I was wondering if anyone had some resources that describe the binary protocol used
I am writing a mad libs program for fun and to just program something.
I have a large tree of Java Objects in my Desktop Application and am

Trending Tags

analytics british company computer developers django employee employer english facebook french google interview javascript language life php programmer programs salary

Top Members

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.