Results 1 to 9 of 9
  1. #1
    trishtren is offline Member
    Join Date
    Feb 2011
    Posts
    53
    Rep Power
    0

    Smile access array list item from draw

    Hey, iv been struggling trying to access data in an array list object, the array list contains an integer value id like to use to draw on screen so if anyone can tell me how to access it id be very grateful :
    Java Code:
     ArrayList<Object> imageposx=new ArrayList<Object>();
     //instance of the arraylist im using
    
    
      Integer xpositionlist=new Integer(last_X);
      imageposx.add(xpositionlist);
     // add last place clicked to the array list
    
     //how i am trying to use it
     g.drawImage(imagefile,imageposx.get(0), 0, null);
    The problem is the draw function wont accept imageposx.get() as it is an object type as opposed to a integer, so is there a way to access it or convert it to an integer type to use in the drawimage function?

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

    Default

    Declare ArrayList<Integer> instead of ArrayList<Object> and autoboxing should take care of it.

    And use meaningful variable names that aren't confusing. I can't see any justification for naming an Integer type variable xpositionlist, especially when your List type variable is named imageposx

    db

  3. #3
    ozzyman's Avatar
    ozzyman is offline Senior Member
    Join Date
    Mar 2011
    Location
    London, UK
    Posts
    797
    Blog Entries
    2
    Rep Power
    4

    Default

    it is because you converted it to an Object in the first place...
    Java Code:
    ArrayList<Object> imageposx=new ArrayList<Object>();

    that should say ArrayList<Integer> imageposx = new ArrayList<>();

  4. #4
    JosAH's Avatar
    JosAH is offline Moderator
    Join Date
    Sep 2008
    Location
    Voorschoten, the Netherlands
    Posts
    13,433
    Blog Entries
    7
    Rep Power
    20

    Default

    Quote Originally Posted by ozzyman View Post
    it is because you converted it to an Object in the first place...
    Java Code:
    ArrayList<Object> imageposx=new ArrayList<Object>();

    that should say ArrayList<Integer> imageposx = new ArrayList<>();
    Please read the entire thread first before you reply; you could and should've seen that the answer was already supplied in the first reply.

    kind regards,

    Jos
    cenosillicaphobia: the fear for an empty beer glass

  5. #5
    ozzyman's Avatar
    ozzyman is offline Senior Member
    Join Date
    Mar 2011
    Location
    London, UK
    Posts
    797
    Blog Entries
    2
    Rep Power
    4

    Default

    JosAh there was a minute difference between our posts, db's post didnt appear until after I posted mines

  6. #6
    JosAH's Avatar
    JosAH is offline Moderator
    Join Date
    Sep 2008
    Location
    Voorschoten, the Netherlands
    Posts
    13,433
    Blog Entries
    7
    Rep Power
    20

    Default

    Quote Originally Posted by ozzyman View Post
    JosAh there was a minute difference between our posts, db's post didnt appear until after I posted mines
    Ah, ok, my fault; sorry about that.

    kind regards,

    Jos
    cenosillicaphobia: the fear for an empty beer glass

  7. #7
    ozzyman's Avatar
    ozzyman is offline Senior Member
    Join Date
    Mar 2011
    Location
    London, UK
    Posts
    797
    Blog Entries
    2
    Rep Power
    4

    Default

    It's cool, i was going to post again saying Ah, db beat me to it, but i didn't wanna make it worse lol

  8. #8
    JosAH's Avatar
    JosAH is offline Moderator
    Join Date
    Sep 2008
    Location
    Voorschoten, the Netherlands
    Posts
    13,433
    Blog Entries
    7
    Rep Power
    20

    Default

    Quote Originally Posted by ozzyman View Post
    It's cool, i was going to post again saying Ah, db beat me to it, but i didn't wanna make it worse lol
    When that happens to me (and it happens often) I edit my previous reply and confess that I'm a slow old sod ... ;-)

    kind regards,

    Jos
    cenosillicaphobia: the fear for an empty beer glass

  9. #9
    trishtren is offline Member
    Join Date
    Feb 2011
    Posts
    53
    Rep Power
    0

    Default

    ah thanks for the help and the advice guys iv made the changes and it works. Thanks heaps :)

Similar Threads

  1. Getting an item from an array
    By ile4 in forum New To Java
    Replies: 9
    Last Post: 01-13-2011, 03:42 PM
  2. Appending and item to a Select List
    By Samurai Coder in forum New To Java
    Replies: 1
    Last Post: 12-04-2009, 10:56 PM
  3. Button to remove item from list
    By dacool25 in forum Java Applets
    Replies: 6
    Last Post: 10-14-2009, 04:30 AM
  4. Replies: 0
    Last Post: 10-15-2008, 05:45 PM
  5. linked list nodes all refernce same item.
    By yllawwally in forum New To Java
    Replies: 0
    Last Post: 12-18-2007, 08:45 PM

Posting Permissions

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