[Okta Webinar] Learn how to a build a cloud-first strategyRegister Now

x
?
Solved

simple ArrayList question!

Posted on 2006-03-26
4
Medium Priority
?
262 Views
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

r6


------------------
Class A {
  ...
  ...
}

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

0
Comment
Question by:R6
  • 2
4 Comments
 
LVL 14

Expert Comment

by:StillUnAware
ID: 16293310
You must cast the type back:

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

Accepted Solution

by:
StillUnAware earned 200 total points
ID: 16293312
Or use generics (since Java 5.0):

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

Expert Comment

by:CEHJ
ID: 16293315
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
0
 

Author Comment

by:R6
ID: 16293529
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
0

Featured Post

Independent Software Vendors: We Want Your Opinion

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

After being asked a question last year, I went into one of my moods where I did some research and code just for the fun and learning of it all.  Subsequently, from this journey, I put together this article on "Range Searching Using Visual Basic.NET …
For beginner Java programmers or at least those new to the Eclipse IDE, the following tutorial will show some (four) ways in which you can import your Java projects to your Eclipse workbench. Introduction While learning Java can be done with…
Viewers learn about the “for” loop and how it works in Java. By comparing it to the while loop learned before, viewers can make the transition easily. You will learn about the formatting of the for loop as we write a program that prints even numbers…
This tutorial explains how to use the VisualVM tool for the Java platform application. This video goes into detail on the Threads, Sampler, and Profiler tabs.
Suggested Courses
Course of the Month19 days, 11 hours left to enroll

872 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