Results 1 to 4 of 4
  1. #1
    Join Date
    Jan 2013
    Posts
    2
    Rep Power
    0

    Default Help with arraylists!

    Hi, Im new to this forum and to the java scene so my apologies if my "jargon" and knowledge of it isnt very good - you have to start somewhere i guess.
    I am having trouble with an assignment so I need a bit of guidance what to do as I have been stuck for awhile and I bet its pretty easy..

    I have created an arraylist

    private ArrayList<Object> shapes;

    and then have constructed it as:

    shapes = new ArrayList<Object>()

    When the mouse is clicked on the JPanel a new object is created:
    shapes.add(new Object(e.getX(), e.getY(), currentLevel, currentLength));

    now here is the problem:

    When a button is clicked a method is called and its supposed to increase the currentLevel of each element in the list by 1.

    Can anyone help me with this?

    Thanks in advance..

  2. #2
    sehudson's Avatar
    sehudson is offline Senior Member
    Join Date
    Mar 2010
    Posts
    356
    Rep Power
    5

    Default Re: Help with arraylists!

    The Object class does not have a constructor that takes in 4 parameters. Based on the name of your ArrayList (shapes), you probably want to create a Shape class that has x, y, level, and length attributes.

    Then, create and initialize an ArrayList of Shape objects, rather than an ArrayList of Objects, and populate your ArrayList.
    Last edited by sehudson; 01-23-2013 at 08:50 PM.

  3. #3
    Join Date
    Jan 2013
    Posts
    2
    Rep Power
    0

    Default Re: Help with arraylists!

    Ok i see, but then how do I increase the currentLevel of each element in the list by 1.
    Ive got a for loop going through the size but i tried just doing currentLevel += 1, and it doesnt seem to be what i want.

  4. #4
    Tolls is offline Moderator
    Join Date
    Apr 2009
    Posts
    12,016
    Rep Power
    20

    Default Re: Help with arraylists!

    Does currentLevel increment each time the button is clicked?
    Does it ever get reset (other than closing and relaunching the program)?

    If the answers are 'yes' and 'no' respectively then you could simply hold a private static variable in your Shape class (naming classes after common Java classes is a bad idea) called CURRENT_LEVEL, then simply assign that in the constructor (don't bother passing it in) and assign it to the local currentLevel attribute, then increment it.
    Please do not ask for code as refusal often offends.

    ** This space for rent **

Similar Threads

  1. I don't get ArrayLists
    By Gio!? in forum New To Java
    Replies: 10
    Last Post: 05-03-2012, 12:50 PM
  2. Arraylists
    By talia in forum New To Java
    Replies: 5
    Last Post: 01-30-2012, 06:44 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
  •