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

编程入门 行业动态 更新时间:2024-10-27 20:33:11
本文介绍了如何将java程序从数组修改为arraylist对象?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

限时送ChatGPT账号..
// 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 对象,但我不知道该怎么做.任何信息/广告都是有帮助的.如果你需要知道什么eles问,我会告诉你的.再次感谢您的帮助.

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.

推荐答案

从改变markers的类型开始:

private ArrayList<Integer> markers;

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

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.

这篇关于如何将java程序从数组修改为arraylist对象?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

更多推荐

[db:关键词]

本文发布于:2023-04-16 07:48:19,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/881900.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:数组   如何将   对象   程序   java

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!