CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Results 1 to 5 of 5
  1. #1
    Join Date
    Feb 2011
    Posts
    2

    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. #2
    Join Date
    May 2006
    Location
    UK
    Posts
    4,473

    Re: Passing ArrayList?

    So what's your question?
    Posting code? Use code tags like this: [code]...Your code here...[/code]
    Click here for examples of Java Code

  3. #3
    dlorde is offline Elite Member Power Poster
    Join Date
    Aug 1999
    Location
    UK
    Posts
    10,163

    Re: Passing ArrayList?

    Quote Originally Posted by 0c0de View Post
    I'm really trying to get my programming skills up any help or suggestions would be great.
    Your PokeMonAbilities class has a private ArrayList of abilities (why call it 'ability' (singular) when it is a list (plural)?), but it passes this private data out to anyone who asks for it, which means anyone can modify it outside the PokeMonAbilities class. This is undesirable for two main reasons - firstly, the PokeMonAbilities class loses control over its data, making it hard to keep it consistent and hard to debug when errors occur. Secondly, it raises design questions - why have the PokeMonAbilities class if you're going to pass the raw list around for anyone to modify anyway? Why declare it private if you've effectively made it public?

    A class should have exclusive control over the private data it contains. Either give the class methods that others can use to manipulate the list, or pass out a read-only list (you can use the unmodifiableList method of the Collections class). The former is option is usually preferred, but I can't say which is better for you without knowing why you want to pass out the raw list.

    Oh, and you can't print out a list just by passing it to a print method. You'll need to loop though it and print each element in the list.

    Everyone by now presumably knows about the danger of premature optimization. I think we should be just as worried about premature design — designing too early what a program should do...
    Paul Graham
    Last edited by dlorde; February 4th, 2011 at 06:16 AM.
    Please use &#91;CODE]...your code here...&#91;/CODE] tags when posting code. If you get an error, please post the full error message and stack trace, if present.

  4. #4
    Join Date
    Feb 2011
    Posts
    2

    Re: Passing ArrayList?

    Thanks for the response. I should of made my question more clear, what I' m trying to get accomplished with this program, is that one class would contain data of all the abilities in an array string, and from the main class I would like to pull an array index from that array class and print it.

  5. #5
    Join Date
    Feb 2011
    Posts
    27

    Re: Passing ArrayList?

    Quote Originally Posted by 0c0de View Post
    Thanks for the response. I should of made my question more clear, what I' m trying to get accomplished with this program, is that one class would contain data of all the abilities in an array string, and from the main class I would like to pull an array index from that array class and print it.
    Maybe you should make a method in your PokeMonAbilities class that takes an Integer (the array index) and returns a String (the value stored in that array index)

    Then assuming bulbasaur is an Object of type PokeMonAbilities, you can call your new method with an Integer parameter and, if you write the method correctly it will return the String stored at the index you supplied it.

Posting Permissions

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





Click Here to Expand Forum to Full Width

Featured