Welcome to the Java Programming Forums


The professional, friendly Java community. 21,500 members and growing!


The Java Programming Forums are a community of Java programmers from all around the World. Our members have a wide range of skills and they all have one thing in common: A passion to learn and code Java. We invite beginner Java programmers right through to Java professionals to post here and share your knowledge. Become a part of the community, help others, expand your knowledge of Java and enjoy talking with like minded people. Registration is quick and best of all free. We look forward to meeting you.


>> REGISTER NOW TO START POSTING


Members have full access to the forums. Advertisements are removed for registered users.

Results 1 to 3 of 3

Thread: Defining an Array of ArryaList of ArrayList

  1. #1
    Junior Member
    Join Date
    Nov 2018
    Posts
    1
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Defining an Array of ArryaList of ArrayList

    I have this :
    public final static AxisAlignedBB BASE_AACB = new AxisAlignedBB		(0.0D, 0.0D, 0.0D,		1.0D , 0.5D, 1.0D);
    public final static AxisAlignedBB NORTH_QTR_AACB = new AxisAlignedBB 	(0.0D  , 0.5D,  0.0D	, 1.0D , 1.0D,  0.5D);
    public final static AxisAlignedBB WEST_RAIL_AACB = new AxisAlignedBB	(0.0D,	0.0D , 0.0D 	, 0.07D, 1.3D, 1.0D);	
     
    public final static ArrayList<AxisAlignedBB> STAIR_STEPS_NORTH = new ArrayList<AxisAlignedBB>(Arrays.asList(BASE_AACB, NORTH_QTR_AACB));
    .
    .
    .
     
     
    	public final static ArrayList[] STAIR_STEPS_ALL = {STAIR_STEPS_NORTH, STAIR_STEPS_SOUTH, STAIR_STEPS_WEST, STAIR_STEPS_EAST};
    I get a warning about raw types ( on the = {STAIR_STEPS_NORTH, STAIR_STEPS_SOUTH, STAIR_STEPS_WEST, STAIR_STEPS_EAST} part)

    How can i define STAIR_STEPS_ALL[] correctly without rawtypes?

  2. #2
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,140
    Thanks
    65
    Thanked 2,720 Times in 2,670 Posts

    Default Re: Defining an Array of ArryaList of ArrayList

    You can't create arrays with a generic component type.
    Use a collection like List in place of the array.
    If you don't understand my answer, don't ignore it, ask a question.

  3. #3
    Member
    Join Date
    Sep 2018
    Location
    Virginia
    Posts
    284
    My Mood
    Cool
    Thanks
    0
    Thanked 38 Times in 36 Posts

    Default Re: Defining an Array of ArryaList of ArrayList

    You can do the following:

    public final static ArrayList<?>[]				STAIR_STEPS_ALL	  = {
             STAIR_STEPS_NORTH, STAIR_STEPS_SOUTH, STAIR_STEPS_WEST,
             STAIR_STEPS_EAST
       };

    However, when you retrieve one you will have to cast it. The SuppressWarnings statement isn't required as it just gets rid of a warning.

      public static void main(String[] args) {
    	   @SuppressWarnings("unchecked")
    	   ArrayList<AxisAlignedBB> b = (ArrayList<AxisAlignedBB>)STAIR_STEPS_ALL[0];
       }


    But there is really no point to all of that. Since you have to cast you may as well declare the array as type Object and then cast as I did up above.

    Regards,
    Jim
    Last edited by jim829; November 22nd, 2018 at 03:23 PM.

Similar Threads

  1. Defining an array
    By Yehonatan in forum Java Theory & Questions
    Replies: 1
    Last Post: September 30th, 2018, 08:57 AM
  2. convert arraylist to 2D array
    By ishrne in forum Object Oriented Programming
    Replies: 5
    Last Post: April 5th, 2013, 07:57 AM
  3. Adding an array to an arraylist
    By raja211991 in forum Collections and Generics
    Replies: 1
    Last Post: January 7th, 2013, 03:46 PM
  4. Array with null (same for Arraylist)
    By Alex L in forum What's Wrong With My Code?
    Replies: 5
    Last Post: December 5th, 2011, 12:51 AM
  5. converting array into arraylist
    By Harry Blargle in forum Collections and Generics
    Replies: 6
    Last Post: October 30th, 2011, 03:38 AM