如何将java程序从数组修改为arraylist对象?

发布于 2024-10-28 01:05:05 字数 2129 浏览 1 评论 0原文

// sets up random number of markers in a
// one-dimensional array
// numMarkers markers in a board of size boardSize
public class SimpleDotCom
{
  // constants
  private final static int DEFAULT_MARKERS = 3;
  private final static int DEFAULT_BOARD_SIZE = 10;

  // data members
  private int[] markers; // stores the marker positions
  private int boardSize; // stores the size of the board
  private int endOfMarkers;

  // default constructor
  // 3 markers in a board of 10
  public SimpleDotCom()
  {
    this( DEFAULT_MARKERS, DEFAULT_BOARD_SIZE );
  }

  // constructor to set up
  // numMarkers and boardSize
  public SimpleDotCom( int numMarkers, int boardSize )
  {
    markers = new int[numMarkers];
    this.boardSize = boardSize;
    endOfMarkers = markers.length - 1;

    int i, j, randNum;
    int[] original = new int[boardSize];

    for ( i = 0; i < original.length; i++ )
      original[i] = i;

    // scramble original
    for ( i = original.length - 1;
          i >= original.length - markers.length;
          i-- )
    {
      randNum = (int) (Math.random() * (i+1) );
      // swap original[i] and original[randNum]
      j = original[i];
      original[i] = original[randNum];
      original[randNum] = j;
    }

    for ( i = 0; i < markers.length; i++ )
      markers[i] = original[i+original.length-markers.length];
  } // end SimpleDotCom

  // check if the guess is a hit or a miss
  // precondition: guess is valid
  public String checkYourself( int guess )
  {
    for ( int i = 0; i <= endOfMarkers; i++ )
      if ( markers[i] == guess )
      {
        markers[i] = markers[endOfMarkers];
        endOfMarkers--;
        return "Hit";
      }
    return "Miss";
  } // end checkYourself

  // returns the number of markers in the game
  public int numberOfMarkers()
  {
    return markers.length;
  } // end numberOfMarkers

  // returns the size of the board
  public int sizeOfBoard()
  {
    return boardSize;
  } // end sizeOfBoard
} // end SimpleDotCom

这就是我需要修改的程序。我要将数组修改为 arraylist 对象,但我不知道该怎么做。任何信息/建议都有帮助。如果您需要了解任何信息,请询问,我会让您知道。再次感谢您的帮助。

// sets up random number of markers in a
// one-dimensional array
// numMarkers markers in a board of size boardSize
public class SimpleDotCom
{
  // constants
  private final static int DEFAULT_MARKERS = 3;
  private final static int DEFAULT_BOARD_SIZE = 10;

  // data members
  private int[] markers; // stores the marker positions
  private int boardSize; // stores the size of the board
  private int endOfMarkers;

  // default constructor
  // 3 markers in a board of 10
  public SimpleDotCom()
  {
    this( DEFAULT_MARKERS, DEFAULT_BOARD_SIZE );
  }

  // constructor to set up
  // numMarkers and boardSize
  public SimpleDotCom( int numMarkers, int boardSize )
  {
    markers = new int[numMarkers];
    this.boardSize = boardSize;
    endOfMarkers = markers.length - 1;

    int i, j, randNum;
    int[] original = new int[boardSize];

    for ( i = 0; i < original.length; i++ )
      original[i] = i;

    // scramble original
    for ( i = original.length - 1;
          i >= original.length - markers.length;
          i-- )
    {
      randNum = (int) (Math.random() * (i+1) );
      // swap original[i] and original[randNum]
      j = original[i];
      original[i] = original[randNum];
      original[randNum] = j;
    }

    for ( i = 0; i < markers.length; i++ )
      markers[i] = original[i+original.length-markers.length];
  } // end SimpleDotCom

  // check if the guess is a hit or a miss
  // precondition: guess is valid
  public String checkYourself( int guess )
  {
    for ( int i = 0; i <= endOfMarkers; i++ )
      if ( markers[i] == guess )
      {
        markers[i] = markers[endOfMarkers];
        endOfMarkers--;
        return "Hit";
      }
    return "Miss";
  } // end checkYourself

  // returns the number of markers in the game
  public int numberOfMarkers()
  {
    return markers.length;
  } // end numberOfMarkers

  // returns the size of the board
  public int sizeOfBoard()
  {
    return boardSize;
  } // end sizeOfBoard
} // end SimpleDotCom

That is the program that I need to modify. I am to modify the arrays to arraylist objects and I don't know how to do it. Any information/adive is helpful. If you need to know anything eles ask and I will let you know. Again thanks for your help.

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

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

发布评论

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

评论(4

紫瑟鸿黎 2024-11-04 01:05:05

首先更改 标记 的类型:

private ArrayList<Integer> markers;

您的 IDE 现在应该会显示大量错误,因为 ArrayList 和数组不可互换。修复这些错误,就完成了。

Start by changing the type of markers:

private ArrayList<Integer> markers;

Your IDE should now show you a whole lot of errors since ArrayList and arrays are not interchangeable. Fix those errors, and you’re done.

远山浅 2024-11-04 01:05:05

我没有看到公开数组的公共 api。所以没必要从数组改成ArrayList。

如果还需要改变的话。看一下 ArrayList API:
http://download.oracle.com/javase/6 /docs/api/java/util/ArrayList.html

使用数组和 [index] 可以执行的操作,可以使用 .get(index) 和 .set(index) 方法在 ArrayList 上执行。

myArray.length 是 myArrayList.size()。

I see no public api that exposes the arrays. So there is no need to change it from arrays to ArrayList.

If you still need to change it. HAve a look at the ArrayList API:
http://download.oracle.com/javase/6/docs/api/java/util/ArrayList.html

What you can do with arrays and [index] you can do on an ArrayList with the .get(index) and .set(index) methods.

myArray.length is myArrayList.size().

一笔一画续写前缘 2024-11-04 01:05:05

您可以首先将所有标记从数组更改为ArrayList,然后修复错误。

但更好的方法是考虑您正在执行的操作类型。您通常会对数组执行许多操作:创建数组、添加元素、获取元素。如何为数组做这些事情?如何对 ArrayList 执行这些操作?如果您不知道,请在 ArrayList 文档中查找。找到它为数组完成的位置,然后更改为它为 ArrayList 完成的方式。

更重要的是,数组和 ArrayList 之间根本的结构差异是什么。您已在课堂上学过这一点(提示 - 尺寸是多少?)。这对向其中添加对象的方式有影响吗?也许您应该改变添加对象的方式?

You can start by simply changing all the markers from arrays to ArrayList and then fix up the errors.

But a better way to do this is to think about the kinds of operations you are doing. You are typically doing a number of things to an array: creating it, adding an element, getting an element. How do you do those things for an array? How do you do them for an ArrayList? Look it up in the ArrayList documentation if you don't know. Find the places it's done for the array, and change then to the way it's done for an ArrayList.

More importantly, what's the fundamendal strucural difference between arrays and ArrayList. You've been taught this in class (hint - what's the size?). Does that make a difference to how you add objects to them? Maybe you should change the way you add objects?

一百个冬季 2024-11-04 01:05:05

根据上述答案获得 ArrayList 对象“标记”后,以下是 ArrayList 基础知识(实际上是不言自明的):

  • add(Object)
  • get(int)
  • size()
  • remove(int)
  • indexOf(Object) - 查找索引的
    对象第一次出现时
  • clear()

Once you have your ArrayList object "markers" as per the above answer, here are the ArrayList basics (self-explanatory really):

  • add(Object)
  • get(int)
  • size()
  • remove(int)
  • indexOf(Object) - finds the index of
    the first occurrence of the object
  • clear()
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文