Results 1 to 7 of 7

Thread: Table of parameterized ArrayList

  1. #1
    Join Date
    Dec 2009
    Posts
    192

    Table of parameterized ArrayList

    Hello!
    I looked in some tutorial about the parametrized arraylists and I have a problem when using them.
    Here is my code:
    Code:
    		ArrayList <?>[] ts = new ArrayList <?>[2];
    		for(ArrayList <?> A: ts) a = new ArrayList <String>();
    ts[0].add("Hello");
    And I have the following error on the add (): The method add (capture-of?) In the type ArrayList <capture-of ?> is not applicable for the arguments (String) Where am I wrong? Do you have some idea about it.

  2. #2
    Join Date
    Nov 2009
    Posts
    359

    Re: Table of parameterized ArrayList

    Hello,
    Just have a look at the following part of code of your
    Code:
    ArrayList <?>[] ts = new ArrayList <?>[2];
    You said your test variable as an array of ArrayList type ? necessarily when you made the add (), java expects to have sth like ? not String. So to solve your problem replace all your? by String.

  3. #3
    Join Date
    Dec 2009
    Posts
    296

    Re: Table of parameterized ArrayList

    Hello,
    I am trying a similar sort of coding, I already tried it and it is not possible. In fact I based my experiment on this statement of java faq, section "Java 5.0 and parametrized types" (I said I use java 6). It is interesting that only the unconstrained wildcard allow the creation of tables configured. It is impossible to declare such an array of 42 lists of Double:
    Code:
    List <Double>[] tb = new ArrayList <Double>[42];
    This limitation serves once again to protect the code at run time because it would otherwise be possible to manipulate the table as an array of Object and change the content without worrying about parameterized types:
    Code:
    Object[] tab = (Object[]) o;
    o[0] = new ArrayList <String>();

  4. #4
    Join Date
    Dec 2009
    Posts
    192

    Re: Table of parameterized ArrayList

    Hello,
    My example code add to a list of strings in an array supposed to contain only lists of Double. That is why only wild cards are allowed:
    Code:
    List <?>[] tb = new ArrayList <?>[38];
    So, I think the trick is that the definition, it has not seemed to pose problems, but for handling. If I forget the parametrization, I care less warnings everywhere, and I am obliged to caster in all directions.

  5. #5
    Join Date
    Nov 2009
    Posts
    333

    Re: Table of parameterized ArrayList

    Hello,
    If you want any warning you can always add
    Code:
    @ SuppressWarnings("unchecked")
    So you'd have some work like this:
    Code:
    ArrayList <Double> []ts;
        @ SuppressWarnings("unchecked")   
    	ts =new ArrayList[42];
    	for (int i =0; I <42; i + +)
    	    ts[i]=new ArrayList <Double>();
    Anyways can not solve your problem but it takes away the warnings.

  6. #6
    Join Date
    Nov 2009
    Posts
    356

    Re: Table of parameterized ArrayList

    Hello,
    Otherwise you can make a ArrayList <ArrayList <Double>>. Basically your code look like this
    Code:
    ArrayList <ArrayList <Double>> ts;
    ts =new ArrayList <ArrayList <Double>>();
    ts.add(new ArrayList <Double>()); / / To add an item to ts
    	ts.get(0).add(5.0);  / / Add values for the ts elements
    	ts.get(0).add(4.0);

  7. #7
    Seoconsultant786 Guest

    Re: Table of parameterized ArrayList

    Yes he is right the code is same of Arraylist i also use the same code for this when i need to make it.

Similar Threads

  1. Replies: 5
    Last Post: 27-08-2011, 10:53 AM
  2. Link a Table to another Table to Drop Down In Main Table
    By himeshRES in forum Windows Software
    Replies: 6
    Last Post: 11-12-2010, 02:01 PM
  3. Replies: 4
    Last Post: 30-11-2010, 03:01 AM
  4. To convert a pivot table to a flattened table in MS Excel
    By zeemga in forum Windows Software
    Replies: 3
    Last Post: 27-11-2010, 06:48 AM
  5. How to manage Parameterized Queries in MySQL
    By Zeverto in forum Software Development
    Replies: 3
    Last Post: 24-09-2009, 03:23 PM

Tags for this Thread

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  
Page generated in 1,750,357,277.81760 seconds with 16 queries