• Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

making a swap() method for a class DeckList that extends ArrayList

 
Luke Stanley
Greenhorn
Posts: 4
Opera Ubuntu
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.

Thanks,
Luke
 
Jesper de Jong
Java Cowboy
Saloon Keeper
Posts: 15219
36
Android IntelliJ IDE Java Scala Spring
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Your swap method is obviously part of a class that has get() and remove() methods that you didn't show us.

The error message says that the class does not have a get() method that takes an int and an Object as arguments.
 
Luke Stanley
Greenhorn
Posts: 4
Opera Ubuntu
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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

"add
public void add(int index,
Object element)
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).

Specified by:
add in interface List
Overrides:
add in class AbstractList
Parameters:
index - index at which the specified element is to be inserted.
element - element to be inserted.
Throws:
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."

what does this mean?
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic