如果我有一个类,并且在这个类中有一个创建 ArrayList 的构造函数...将此数组列表传递给类方法的最佳方法是什么?

发布于 2024-10-05 05:22:43 字数 1002 浏览 0 评论 0原文

抱歉这个愚蠢的问题,但我似乎无法在谷歌上找到答案。我编写了一个类,在该类中有一个创建数组列表的构造函数,在同一个类中有一个通过创建迭代器对象来迭代数组列表的方法。然而,就我的代码而言,它无法识别 arraylists 名称,我应该将数组列表存储在类变量中,还是将其作为参数传递到方法中?

通常这里的最佳实践是什么,因为这总是让我困惑?

如果您无法理解我有些复杂的解释,我的代码如下!抱歉!非常感谢您的阅读:)

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;
    }
  }

(另外,如果您发现我的代码有任何可怕的错误,请不要害怕批评我,建设性的批评越多越好!) }

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!)
}

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(4

弥繁 2024-10-12 05:22:43

您为 ArrayList 定义一个私有变量,并在构造函数中初始化该变量。
现在您可以访问班级内的列表了:)
希望有帮助。

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;
    }
  }

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;
    }
  }
叹沉浮 2024-10-12 05:22:43

listOfPrimeNumbers 是一个局部变量。您可以将其更改为类的静态成员,使其成为实例变量并使 isPrime 非静态,或者将其作为参数传递给 isPrime

考虑到此类的明显意图,我会将 isPrime 设置为非静态,并将 listOfPrimeNumbers 设置为实例变量。

listOfPrimeNumbers is a local variable. You can either change it into a static member of the class, make it an instance variable and make isPrime non-static, or pass it as a parameter to isPrime.

Given the apparent intent of this class I'd make isPrime non-static and make listOfPrimeNumbers an instance variable.

甜警司 2024-10-12 05:22:43

您需要重新考虑如何将类组合在一起:

  • 在构造函数中完成所有工作通常不是一个好主意。 Java 中的构造函数很棘手,通常我会尝试使它们尽可能简单,并将真正的工作放在其他地方。

  • 静态方法被高估了,我尽量避免使用它们。如果您希望方法访问某些数据结构,请将这些方法设为实例方法并将数据结构作为实例变量。 Java 应该是一种面向对象的语言,使用静态方法会阻碍面向对象并限制你的选择。

  • 考虑一下您希望如何使用该类,以及您希望如何测试它,并更改该类实现的 API 以与之匹配。

You need to reconsider how your class is put together:

  • Having all the work done within the constructor is typically not a good idea. Constructors in Java are tricky, generally I try to make them as simple as possible, and have the real work go elsewhere.

  • Static methods are overrated, I try to avoid them. If you have some data structure you want your methods to access, make those methods instance methods and have the data structure be an instance variable. Java is supposed to be an object-oriented language, using static methods prevents object-orientation and restricts your options.

  • Think about how you want to use the class, and how you'd like to test it, and change the API implemented by the class to match that.

风流物 2024-10-12 05:22:43

看来您想将 arraylist 设置为类成员变量。但是,您将 isPrime 声明为静态。这两件事放在一起没有意义。如果 isPrime 不需要是静态的,那么只需将 arraylist 存储为类成员即可。如果它确实需要是静态的(无论出于何种原因),则不需要构造函数(因为在这种情况下 isPrime 是静态的没有意义)并在其他地方创建数组列表并将其作为参数传递到 isPrime。

编辑:再想一想,你不需要 isPrime 是静态的,它应该看起来像这样:

public class Primes {
  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;
    }
  }

但是,你似乎还有其他问题。构造函数中的 while 循环将如何结束?你每次都会增加索引,但只有当它是素数时,你才会将一些东西添加到你的列表中。

在你的 isPrime 方法中,你的 while 循环有什么意义?您调用 iter.hasNext(),但不使用其迭代的内容。您只检查 candidateNoi

It seems you want to set the arraylist as a class member variable. However you're declaring isPrime as static. Those 2 things don't make sense together. If isPrime doesn't need to be static then simply store the arraylist as a class member. If it DOES need to be static (for whatever reason), you'll need to NOT have a constructor (because it wouldn't make sense with isPrime being static in this case) and create the arraylist somewhere else and pass it as an argument to isPrime.

Edit: on second thought, you don't need isPrime to be static, It should look like this:

public class Primes {
  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;
    }
  }

However it seems like you have other problems. How will your while loop in your constructor ever end? You're incrempending index every time, but you're only adding something to your list if it's prime.

And in your isPrime method, what's the point of your while loop? You call iter.hasNext(), but don't use what its iterating over. You only ever check candidateNo and i.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文