ArrayList problem - getting 'cannot resolve symbol' errors when trying to access ArrayList

Hi.  I'm just learning java (I know, you've never heard that line before...)

I am writing a program that builds a deck of cards into an array list.  

The errors I am getting are:

Deck.java:85: cannot resolve symbol
symbol  : method size ()
location: class Deck
Object deckSize = theDeck.size();
                                      ^
Deck.java:86: cannot resolve symbol
symbol  : method get (int)
location: class Deck
System.out.println(theDeck.get(0));
                                      ^


Here is the code I have:

-----------------------------------------------------------------
import java.util.ArrayList;
import java.util.Iterator;
import java.util.Random;

   
public class Deck extends Object
{
  static int DECKSIZE;
  protected int remaining;
  private ArrayList theDeck;


  //Default Constructor
  public Deck()
  {
  /* Default constructor creates a deck of 52 cards - Sets remaining to
  DECKSIZE -1 **/
      theDeck = new ArrayList();
      DECKSIZE = 0;
      char suitl = 'E';
      for(int suitno = 0; suitno < 4; suitno ++)
      {
              switch(suitno)
             {
                   case 0: suitl='C';
                         break;
                   case 1: suitl='D';
                         break;
                   case 2: suitl='H';
                         break;
                   case 3: suitl='S';
                         break;
                      }  
        for(int rank = 2; rank < 15; rank ++)
            {
                   
            theDeck.add(DECKSIZE, new Card(suitl,rank));
            ++DECKSIZE;
            }//end rank for loop
      }//end suit for loop
            remaining = DECKSIZE -1;
    }//end Default Constructor
 
   //Main method
public static void main(java.lang.String [] args)
{
int i = 0;
Deck theDeck = new Deck();
Object deckSize = theDeck.size();
System.out.println(theDeck.get(0));
  }  
} //end Class

As far as I can tell, my syntax on the get statements to retrieve info from the ArrayList are correct.  Any help would be greatly appreciated.

LVL 1
Stilett0Asked:
Who is Participating?
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

Mayank SAssociate Director - Product EngineeringCommented:
You have a class-level data-member with the same name as the local-variable in the main () method.

>> private ArrayList theDeck;

This is the class-level member.

>> Deck theDeck = new Deck();

This is the local variable in main (). Now since they have the same name 'theDeck', this one would over-ride the one in the class-member. Hence, if you want to access the class-member, you should change their names. Also - do you want to access the array-list for the 'theDeck' object? If so, you should provide an accessor method for it. Something like:

public ArrayList getDeck ()
{
  return theDeck ;

} // end of getDeck ()

From main (), you can write:

Deck theDeck = new Deck () ;
System.out.println ( theDeck.getDeck ().get ( 0 ) ) ; // in this case, you can live with the same variable-name.

Also - if you want to access the ArrayList directly from main (), you cannot do that since the ArrayList is not a static member whereas the main () method is static. You either need to make the array-list static or use an object to access it, the way I've shown.

As a matter of fact, since the main () method belongs to the same class, you don't need the accessor method, meaning that you can directly use:

Deck theDeck = new Deck () ;
System.out.println ( theDeck.theDeck.get ( 0 ) ) ; // I hope the variable-name should not be an issue

But its better to keep an accessor if you want to access this list from outside the class.
0

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
sciuriwareCommented:
You are confusing 2 things:
theDeck is an ArrayList in one place and a Deck in the main.
And Deck has no .size()
<*>
0
Stilett0Author Commented:
mayankeagle,

If I was to differentiate the names, which ones do I change?  I tried to use a separate array name in the Main method to begin with, and it also was not working.  

Your answer was excellent, and the program will work with what you suggested.  I guess I'm just confused as to where the name differentiation needs to occur.

0
Mayank SAssociate Director - Product EngineeringCommented:
Better to change the name of the ArrayList to something else.
0
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
Java

From novice to tech pro — start learning today.

Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.