How to modify a java program from arrays to arraylist objects?
// 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 "M开发者_如何学Pythoniss";
} // 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.
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.
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().
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?
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()
精彩评论