Results 1 to 11 of 11
Like Tree2Likes
  • 1 Post By Tolls
  • 1 Post By DarrylBurke

Thread: help with arraylists

  1. #1
    Lonewolf is offline Member
    Join Date
    Sep 2013
    Posts
    4
    Rep Power
    0

    Default help with arraylists

    Hi, Hoping someone can help out, as I'm stuck with trying to implement an array list the way I need to work. Not sure if this is beginner or advanced, apologies in advance if in the wrong spot.

    The goal I am trying to accomplish is to store x and y as an array in sector and then to store a bunch of sectors in the array list, for later processing. It has to be an arraylist as the amount will be dynamic during the game it is for. I'm sure it's probably a simple fix, but my usual resolve has given way to asking for help for this one.

    the below is just a bare bones of what I am trying to get to run before I integrate with the rest of my code. If there is a "smarter" way to achieve the abovementioned goal, I'm all ears :)

    Java Code:
    import java.util.*;
    
    public class testa {
    	long x;
    	long y;
    	long[] sector = {x,y};
    	ArrayList<Long[]> sectorlist;
    	
    	public testa(){
    		x = 1;
    		y = 2;
    		sectorlist = new ArrayList<Long[]>();
    	}
    	
    	public void runtest(){
    		sectorlist.add(sector);
    	}
    }
    The error I get is "The method add(long[]) in the ArrayList <Long[]> is not applicable for the arguments(Long[])" ... yet it works if I change the Long[]'s to Long's and "sectorlist.add(x)" what am I missing?

  2. #2
    Tolls is offline Moderator
    Join Date
    Apr 2009
    Posts
    11,856
    Rep Power
    19

    Default Re: help with arraylists

    The Java compiler can auto-wrap primitives, but cannot do the same thing for arrays of primitives.
    Put it this way, a long[] is an object.
    A Long[] is another object, but a different kind of object, in the same way a String[] is not the same as an Integer[].

    Why not use a Long[]?
    gimbal2 likes this.
    Please do not ask for code as refusal often offends.

    ** This space for rent **

  3. #3
    jbarke12 is offline Member
    Join Date
    May 2013
    Location
    Charleston, South Carolina USA
    Posts
    79
    Rep Power
    0

    Default Re: help with arraylists

    Well if you look at your code the ArrayList is of type Long[] and the sector is of type long[]. It would work if you put the type in line 5 as <long[]> and in line 12. Another way to do this would be to create a class called sector that had a long x and y in it with their corresponding getters and setters. I would do the second way if there would be ever anything else you would want to add to a sector.

  4. #4
    gimbal2 is offline Just a guy
    Join Date
    Jun 2013
    Location
    Netherlands
    Posts
    3,758
    Rep Power
    5

    Default Re: help with arraylists

    Key point here is that long[] (array) and Long[] (array) are two different types; you can't assign one to the other. This might be confusing because this:

    Java Code:
    long value1 = 5;
    Long value2 = value1;
    does work, because Java can autobox this (basically an expensive word for 'convert'); Java can do this because these are based on primitive types. But an array is just any other object, autoboxing does not apply there.
    Last edited by gimbal2; 09-13-2013 at 03:18 PM.
    "Syntactic sugar causes cancer of the semicolon." -- Alan Perlis

  5. #5
    Tolls is offline Moderator
    Join Date
    Apr 2009
    Posts
    11,856
    Rep Power
    19

    Default Re: help with arraylists

    "autobox"!
    That's the word I was ferreting about for!
    :)
    Please do not ask for code as refusal often offends.

    ** This space for rent **

  6. #6
    gimbal2 is offline Just a guy
    Join Date
    Jun 2013
    Location
    Netherlands
    Posts
    3,758
    Rep Power
    5

    Default Re: help with arraylists

    ... crap, I totally missed your reply. Sorry for parroting you :/ I'll add a like to yours to make up for it ;)

    So now we were ferreting and parroting. Back to our animal roots huh! Alright, lets stop monkeying about.
    "Syntactic sugar causes cancer of the semicolon." -- Alan Perlis

  7. #7
    Lonewolf is offline Member
    Join Date
    Sep 2013
    Posts
    4
    Rep Power
    0

    Default Re: help with arraylists

    Thanks guys, hit the nail on the head, not it behaves just like a good bit of code :)

    I set them all to long[] and now I can put the array into the array list, and later retrieve it and get the correct data out of it.

    Can't wait to get that implemented into the generator now, but now I must sleep.

  8. #8
    DarrylBurke's Avatar
    DarrylBurke is offline Member
    Join Date
    Sep 2008
    Location
    Madgaon, Goa, India
    Posts
    11,184
    Rep Power
    19

    Default Re: help with arraylists

    Quote Originally Posted by gimbal2 View Post
    So now we were ferreting and parroting. Back to our animal roots huh! Alright, lets stop monkeying about.
    Not gonna happen. A leopard doesn't change his spots.
    gimbal2 likes this.
    If you're forever cleaning cobwebs, it's time to get rid of the spiders.

  9. #9
    Lonewolf is offline Member
    Join Date
    Sep 2013
    Posts
    4
    Rep Power
    0

    Default Re: help with arraylists

    So I got the arraylist working, can store sector in the arraylist and then use get to retrieve the data. I did notice that when I tested on 10 "Sectors" that they all seem to hold the same data, like that arraylist just held 10 references to the same array.

    Is that what jbarke12 was referring to by making a separate class and running a class call to set and get the data? is that the most efficient way if there was say, 1-2 million "sectors" being stored?

  10. #10
    Tolls is offline Moderator
    Join Date
    Apr 2009
    Posts
    11,856
    Rep Power
    19

    Default Re: help with arraylists

    If you have lots of sectors then each one needs to be its own object.
    At the moment all the entries in your array are pointing to the same object on the heap, so a change on one entry is visible from all the others.
    Please do not ask for code as refusal often offends.

    ** This space for rent **

  11. #11
    Lonewolf is offline Member
    Join Date
    Sep 2013
    Posts
    4
    Rep Power
    0

    Default Re: help with arraylists

    Thanks Tolls, sounds like I was on the right track, after fixing the long/Long typo and putting the sector class in, it all good. I'm sure I'll run into other hurdles, but at least I'm not hung up on one section now.

Similar Threads

  1. Help with arraylists!
    By MrSquishyBoots in forum New To Java
    Replies: 3
    Last Post: 01-24-2013, 09:52 AM
  2. I don't get ArrayLists
    By Gio!? in forum New To Java
    Replies: 10
    Last Post: 05-03-2012, 12:50 PM
  3. help with ArrayLists !
    By bigbreez21 in forum New To Java
    Replies: 3
    Last Post: 11-20-2011, 08:23 PM
  4. ArrayLists
    By Freakzoyd in forum New To Java
    Replies: 4
    Last Post: 11-12-2010, 04:27 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
  •