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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 18, 20262026-05-18T08:54:06+00:00 2026-05-18T08:54:06+00:00

Sorry for the silly question, but I cannot seem to find an answer on

  • 0

Sorry for the silly question, but I cannot seem to find an answer on google. I have written a class, and within the class there is a constructor which creates an arraylist, in the same class there is a method which iterates through the array list by creating an iterator object. As my code stands, however, it is not recognizing the arraylists name, should I store the array list in a class variable, or pass it into the method as an argument ?

What is usually best practice here as this is something that is always getting me ?

My code is as follows if you can’t follow my somewhat convuluted explanation ! Apologies ! Thanks a lot for reading 🙂

import java.util.*;
public class Primes {
  public Primes( int initialCapacity) {
    ArrayList<Integer> listOfPrimeNumbers = new ArrayList<Integer>(initialCapacity);    
    //how do I get the above...
    int index = 2;
    while (index != listOfPrimeNumbers.size())
    {
      if (isPrime(index))
      {
        listOfPrimeNumbers.add(index);  
      }
      index++;
    }
  }
  public static boolean isPrime(int candidateNo) {
    Iterator<Integer> iter = listOfPrimeNumbers.iterator( );
    //in here ! ?
    i=2;
    while ( iter.hasNext( ) ) {
      if (candidateNo%i==0 && i!=1) {
        return false;
      }
      else
        return true;
    }
  }

(Also, if you see anything horrifically wrong with my code please don’t be afraid to call me out on it, the more constructive criticism the better!)
}

  • 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-18T08:54:06+00:00Added an answer on May 18, 2026 at 8:54 am

    You define a private var for your ArrayList and initalize this variable in your Constructor.
    Now you can access the list inside your Class 🙂
    hope that helps.

    import java.util.*;
    public class Primes {
    
      private ArrayList<Integer> listOfPrimeNumbers;
    
      public Primes( int initialCapacity) {
        listOfPrimeNumbers = new ArrayList<Integer>(initialCapacity);    
        //how do I get the above...
        int index = 2;
        while (index != listOfPrimeNumbers.size())
        {
          if (isPrime(index))
          {
            listOfPrimeNumbers.add(index);  
          }
          index++;
        }
      }
      public static boolean isPrime(int candidateNo) {
        Iterator<Integer> iter = listOfPrimeNumbers.iterator( );
        //in here ! ?
        i=2;
        while ( iter.hasNext( ) ) {
          if (candidateNo%i==0 && i!=1) {
            return false;
          }
          else
            return true;
        }
      }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Ok sorry this might seem like a dumb question but I cannot figure this
Sorry for this silly question, but is there any way to restrict using directives
I'm using Symfony 1.4 with Doctrine. Sorry if this is a silly question but
Sorry for the silly question, but I ran across code that used: <?=$MAP_OBJECT->printOnLoad();?> <?=$MAP_OBJECT->printMap();?>
sorry for the silly question, but i am stuck converting for example the following
Sorry for the basic question - I'm a .NET developer and don't have much
Sorry for this not being a real question, but Sometime back i remember seeing
Sorry if this sounds like a really stupid question, but I need to make
Sorry about this silly question. I'm trying to learn objc and I'm unable to
Sorry the title isn't more help. I have a database of media-file URLs that

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.