SWT ArrayList with variable element type

Posted on 2009-05-27
Medium Priority
Last Modified: 2013-12-15

Is something like this possible?
public class MyClass {
private List list;
public  MyClass (Class elementType) 
   list =  new ArrayList<elementType>();

Open in new window

Question by:cc042297
  • 2
LVL 20

Accepted Solution

a_b earned 200 total points
ID: 24489811
use java generics -

public class MyClass<T> {
private List list;
public  MyClass ()
   list =  new ArrayList<T>();
LVL 20

Expert Comment

ID: 24489820
Or you can do -
public  Test (Class<?> elementType)
        if(elementType == String.class)
            {ArrayList<String>list =  new ArrayList<String>();}

So if you have a handful of object types, you can handle it this way.

Author Closing Comment

ID: 31586109
Thank you!

Featured Post

Free Tool: IP Lookup

Get more info about an IP address or domain name, such as organization, abuse contacts and geolocation.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

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.

Join & Write a Comment

Jaspersoft Studio is a plugin for Eclipse that lets you create reports from a datasource.  In this article, we'll go over creating a report from a default template and setting up a datasource that connects to your database.
A discussion about automated testing of Web Applications utilizing Selenium, along with illustrated configuration steps for the Jenkins open source tool.
Viewers learn how to read error messages and identify possible mistakes that could cause hours of frustration. Coding is as much about debugging your code as it is about writing it. Define Error Message: Line Numbers: Type of Error: Break Down…
Viewers will learn about arithmetic and Boolean expressions in Java and the logical operators used to create Boolean expressions. We will cover the symbols used for arithmetic expressions and define each logical operator and how to use them in Boole…
Suggested Courses

627 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