simple ArrayList question!

Posted on 2006-03-26
Last Modified: 2010-03-31
hi experts,

basically, i want to create an arraylist of user defined class, in this case, an arraylist of A as illustrated below.  the problem occurs after i have initialized the arraylist and i want to get the inidividual element out of it, i have encountered an incompatible type error as illustrated below.

can someone explain this to me?

many thanks


Class A {

Class State {
  ArrayList al = new ArrayList();
  A a = new A();
  void somefunction() {
    A a = al.get(i);  // Incompatible types error!!!!!

Question by:R6
    LVL 14

    Expert Comment

    You must cast the type back:

    A a = (A)al.get(i);
    LVL 14

    Accepted Solution

    Or use generics (since Java 5.0):

    ArrayList<A> al = new ArrayList<A>();
    A a = al.get(i);
    LVL 86

    Expert Comment

    If you're using >= 1.5 you can use generics to avoid typecasting and introduce compile time type safety:

    class State {
      ArrayList<A> al = new ArrayList<A>();

    A a = al.get(i);

    is then possible

    Author Comment

    well thanks StillUnAware and CEHJ for the rapid responses:) i really appreciate it.

    not only have i learnt the solution to my problem, i've also realized that i was compiling with java 1.4.2..took me a while to reset the path to 1.5 on mac.. and i finally know what <E> stands for on the API.

    thanks again

    Write Comment

    Please enter a first name

    Please enter a last name

    We will never share this with anyone.

    Featured Post

    Do You Know the 4 Main Threat Actor Types?

    Do you know the main threat actor types? Most attackers fall into one of four categories, each with their own favored tactics, techniques, and procedures.

    Suggested Solutions

    Title # Comments Views Activity
    fix34  challenge 9 70
    SPLUNK REST  API call to Splunk to create and index? 2 40
    stringclean challenge 26 43
    countHi2 challenge 7 30
    If you have upgraded to Java2 update 10 on a Microsoft Windows client, you may have discovered that your Java application does not work as it did before.  For example, the colors of your Java2D graphic may be all wrong for no apparent reason. Aft…
    By the end of 1980s, object oriented programming using languages like C++, Simula69 and ObjectPascal gained momentum. It looked like programmers finally found the perfect language. C++ successfully combined the object oriented principles of Simula w…
    Viewers learn about the third conditional statement “else if” and use it in an example program. Then additional information about conditional statements is provided, covering the topic thoroughly. Viewers learn about the third conditional statement …
    This tutorial covers a step-by-step guide to install VisualVM launcher in eclipse.

    759 members asked questions and received personalized solutions in the past 7 days.

    Join the community of 500,000 technology professionals and ask your questions.

    Join & Ask a Question

    Need Help in Real-Time?

    Connect with top rated Experts

    11 Experts available now in Live!

    Get 1:1 Help Now