Results 1 to 6 of 6
  1. #1
    Join Date
    Mar 2014
    Posts
    72
    Rep Power
    0

    Default How to use ArrayList with an Array?

    Something like ArrayList<Object>[] = new ArrayList<Object>[]();

  2. #2
    Norm's Avatar
    Norm is offline Moderator
    Join Date
    Jun 2008
    Location
    SW Missouri
    Posts
    17,305
    Rep Power
    25

    Default Re: How to use ArrayList with an Array?

    What happens when you compile that statement?
    If you don't understand my response, don't ignore it, ask a question.

  3. #3
    Join Date
    Mar 2014
    Posts
    72
    Rep Power
    0

    Default Re: How to use ArrayList with an Array?

    Nothing, it just doesn't work.
    On another note, can I store objects that extend or implement a class in an ArrayList of another class?
    Like ArrayList<Human> humans = new ArrayList();
    Man man = new Man();
    humans.add(man);
    ?

  4. #4
    kneitzel is offline Senior Member
    Join Date
    Feb 2014
    Posts
    448
    Rep Power
    1

    Default Re: How to use ArrayList with an Array?

    Hi,

    is there a specific reason why you want to mix up an array with a generic type?

    I fear that it is not as straight forward with error messages.
    - when you create an array, no () are set. So remove them.
    - you need a variable name.
    - you need to specify an array size
    - you have to remove the generic from the left side.
    ArrayList<Object>[] listArray = new ArrayList[10];

    Konrad

  5. #5
    Join Date
    Mar 2014
    Posts
    72
    Rep Power
    0

    Default Re: How to use ArrayList with an Array?

    Thanks konrad. By specific the size of the array like that, will its size still automatically increase when objects are added to it?

  6. #6
    kneitzel is offline Senior Member
    Join Date
    Feb 2014
    Posts
    448
    Rep Power
    1

    Default Re: How to use ArrayList with an Array?

    You created a fixed size Array of ArrayList instanzes. So each ArrayList will have a dynamic size. But the outer array is fixed.

    If you want to be fully dynamic, then you could use an ArrayList of ArrayLists instead.

    Konrad

Similar Threads

  1. Array or ArrayList?
    By rhym1n in forum New To Java
    Replies: 1
    Last Post: 03-08-2013, 01:08 PM
  2. Array vs ArrayList
    By Lund01 in forum New To Java
    Replies: 5
    Last Post: 10-14-2010, 10:41 AM
  3. 2D array of ArrayList (technically a 3D array)
    By Lil_Aziz1 in forum New To Java
    Replies: 3
    Last Post: 05-15-2010, 08:11 PM
  4. Array and arraylist
    By MIA6 in forum New To Java
    Replies: 3
    Last Post: 11-29-2009, 02:22 AM
  5. Array to ArrayList
    By javaplus in forum New To Java
    Replies: 2
    Last Post: 11-12-2007, 12:46 AM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •