Go Back   CodingForums.com > :: Server side development > Java and JSP

Before you post, read our: Rules & Posting Guidelines

Reply
 
Thread Tools Rate Thread
Enjoy an ad free experience by logging in. Not a member yet? Register.
Old 02-02-2011, 01:23 AM   PM User | #1
0c0de
New to the CF scene

 
Join Date: Feb 2011
Posts: 5
Thanks: 0
Thanked 0 Times in 0 Posts
0c0de is an unknown quantity at this point
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.
0c0de is offline   Reply With Quote
Old 02-02-2011, 12:56 PM   PM User | #2
Fou-Lu
God Emperor


 
Fou-Lu's Avatar
 
Join Date: Sep 2002
Location: Saskatoon, Saskatchewan
Posts: 15,635
Thanks: 4
Thanked 2,448 Times in 2,417 Posts
Fou-Lu is a name known to allFou-Lu is a name known to allFou-Lu is a name known to allFou-Lu is a name known to allFou-Lu is a name known to allFou-Lu is a name known to all
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.
__________________
As of PHP 5.5, the MySQL library has been officially deprecated. It is recommended to move to either MySQLi or PDO libraries for your mysql connectivity. See here for help choosing which interface you prefer: http://php.net/manual/en/mysqlinfo.api.choosing.php
Fou-Lu is offline   Reply With Quote
Old 02-02-2011, 01:25 PM   PM User | #3
sandeep.e
New Coder

 
Join Date: Jan 2011
Location: India
Posts: 31
Thanks: 0
Thanked 4 Times in 4 Posts
sandeep.e is an unknown quantity at this point
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
sandeep.e is offline   Reply With Quote
Old 02-03-2011, 01:48 AM   PM User | #4
0c0de
New to the CF scene

 
Join Date: Feb 2011
Posts: 5
Thanks: 0
Thanked 0 Times in 0 Posts
0c0de is an unknown quantity at this point
Thanks will try it out.
0c0de is offline   Reply With Quote
Old 02-03-2011, 08:26 PM   PM User | #5
ckeyrouz
Senior Coder

 
ckeyrouz's Avatar
 
Join Date: Jun 2009
Location: Montreal, Canada
Posts: 1,044
Thanks: 5
Thanked 179 Times in 179 Posts
ckeyrouz is on a distinguished road
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.
ckeyrouz is offline   Reply With Quote
Old 02-07-2011, 09:24 PM   PM User | #6
0c0de
New to the CF scene

 
Join Date: Feb 2011
Posts: 5
Thanks: 0
Thanked 0 Times in 0 Posts
0c0de is an unknown quantity at this point
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.
0c0de is offline   Reply With Quote
Old 02-07-2011, 09:31 PM   PM User | #7
Fou-Lu
God Emperor


 
Fou-Lu's Avatar
 
Join Date: Sep 2002
Location: Saskatoon, Saskatchewan
Posts: 15,635
Thanks: 4
Thanked 2,448 Times in 2,417 Posts
Fou-Lu is a name known to allFou-Lu is a name known to allFou-Lu is a name known to allFou-Lu is a name known to allFou-Lu is a name known to allFou-Lu is a name known to all
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.
__________________
As of PHP 5.5, the MySQL library has been officially deprecated. It is recommended to move to either MySQLi or PDO libraries for your mysql connectivity. See here for help choosing which interface you prefer: http://php.net/manual/en/mysqlinfo.api.choosing.php
Fou-Lu is offline   Reply With Quote
Reply

Bookmarks

Jump To Top of Thread


Thread Tools
Rate This Thread
Rate This Thread:

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off

Forum Jump


All times are GMT +1. The time now is 03:16 PM.


Advertisement
Log in to turn off these ads.