Results 1 to 8 of 8
  1. #1
    Join Date
    Dec 2007
    Posts
    24
    Rep Power
    0

    Default ArrayList problem

    package ArrayList;

    public class C1
    {
    public void exec()
    {
    System.out.println("Now we are in c1 Class");
    }

    }

    this is one class...
    then..

    package ArrayList;

    import java.util.ArrayList;
    public class Check
    {
    public static void main(String args[])
    {
    ArrayList<Object> a1=new ArrayList<Object>(12);
    C1 aa=new C1();
    a1.add(aa);
    for(int i=0;i<a1.size();i++)
    {

    }
    }
    }

    I have to access that exec method of the class c1 in the for loop of the check class
    please tell me howit is possible

  2. #2
    revathi17 is offline Member
    Join Date
    Aug 2007
    Posts
    26
    Rep Power
    0

    Default

    you just have to call

    aa.exec();

    inside the for loop..

    Java Code:
    import java.util.ArrayList;
    
    public class Check {
    	public static void main(String[] args){
    		ArrayList<Object> a1 = new ArrayList<Object>(12);
    		C1 aa = new C1();
    		a1.add(aa);
    		for(int i=0; i<a1.size(); i++){
    			aa.exec();
    		}
    	}
    
    }

  3. #3
    Join Date
    Dec 2007
    Posts
    24
    Rep Power
    0

    Default

    ya mam i knoew that but i must call that method by using arraylist only thats the work given for me my boss

  4. #4
    revathi17 is offline Member
    Join Date
    Aug 2007
    Posts
    26
    Rep Power
    0

    Default

    Ok..I don't think you mentioned that in the previous posts...
    anyway, if you have to call it via the arraylist you can get the value you just added to the list and then call the method exec(), like this:
    Java Code:
    public class Check {
    	public static void main(String[] args){
    		ArrayList<Object> a1 = new ArrayList<Object>(12);
    		C1 aa = new C1();
    		a1.add(aa);
    		for(int i=0; i<a1.size(); i++){
    			C1 bb = (C1)a1.get(i);
    			bb.exec();
    		}
    	}
    
    }
    I hope I understood what you actually need..

    -R

  5. #5
    Join Date
    Dec 2007
    Posts
    24
    Rep Power
    0

    Default

    its giving error mam dupilicate local variable bb

  6. #6
    revathi17 is offline Member
    Join Date
    Aug 2007
    Posts
    26
    Rep Power
    0

    Default

    Are you sure you are running the same code? I just ran the code and it works fine for me.I copied it from my editor and pasted it here..
    Just check if you have instantiated bb twice. Only that would give a duplicate variable error...

  7. #7
    Join Date
    Dec 2007
    Posts
    24
    Rep Power
    0

    Default

    ok ok there is no way to run without specifieng the orginal class, with only object get from the array list mam

  8. #8
    spoon! is offline Member
    Join Date
    Dec 2007
    Posts
    12
    Rep Power
    0

    Default

    because how do you know that that object has a .exec() method?

Similar Threads

  1. Replies: 2
    Last Post: 04-21-2008, 11:43 AM
  2. ArrayList problem with images
    By Cymro in forum New To Java
    Replies: 2
    Last Post: 02-05-2008, 06:22 PM
  3. ArrayList
    By kizilbas1 in forum New To Java
    Replies: 1
    Last Post: 01-12-2008, 08:48 PM
  4. ArrayList problem (finding largest no)
    By bugger in forum New To Java
    Replies: 3
    Last Post: 12-12-2007, 12:47 PM
  5. Help ArrayList.add()
    By eNine in forum New To Java
    Replies: 2
    Last Post: 08-06-2007, 01:13 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
  •