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 2 of 2

Thread: adding an object to an arrayList

  1. #1
    Junior Member
    Join Date
    Oct 2011
    Posts
    3
    My Mood
    Inspired
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default adding an object to an arrayList

    Hello:

    I have an interface called Taxable,
    interface Taxable
    {
          public String getName();
          public float getIncome();
          public double getTax();
          public Object clone();
     
    }

    And a Taxpayer class which implements cloneable interface and Taxable interface.

    Now I am creating a TaxpayerGroup , where TaxPayer objects will be added.
    TaxListener is an Interface.

    Question:
    In my add(Taxable obj) method; iPayer.Add(new obj.clone()); does not work.
    Please could you tell me,how do I add Taxable object into my arrayList?


    import java.util.*;
     
    public class TaxpayerGroup
    {
        private TaxListener[] iListener = new TaxListener[4];
        private static ArrayList<Taxpayer> iObjs = new ArrayList<Taxpayer>();
     
        public TaxpayerGroup()
        {
     
     
        }
     
        public void addListener(TaxListener listener)
        {
               for(int i =0; i < iListener.length; i++)
               {
                       iListener[i] = listener;
               }
     
        }
     
        public void add(Taxable obj)
        {
               if(!iObjs.contains(obj))
               {
                   iObjs.add(obj);
                   for(TaxListener lis : iListener)
                   {
                         lis.added(obj);
                   }
               }
     
        }    
     
    }

    Thank you,
    Regards,
    Last edited by urpalshu; October 28th, 2011 at 04:04 PM.


  2. #2
    Junior Member
    Join Date
    Oct 2011
    Posts
    10
    My Mood
    Cheerful
    Thanks
    0
    Thanked 1 Time in 1 Post

    Default Re: adding an object to an arrayList

    Here I counld not understand your quection well.but as i understood you couldn't able to put objects details into array list.In that case i can give you a tip for your problem.If this is not suffient to solve your problem please give more details.Thanks very much.

    import java.util.ArrayList;


    public class TaxpayerGroup {

    private Taxpayer []object=new Taxpayer[4]; //object array
    private ArrayList<Taxpayer> Tax=new ArrayList<Taxpayer>(); //arraylist to those all objects

    public TaxpayerGroup()
    {
    object[0]=new Taxpayer("ravi", 23); //initialize object
    object[1]=new Taxpayer("chanaka", 24);
    object[2]=new Taxpayer("sirimanna", 25);
    object[3]=new Taxpayer("yashodha", 22);

    Tax.add(object[0]); //add to the arraylist
    Tax.add(object[1]);
    Tax.add(object[2]);
    Tax.add(object[3]);


    }

    public void display()
    {
    int n=Tax.size();
    for(int i=0;i<n;i++)
    {
    System.out.println(Tax.get(i).name+" "+Tax.get(i).age);

    }

    }


    public static void main(String[] args) {
    TaxpayerGroup TPG=new TaxpayerGroup();
    TPG.display();
    }
    }

    public class Taxpayer {

    String name;
    int age;

    public Taxpayer(String A,int B)
    {
    name=A;
    age=B;
    }

    }

    This is only model.this is not answer for your problem.hope your reply....

Similar Threads

  1. Help with ArrayList ( Adding certain elements together)
    By williamsant in forum Collections and Generics
    Replies: 13
    Last Post: September 27th, 2011, 10:32 AM
  2. [SOLVED] How to get the return of a method inside an object in an ArrayList?
    By Hallowed in forum Java Theory & Questions
    Replies: 7
    Last Post: May 1st, 2011, 11:44 PM
  3. SAX Parser will not add object to arrayList
    By michaelz in forum What's Wrong With My Code?
    Replies: 7
    Last Post: July 1st, 2010, 03:59 PM
  4. Edit ArrayList Object
    By frankycool in forum Collections and Generics
    Replies: 12
    Last Post: November 16th, 2009, 12:31 AM
  5. Arraylist or Arraylist Object?
    By igniteflow in forum Collections and Generics
    Replies: 2
    Last Post: September 11th, 2009, 03:08 AM