Results 1 to 2 of 2
  1. #1
    L3v3L is offline Member
    Join Date
    May 2012
    Rep Power

    Question How to change object attributes in an ArrayList

    Heres my case:
    I have an arraylist called farm.
    In this farm I have stored Cows with Age, Color and Sex.

    Using an Iterator how would I change all my cows that are 4 years old into the color red, and change sex into male?

    I only know how to use the next() method to access what an iterator points at, but I want it to stay put, so I can do otherthings to the object its pointing at.

    Heres my problem in a simpler situation:
    Java Code:
    //iterator_farm is an iterator to a farm which is just an arraylist with Integers, 
    //how would I print the same number twice without using a var to store it at.
                System.out.printf( + " ");
                System.out.printf( + "\n");

  2. #2
    wsaryada is offline Senior Member
    Join Date
    Jun 2007
    Bali, Indonesia
    Rep Power

    Default Re: How to change object attributes in an ArrayList

    If you look at the API documentation of the next() method of Iterator you will see that it return an Object. So if you store some Cows in the iterator then it will give you the Cow object back. And then you can just do everything your want with your Cow, such as check their age, and when it 4 years old change their color into red, etc.

    If you didn't specifically say that you'll store a cow in the collection you need to do object casting in here.

Similar Threads

  1. Change file attributes
    By Pratyush13 in forum New To Java
    Replies: 0
    Last Post: 11-02-2011, 05:18 AM
  2. Replies: 3
    Last Post: 09-08-2011, 12:53 PM
  3. How to change controls attributes
    By varesa in forum SWT / JFace
    Replies: 3
    Last Post: 06-23-2011, 07:41 PM
  4. Replies: 9
    Last Post: 01-20-2011, 05:01 PM
  5. Classes with object attributes
    By RRasco in forum New To Java
    Replies: 5
    Last Post: 11-25-2008, 01:07 AM

Tags for this Thread

Posting Permissions

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