Hello and welcome to our community! Is this your first visit?
Register
Enjoy an ad free experience by logging in. Not a member yet? Register.
Results 1 to 7 of 7
  1. #1
    New to the CF scene
    Join Date
    Feb 2011
    Posts
    5
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Passing ArrayList?

    Hi fairly new at programming and I'm trying to build my first actual program. I've created an ArrayList in one class and created a method in another class so I can display it on my main class.

    Code:
    import java.util.*;
    
    class PokeMonAbilities {
    
    	private ArrayList<String> ability; 
    
    	public PokeMonAbilities(){
    		
    		ability = new ArrayList<String>();
    		ability.add("d");
    
    	}
    
    	
    	 public ArrayList<String> getAbility(){
    		return ability;
    	}		
    }
    And here's my main class

    Code:
    public static void main(String[] args){
    		
    ...
    System.out.println("Bulbasaur ability is: " + bulbasaur.getAbility());
    	}
    I'm really trying to get my programming skills up any help or suggestions would be great.

  • #2
    God Emperor Fou-Lu's Avatar
    Join Date
    Sep 2002
    Location
    Saskatoon, Saskatchewan
    Posts
    16,987
    Thanks
    4
    Thanked 2,660 Times in 2,629 Posts
    Printing an arraylist probably won't give you its contents; try to iterate it with a foreach instead:
    PHP Code:
    for (String sAbility bulbasaur.getAbility())
    {
        
    System.out.println("Bulbasaur ability is " sAbility);

    You don't show how you are getting the bulbasaur though, and you don't mention what the problem is, so I have to assume its nothing to do with either a compile time error, or a runtime error and only the output.
    PHP Code:
    header('HTTP/1.1 420 Enhance Your Calm'); 

  • #3
    New Coder
    Join Date
    Jan 2011
    Location
    India
    Posts
    31
    Thanks
    0
    Thanked 4 Times in 4 Posts
    Inorder to print the contents of ArrayList your need to iterate over it.

    for(Iterator i = arrayList.iterator();i.hasNext()
    {
    System.out.println(i.next());
    }

    with java 1.5 onwards you can use for each loop to iterate through arrayLists

  • #4
    New to the CF scene
    Join Date
    Feb 2011
    Posts
    5
    Thanks
    0
    Thanked 0 Times in 0 Posts
    Thanks will try it out.

  • #5
    Senior Coder ckeyrouz's Avatar
    Join Date
    Jun 2009
    Location
    Montreal, Canada
    Posts
    1,044
    Thanks
    5
    Thanked 179 Times in 179 Posts
    The solution provided by Fou-Lu is better since it uses the iteration of JDK 1.5 that is more performant than the old Iterator thing.
    Software and cathedrals are much the same - first we build them, then we pray.

  • #6
    New to the CF scene
    Join Date
    Feb 2011
    Posts
    5
    Thanks
    0
    Thanked 0 Times in 0 Posts
    So I still get some compile errors, I wanna make it clear what I'm trying to get accomplished with this program. So I would to make say class B initialize arrayList's with some index's, and class A my main class to print out the index from class B. Thanks for quick responses.

  • #7
    God Emperor Fou-Lu's Avatar
    Join Date
    Sep 2002
    Location
    Saskatoon, Saskatchewan
    Posts
    16,987
    Thanks
    4
    Thanked 2,660 Times in 2,629 Posts
    Can you post what you are using? You have a public method in there to access the underlying ArrayList object, so that shouldn't be a problem at all.
    PHP Code:
    header('HTTP/1.1 420 Enhance Your Calm'); 


  •  

    Posting Permissions

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