2011-03-28 41 views
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對象,我不知道該怎麼做。任何信息/愛慕都有幫助。如果你需要知道任何問題,我會讓你知道的。再次感謝您的幫助。如何修改一個Java程序從數組到ArrayList對象?

+1

這個功課? – Neal 2011-03-28 14:32:09

+0

是的,我不會對此撒謊。我只是不知道該怎麼做。這是我來到這裏的唯一原因。我不明白。 – Leasha 2011-03-28 14:34:23

+0

是的,這是作業。我沒有要求任何人爲我做。我所要求的只是建議如何去做。我迷路了,不明白,所以我在努力。我在尋求建議並不壞。沒有人可以知道是否有人願意向需要它的人提供建議。感謝任何人提供adivce。我非常感謝它,它幫助我理解我需要做什麼。 – Leasha 2011-03-28 14:39:53

回答

2

開始通過改變markers類型:

private ArrayList<Integer> markers; 

你的IDE現在應該告訴你,因爲ArrayList一大堆錯誤和數組不能互換。修復這些錯誤,你就完成了。

+1

好的建議,但做什麼樣的功課。:-( – JackWilson 2011-03-28 14:36:14

+0

好吧,所以我改變了標記,但是我不確定我在做什麼,我正在閱讀我的錯誤並試圖改變它們,但我不知道它們是什麼 – Leasha 2011-03-28 14:52:09

+0

我想這是作業部分:找出你在做什麼,閱讀文檔,谷歌!認真,GOOGLE。信息已經存在 – Bombe 2011-03-28 14:55:27

0

您可以先將所有標記從數組更改爲ArrayList,然後修復錯誤。

但是,一個更好的方法來做到這一點是想想你正在做的操作的種類。您通常會爲數組做一些事情:創建它,添加元素,獲取元素。你如何爲數組做這些事情?你如何做一個ArrayList?如果你不知道,請在ArrayList文檔中查找它。找到它爲數組完成的地方,然後改變它爲ArrayList完成的方式。

更重要的是,陣列和ArrayList之間的基金會結構差異是什麼。你已經在課堂上教過(提示 - 尺寸是多少?)。這對你如何向他們添加對象有什麼不同?也許你應該改變你添加對象的方式?

1

我看不到暴露數組的公開api。所以不需要將它從數組改爲ArrayList。

如果你仍然需要改變它。看看ArrayList API: http://download.oracle.com/javase/6/docs/api/java/util/ArrayList.html

你可以用Array和[index]做什麼,你可以用.get(index)和.set(index)方法對ArrayList進行操作。

myArray.length是myArrayList.size()。

0

一旦你有你的ArrayList對象 「標誌」 按照上面的回答,這裏是ArrayList的基礎知識(不言自明真的):

  • 加(對象)
  • GET(INT)
  • 尺寸()
  • 刪除(INT)
  • 的indexOf(對象) - 認定的 索引對象
  • 明確的第一次出現( )
相關問題