Hello. I am writing a card game program, but I cannot figure out how to write a swap() method with which to shuffle the cards. I have created a class DeckList that extends ArrayList (I did this to to override the toString method so that it displays the Cards instead of the memory addresses of these cards). I am trying to use ArrayList's add() method to swap the objects at the specified indexes. I know the process to do this is
1. Store the Card at the first index at a temporary location
2. Store the Card at the second location at the first location
3. Store the Card at the temporary location at the second location
but I can not work through the syntax. Here is what I have so far:
but it gives me an error on line 11, that it "cannot find symbol
symbol : method add(int,java.lang.Object)
location: class DeckList<E>"
Does anyone know the problem? I reckon I shall also have to somehow cast the Objects it swaps back into Cards.
yes, the get() and remove() are inherited from ArrayList. And I am only passing the get() an int for the index, it is the add() method that gives me the error. I figured since DeckList inherited get() and remove() from ArrayList it would also inherit ArrayLists's add() method. oracle.com says ArrayList has
public void add(int index,
Inserts the specified element at the specified position in this list. Shifts the element currently at that position (if any) and any subsequent elements to the right (adds one to their indices).
add in interface List
add in class AbstractList Parameters:
index - index at which the specified element is to be inserted.
element - element to be inserted.
IndexOutOfBoundsException - if index is out of range (index < 0 || index > size())."
but when i try to use it the compile tells me it cannot find symbol method add(int,java.lang.Object). It only gives me this error on one line, line 11 in the code above
in any case, I got it working with
but it tells me "Note: I:\PROGRAMMING\Java\DeckList.java uses unchecked or unsafe operations.
Note: Recompile with -Xlint:unchecked for details."