This week's book giveaways are in the Java EE and JavaScript forums.
We're giving away four copies each of The Java EE 7 Tutorial Volume 1 or Volume 2(winners choice) and jQuery UI in Action and have the authors on-line!
See this thread and this one for details.
The moose likes Beginning Java and the fly likes making a swap() method for a class DeckList that extends ArrayList Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of The Java EE 7 Tutorial Volume 1 or Volume 2 this week in the Java EE forum
or jQuery UI in Action in the JavaScript forum!
JavaRanch » Java Forums » Java » Beginning Java
Bookmark "making a swap() method for a class DeckList that extends ArrayList" Watch "making a swap() method for a class DeckList that extends ArrayList" New topic
Author

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

Luke Stanley
Greenhorn

Joined: Apr 21, 2011
Posts: 4

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

Joined: Aug 16, 2005
Posts: 14114
    
  16

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.

Java Beginners FAQ - JavaRanch SCJP FAQ - The Java Tutorial - Java SE 7 API documentation
Scala Notes - My blog about Scala
Luke Stanley
Greenhorn

Joined: Apr 21, 2011
Posts: 4

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?
 
It is sorta covered in the JavaRanch Style Guide.
 
subject: making a swap() method for a class DeckList that extends ArrayList